Skip to content

Commit b536a89

Browse files
author
Michael Dougall
committed
chore: adds follow imports example
1 parent 18bc3c0 commit b536a89

File tree

7 files changed

+84
-1
lines changed

7 files changed

+84
-1
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const hello = 'world';
2+
3+
export default 'hello';
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { hello } from './import';
2+
3+
console.log(hello);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const hello = 'world';
2+
export default 'hello';
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { hello } from './import';
2+
console.log(hello);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import * as ts from 'typescript';
2+
3+
const transformerProgram = (program: ts.Program) => {
4+
const transformerFactory: ts.TransformerFactory<ts.SourceFile> = context => {
5+
return sourceFile => {
6+
const visitor = (node: ts.Node): ts.Node => {
7+
if (
8+
ts.isImportDeclaration(node) &&
9+
ts.isStringLiteral(node.moduleSpecifier) &&
10+
ts.isNamedImports(node.importClause.namedBindings)
11+
) {
12+
const moduleImportName = node.moduleSpecifier.text;
13+
const { resolvedFileName } = (sourceFile as any).resolvedModules.get(moduleImportName);
14+
const moduleSourceFile = program.getSourceFile(resolvedFileName);
15+
16+
(moduleSourceFile as any).symbol.exports.forEach((_, key) => {
17+
console.log(`found export ${key}`);
18+
});
19+
20+
return node;
21+
}
22+
23+
return ts.visitEachChild(node, visitor, context);
24+
};
25+
26+
return ts.visitNode(sourceFile, visitor);
27+
};
28+
};
29+
30+
return transformerFactory;
31+
};
32+
33+
export default transformerProgram;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "../tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "transformed",
5+
"plugins": [{ "transform": "./transformer.ts" }]
6+
},
7+
"files": ["source.ts"]
8+
}

translations/en/transformer-handbook.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ In its AST representation looks like this:
123123
```
124124

125125
For a more detailed look check out the [AST yourself](https://ts-ast-viewer.com/#code/GYVwdgxgLglg9mABACwKYBt10QCgJSIDeAUImYhAgM5zqoB0WA5jgOQDucATugCat4A3MQC+QA)!
126-
You can see also see the code that you can use the generate the same AST in the bottom left panel,
126+
You can also see the code can be used to generate the same AST in the bottom left panel,
127127
and the selected node metadata in the right panel.
128128
Super useful!
129129

@@ -1100,8 +1100,40 @@ return ts.visitEachChild(node, visitor, context);
11001100
11011101
#### Following module imports
11021102

1103+
Following imports is possible -
1104+
however I'm not currently sure it's supported in user land.
1105+
This also may or may not break when type checking is turned off.
1106+
11031107
> **TODO** - Is this possible in a robust way?
11041108
1109+
```ts
1110+
if (
1111+
ts.isImportDeclaration(node) &&
1112+
ts.isStringLiteral(node.moduleSpecifier) &&
1113+
ts.isNamedImports(node.importClause.namedBindings)
1114+
) {
1115+
const moduleImportName = node.moduleSpecifier.text;
1116+
const { resolvedFileName } = sourceFile.resolvedModules.get(moduleImportName);
1117+
const moduleSourceFile = program.getSourceFile(resolvedFileName);
1118+
1119+
moduleSourceFile.symbol.exports.forEach((_, key) => {
1120+
console.log(`found export ${key}`);
1121+
});
1122+
1123+
return node;
1124+
}
1125+
```
1126+
1127+
Which will log this to the console:
1128+
1129+
```
1130+
found export hello
1131+
found export default
1132+
```
1133+
1134+
> **Tip** - You can s
1135+
> ee the source for this at [/example-transformers/follow-imports](/example-transformers/follow-imports)
1136+
11051137
#### Transforming jsx
11061138

11071139
Typescript can also transform [JSX](https://reactjs.org/docs/introducing-jsx.html) -

0 commit comments

Comments
 (0)