-
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Transform type exports to "export type" in order to comply with isola…
…tedModules (#278) When the TypeScript option isolatedModules is used, re-exported types must be exported as `export type { ... }` or `export { type ... }`. This fixup determines the kind of export (type or anything else) and adapts the export statement accordingly. --------- Co-authored-by: Lucas Treffenstädt <[email protected]>
- Loading branch information
1 parent
3fa31b3
commit 1b228d0
Showing
78 changed files
with
267 additions
and
140 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ jobs: | |
strategy: | ||
fail-fast: false | ||
matrix: | ||
node: [14, 18] | ||
node: [18, 20] | ||
os: [ubuntu-latest, windows-latest] | ||
|
||
name: Node ${{ matrix.node }} on ${{ matrix.os }} | ||
|
@@ -24,11 +24,11 @@ jobs: | |
# frequently hangs for whatever reason, | ||
# and we have no platform-specific code anyway… | ||
- name: upload coverage | ||
if: matrix.os == 'ubuntu-latest' && matrix.node == 18 | ||
if: matrix.os == 'ubuntu-latest' && matrix.node == 20 | ||
timeout-minutes: 1 | ||
continue-on-error: true | ||
run: bash <(curl -s https://codecov.io/bash) -t ${{secrets.CODECOV_TOKEN}} -B ${{ github.ref }} -f coverage/coverage-final.json | ||
# test the minimum supported peer dependency version | ||
- run: npm install typescript@4.1 [email protected] | ||
- run: npm install typescript@4.5 [email protected] | ||
# aka `npm test` without the `pretest/build` | ||
- run: node .build/tests/index.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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,149 @@ | ||
import ts from "typescript"; | ||
|
||
type NamedExport = { | ||
localName: string; | ||
exportedName: string; | ||
kind: 'type' | 'value'; | ||
}; | ||
type ExportDeclaration = { | ||
location: { | ||
start: number; | ||
end: number; | ||
}; | ||
exports: Array<NamedExport> | ||
} | ||
|
||
export class ExportsFixer { | ||
private readonly DEBUG = !!(process.env.DTS_EXPORTS_FIXER_DEBUG); | ||
constructor(private readonly source: ts.SourceFile) {} | ||
|
||
public fix(): string { | ||
const exports = this.findExports(); | ||
exports.sort((a, b) => a.location.start - b.location.start); | ||
return this.getCodeParts(exports).join(''); | ||
} | ||
|
||
private findExports(): Array<ExportDeclaration> { | ||
const { rawExports, values, types} = this.getExportsAndLocals(); | ||
|
||
return rawExports.map((rawExport) => { | ||
const elements = rawExport.elements.map((e) => { | ||
const exportedName = e.name.text; | ||
const localName = e.propertyName?.text ?? e.name.text; | ||
const kind = types.some(node => node.getText() === localName) && !values.some(node => node.getText() === localName) ? 'type' as const : 'value' as const; | ||
return { | ||
exportedName, | ||
localName, | ||
kind | ||
} | ||
}) | ||
return { | ||
location: { | ||
start: rawExport.getStart(), | ||
end: rawExport.getEnd(), | ||
}, | ||
exports: elements | ||
}; | ||
}); | ||
} | ||
|
||
private getExportsAndLocals(statements: Iterable<ts.Node> = this.source.statements) { | ||
const rawExports: Array<ts.NamedExports> = []; | ||
const values: Array<ts.Identifier> = []; | ||
const types: Array<ts.Identifier> = []; | ||
|
||
const recurseInto = (subStatements: Iterable<ts.Node>) => { | ||
const { rawExports: subExports, values: subValues, types: subTypes} = this.getExportsAndLocals(subStatements); | ||
rawExports.push(...subExports); | ||
values.push(...subValues); | ||
types.push(...subTypes); | ||
}; | ||
|
||
for (const statement of statements) { | ||
this.DEBUG && console.log(statement.getText(), statement.kind); | ||
if (ts.isImportDeclaration(statement)) { | ||
continue; | ||
} | ||
if (ts.isInterfaceDeclaration(statement) || ts.isTypeAliasDeclaration(statement)) { | ||
this.DEBUG && console.log(`${statement.name.getFullText()} is a type`); | ||
types.push(statement.name); | ||
continue; | ||
} | ||
if ( | ||
ts.isEnumDeclaration(statement) || | ||
ts.isFunctionDeclaration(statement) || | ||
ts.isClassDeclaration(statement) || | ||
ts.isVariableStatement(statement) | ||
) { | ||
if (ts.isVariableStatement(statement)) { | ||
for (const declaration of statement.declarationList.declarations) { | ||
if (ts.isIdentifier(declaration.name)) { | ||
this.DEBUG && console.log(`${declaration.name.getFullText()} is a value (from var statement)`); | ||
values.push(declaration.name); | ||
} | ||
} | ||
} else { | ||
if (statement.name) { | ||
this.DEBUG && console.log(`${statement.name.getFullText()} is a value (from declaration)`); | ||
values.push(statement.name); | ||
} | ||
} | ||
continue; | ||
} | ||
if (ts.isModuleBlock(statement)) { | ||
const subStatements = statement.statements; | ||
recurseInto(subStatements); | ||
continue; | ||
} | ||
if (ts.isModuleDeclaration(statement)) { | ||
recurseInto(statement.getChildren()); | ||
continue; | ||
} | ||
if (ts.isExportDeclaration(statement)) { | ||
if (statement.moduleSpecifier) { | ||
continue; | ||
} | ||
if (statement.isTypeOnly) { | ||
// no fixup neccessary | ||
continue; | ||
} | ||
const exportClause = statement.exportClause; | ||
if (!exportClause || !ts.isNamedExports(exportClause)) { | ||
continue; | ||
} | ||
rawExports.push(exportClause); | ||
continue; | ||
} | ||
this.DEBUG && console.log('unhandled statement', statement.getFullText(), statement.kind); | ||
} | ||
return { rawExports, values, types }; | ||
} | ||
|
||
private createNamedExport(exportSpec: NamedExport, elideType = false) { | ||
return `${!elideType && exportSpec.kind === 'type' ? 'type ' : ''}${exportSpec.localName}${exportSpec.localName === exportSpec.exportedName ? '' : ` as ${exportSpec.exportedName}`}`; | ||
} | ||
|
||
private getCodeParts(exports: Array<ExportDeclaration>) { | ||
let cursor = 0; | ||
const code = this.source.getFullText(); | ||
const parts: Array<string> = []; | ||
for (const exportDeclaration of exports) { | ||
const head = code.slice(cursor, exportDeclaration.location.start); | ||
if (head.length > 0) { | ||
parts.push(head); | ||
} | ||
parts.push(this.getExportStatement(exportDeclaration)); | ||
|
||
cursor = exportDeclaration.location.end; | ||
} | ||
if (cursor < code.length) { | ||
parts.push(code.slice(cursor)); | ||
} | ||
return parts; | ||
} | ||
|
||
private getExportStatement(exportDeclaration: ExportDeclaration) { | ||
const isTypeOnly = exportDeclaration.exports.every((e) => e.kind === 'type') && exportDeclaration.exports.length > 0; | ||
return `${isTypeOnly ? 'type ' : ''}{ ${exportDeclaration.exports.map((exp) => this.createNamedExport(exp, isTypeOnly)).join(', ')} }` | ||
} | ||
} |
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 |
---|---|---|
|
@@ -6,4 +6,4 @@ declare const fn: { | |
(arg: string): string; | ||
staticProp: string; | ||
}; | ||
export { I, fn }; | ||
export { type I, fn }; |
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 |
---|---|---|
|
@@ -10,4 +10,4 @@ type Klass = { | |
[0]: C; | ||
[Dprop]: D; | ||
}; | ||
export { Klass }; | ||
export type { Klass }; |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
interface Foo { | ||
new (): any; | ||
} | ||
export { Foo }; | ||
export type { Foo }; |
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
interface Foo {} | ||
export { Foo }; | ||
export type { Foo }; |
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
interface Foo {} | ||
export { Foo }; | ||
export type { Foo }; |
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
interface Foo {} | ||
export { Foo as default }; | ||
export type { Foo as default }; |
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 |
---|---|---|
@@ -1 +1 @@ | ||
export { } | ||
export { } |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
interface Foo {} | ||
declare type Bar = Foo; | ||
export { Bar }; | ||
export type { Bar }; |
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
interface Foo {} | ||
export { Foo }; | ||
export type { Foo }; |
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 |
---|---|---|
@@ -1,8 +1,6 @@ | ||
interface A {} | ||
type foo_d_A = A; | ||
declare namespace foo_d { | ||
export { | ||
foo_d_A as A, | ||
}; | ||
export type { foo_d_A as A }; | ||
} | ||
export { foo_d as foo }; |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
interface B {} | ||
declare class A {} | ||
export { A, B }; | ||
export { A, type B }; |
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
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
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
interface Bar {} | ||
interface Foo extends Bar {} | ||
export { Foo }; | ||
export type { Foo }; |
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
interface Foo {} | ||
export { Foo }; | ||
export type { Foo }; |
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 |
---|---|---|
|
@@ -2,4 +2,4 @@ interface Bar {} | |
interface Foo { | ||
bar: Bar; | ||
} | ||
export { Foo }; | ||
export type { Foo }; |
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 |
---|---|---|
|
@@ -2,4 +2,4 @@ interface Bar {} | |
interface Foo { | ||
bar: Bar; | ||
} | ||
export { Foo }; | ||
export type { Foo }; |
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
interface Foo {} | ||
export { Foo }; | ||
export type { Foo }; |
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 |
---|---|---|
|
@@ -3,4 +3,4 @@ interface Foo { | |
ns1: foo; | ||
ns2: typeof foo; | ||
} | ||
export { Foo }; | ||
export type { Foo }; |
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 |
---|---|---|
|
@@ -4,4 +4,4 @@ interface Bar<T> { | |
interface Foo { | ||
bar: Bar<number>; | ||
} | ||
export { Foo }; | ||
export type { Foo }; |
Oops, something went wrong.