-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
resources: switch internal scripts to TS (#3562)
- Loading branch information
1 parent
2d1ec99
commit 83a2e84
Showing
14 changed files
with
269 additions
and
170 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
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import * as assert from 'node:assert'; | ||
import * as util from 'node:util'; | ||
|
||
import * as ts from 'typescript'; | ||
|
||
/** | ||
* Adds extension to all paths imported inside MJS files | ||
* | ||
* Transforms: | ||
* | ||
* ``` | ||
* import { foo } from './bar'; | ||
* export { foo } from './bar'; | ||
* ``` | ||
* | ||
* to: | ||
* | ||
* ``` | ||
* import { foo } from './bar.mjs'; | ||
* export { foo } from './bar.mjs'; | ||
* ``` | ||
* | ||
*/ | ||
export function addExtensionToImportPaths(config: { extension: string }) { | ||
const { extension } = config; | ||
return (context: ts.TransformationContext) => { | ||
const { factory } = context; | ||
|
||
return visitSourceFile; | ||
|
||
function visitSourceFile(sourceFile: ts.SourceFile) { | ||
return ts.visitNode(sourceFile, visitNode); | ||
} | ||
|
||
function visitNode(node: ts.Node): ts.Node { | ||
const source: string | undefined = (node as any).moduleSpecifier?.text; | ||
if (source?.startsWith('./') || source?.startsWith('../')) { | ||
if (ts.isImportDeclaration(node)) { | ||
return factory.updateImportDeclaration( | ||
node, | ||
node.decorators, | ||
node.modifiers, | ||
node.importClause, | ||
ts.createStringLiteral(source + extension), | ||
node.assertClause, | ||
); | ||
} | ||
if (ts.isExportDeclaration(node)) { | ||
return factory.updateExportDeclaration( | ||
node, | ||
node.decorators, | ||
node.modifiers, | ||
node.isTypeOnly, | ||
node.exportClause, | ||
ts.createStringLiteral(source + extension), | ||
node.assertClause, | ||
); | ||
} | ||
|
||
assert( | ||
false, | ||
'Unexpected node with moduleSpecifier: ' + util.inspect(node), | ||
); | ||
} | ||
return ts.visitEachChild(node, visitNode, context); | ||
} | ||
}; | ||
} |
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
Oops, something went wrong.