Skip to content

Commit

Permalink
Merge pull request #380 from ef4/erase-type-imports
Browse files Browse the repository at this point in the history
fix(analyzer): ignore type-only imports & exports
  • Loading branch information
ef4 committed Aug 3, 2021
2 parents 97a40ee + 1440052 commit c748055
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 42 deletions.
71 changes: 40 additions & 31 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/ember-auto-import/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"walk-sync": "^0.3.3"
},
"devDependencies": {
"@babel/plugin-syntax-typescript": "^7.14.5",
"@types/babel__core": "^7.0.4",
"@types/babel__generator": "^7.0.1",
"@types/babel__template": "^7.0.1",
Expand Down
32 changes: 22 additions & 10 deletions packages/ember-auto-import/ts/analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { isEqual, flatten } from 'lodash';
import type Package from './package';
import symlinkOrCopy from 'symlink-or-copy';
import { TransformOptions } from '@babel/core';
import { CallExpression, Expression, File, TSType } from '@babel/types';
import type { CallExpression, ExportNamedDeclaration, Expression, File, ImportDeclaration, TSType } from '@babel/types';
import traverse from '@babel/traverse';

makeDebug.formatters.m = (modules: Import[]) => {
Expand Down Expand Up @@ -79,6 +79,15 @@ export default class Analyzer extends Plugin {

private parse: undefined | ((source: string) => File);

// Ignores type-only imports & exports, which are erased from the final build
// output.
// TypeScript: `import type foo from 'foo'`
// Flow: `import typeof foo from 'foo'`
private erasedImportKinds: Set<ImportDeclaration['importKind']> = new Set(['type', 'typeof']);
// TypeScript: `export type foo from 'foo'`
// Flow: doesn't have type-only exports
private erasedExportKinds: Set<ExportNamedDeclaration['exportKind']> = new Set(['type']);

constructor(inputTree: Node, private pack: Package, private treeType?: TreeType) {
super([inputTree], {
annotation: 'ember-auto-import-analyzer',
Expand Down Expand Up @@ -239,6 +248,8 @@ export default class Analyzer extends Plugin {
}
},
ImportDeclaration: path => {
if (this.erasedImportKinds.has(path.node.importKind)) return;

imports.push({
isDynamic: false,
specifier: path.node.source.value,
Expand All @@ -248,15 +259,16 @@ export default class Analyzer extends Plugin {
});
},
ExportNamedDeclaration: path => {
if (path.node.source) {
imports.push({
isDynamic: false,
specifier: path.node.source.value,
path: relativePath,
package: this.pack,
treeType: this.treeType,
});
}
if (!path.node.source) return;
if (this.erasedExportKinds.has(path.node.exportKind)) return;

imports.push({
isDynamic: false,
specifier: path.node.source.value,
path: relativePath,
package: this.pack,
treeType: this.treeType,
});
},
});
return imports;
Expand Down
31 changes: 30 additions & 1 deletion packages/ember-auto-import/ts/tests/analyzer-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Qmodule('analyzer', function (hooks) {
get babelOptions() {
babelOptionsWasAccessed = true;
return {
plugins: [require.resolve('../../babel-plugin')],
plugins: [require.resolve('@babel/plugin-syntax-typescript'), require.resolve('../../babel-plugin')],
};
},
babelMajorVersion: 7,
Expand Down Expand Up @@ -175,6 +175,35 @@ Qmodule('analyzer', function (hooks) {
assert.deepEqual(analyzer.imports, []);
});

test('type-only imports ignored in created file', async function (assert) {
await builder.build();
let original = `
import type Foo from 'type-import';
import Bar from 'value-import';
export type { Qux } from 'type-re-export';
export { Baz } from 'value-re-export';
`;
outputFileSync(join(upstream, 'sample.js'), original);
await builder.build();
assert.deepEqual(analyzer.imports, [
{
isDynamic: false,
specifier: 'value-import',
path: 'sample.js',
package: pack,
treeType: undefined,
},
{
isDynamic: false,
specifier: 'value-re-export',
path: 'sample.js',
package: pack,
treeType: undefined,
},
]);
});

type LiteralExample = [string, string];
type TemplateExample = [string, string[], string[]];
function isLiteralExample(exp: LiteralExample | TemplateExample): exp is LiteralExample {
Expand Down

0 comments on commit c748055

Please sign in to comment.