-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build: convert dedent.ts and tests to TypeScript (#51)
Fixes #50. Integrates with a few tools: * TypeScript itself for type checking (`yarn tsc`) * [Babel's TypeScript preset](https://babeljs.io/docs/babel-preset-typescript) to stick with the existing output process (`yarn build:legacy`, `yarn build:modern`) * [tsup](https://tsup.egoist.dev) to generate a single `.d.ts` from `dedent.ts` (`yarn build:types`) Types are moved from `index.d.ts` to `dist/dedent.d.ts` to match the other `dist/*` files. I confirmed with [the `attw` cli](https://github.com/arethetypeswrong/arethetypeswrong.github.io/tree/e33adb4d894b041a51b8366bf0611ff5ecaf38cb/packages/cli) that these support various Node resolution strategies. `macro.js` and `macro.d.ts` are untouched, per #50 -> #52.
- Loading branch information
1 parent
642d99f
commit 7d735a5
Showing
15 changed files
with
923 additions
and
588 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
dist/ | ||
|
||
# https://github.com/dmnd/dedent/issues/52 | ||
macro.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* eslint-env node */ | ||
module.exports = { | ||
parser: "@typescript-eslint/parser", | ||
parserOptions: { | ||
project: true, | ||
tsconfigRootDir: __dirname, | ||
}, | ||
extends: [ | ||
"eslint:recommended", | ||
"plugin:@typescript-eslint/strict-type-checked", | ||
"plugin:@typescript-eslint/stylistic-type-checked", | ||
], | ||
}; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
export default function dedent(literals: string): string; | ||
export default function dedent( | ||
strings: TemplateStringsArray, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
...values: any[] | ||
): string; | ||
export default function dedent( | ||
strings: string | TemplateStringsArray, | ||
...values: string[] | ||
) { | ||
const raw = typeof strings === "string" ? [strings] : strings.raw; | ||
|
||
// first, perform interpolation | ||
let result = ""; | ||
for (let i = 0; i < raw.length; i++) { | ||
result += raw[i] | ||
// join lines when there is a suppressed newline | ||
.replace(/\\\n[ \t]*/g, "") | ||
// handle escaped backticks | ||
.replace(/\\`/g, "`"); | ||
|
||
if (i < values.length) { | ||
result += values[i]; | ||
} | ||
} | ||
|
||
// now strip indentation | ||
const lines = result.split("\n"); | ||
let mindent: number | null = null; | ||
for (const l of lines) { | ||
const m = l.match(/^(\s+)\S+/); | ||
if (m) { | ||
const indent = m[1].length; | ||
if (!mindent) { | ||
// this is the first indented line | ||
mindent = indent; | ||
} else { | ||
mindent = Math.min(mindent, indent); | ||
} | ||
} | ||
} | ||
|
||
if (mindent !== null) { | ||
const m = mindent; // appease TypeScript | ||
result = lines | ||
// https://github.com/typescript-eslint/typescript-eslint/issues/7140 | ||
// eslint-disable-next-line @typescript-eslint/prefer-string-starts-ends-with | ||
.map((l) => (l[0] === " " || l[0] === "\t" ? l.slice(m) : l)) | ||
.join("\n"); | ||
} | ||
|
||
return ( | ||
result | ||
// dedent eats leading and trailing whitespace too | ||
.trim() | ||
// handle escaped newlines at the end to ensure they don't get stripped too | ||
.replace(/\\n/g, "\n") | ||
); | ||
} |
Oops, something went wrong.