Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly elide TS imports even when referenced by shadowing variables #342

Merged
merged 1 commit into from
Nov 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {Scope} from "./parser/tokenizer/state";
import TokenProcessor from "./TokenProcessor";
import RootTransformer from "./transformers/RootTransformer";
import formatTokens from "./util/formatTokens";
import getTSImportedNames from "./util/getTSImportedNames";

export type Transform = "jsx" | "typescript" | "flow" | "imports";

Expand Down Expand Up @@ -136,10 +137,14 @@ function getSucraseContext(code: string, options: Options): SucraseContext {
enableLegacyTypeScriptModuleInterop,
);
importProcessor.preprocessTokens();
// We need to mark shadowed globals after processing imports so we know that the globals are,
// but before type-only import pruning, since that relies on shadowing information.
identifyShadowedGlobals(tokenProcessor, scopes, importProcessor.getGlobalNames());
if (options.transforms.includes("typescript")) {
importProcessor.pruneTypeOnlyImports();
}
identifyShadowedGlobals(tokenProcessor, scopes, importProcessor.getGlobalNames());
} else if (options.transforms.includes("typescript")) {
identifyShadowedGlobals(tokenProcessor, scopes, getTSImportedNames(tokenProcessor));
}
return {tokenProcessor, scopes, nameManager, importProcessor};
}
3 changes: 2 additions & 1 deletion src/util/getNonTypeIdentifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export function getNonTypeIdentifiers(tokens: TokenProcessor): Set<string> {
!token.isType &&
(token.identifierRole === IdentifierRole.Access ||
token.identifierRole === IdentifierRole.ObjectShorthand ||
token.identifierRole === IdentifierRole.ExportAccess)
token.identifierRole === IdentifierRole.ExportAccess) &&
!token.shadowsGlobal
) {
nonTypeIdentifiers.add(tokens.identifierNameForToken(token));
}
Expand Down
88 changes: 88 additions & 0 deletions src/util/getTSImportedNames.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import {ContextualKeyword} from "../parser/tokenizer";
import {TokenType as tt} from "../parser/tokenizer/types";
import TokenProcessor from "../TokenProcessor";

/**
* Special case code to scan for imported names in ESM TypeScript. We need to do this so we can
* properly get globals so we can compute shadowed globals.
*
* This is similar to logic in CJSImportProcessor, but trimmed down to avoid logic with CJS
* replacement and flow type imports.
*/
export default function getTSImportedNames(tokens: TokenProcessor): Set<string> {
const importedNames = new Set();
for (let i = 0; i < tokens.tokens.length; i++) {
if (
tokens.matchesAtIndex(i, [tt._import]) &&
!tokens.matchesAtIndex(i, [tt._import, tt.name, tt.eq])
) {
collectNamesForImport(tokens, i, importedNames);
}
}
return importedNames;
}

function collectNamesForImport(
tokens: TokenProcessor,
index: number,
importedNames: Set<string>,
): void {
index++;

if (tokens.matchesAtIndex(index, [tt.parenL])) {
// Dynamic import, so nothing to do
return;
}

if (tokens.matchesAtIndex(index, [tt.name])) {
importedNames.add(tokens.identifierNameAtIndex(index));
index++;
if (tokens.matchesAtIndex(index, [tt.comma])) {
index++;
}
}

if (tokens.matchesAtIndex(index, [tt.star])) {
// * as
index += 2;
importedNames.add(tokens.identifierNameAtIndex(index));
index++;
}

if (tokens.matchesAtIndex(index, [tt.braceL])) {
index++;
collectNamesForNamedImport(tokens, index, importedNames);
}
}

function collectNamesForNamedImport(
tokens: TokenProcessor,
index: number,
importedNames: Set<string>,
): void {
while (true) {
if (tokens.matchesAtIndex(index, [tt.braceR])) {
return;
}

// We care about the local name, which might be the first token, or if there's an "as", is the
// one after that.
let name = tokens.identifierNameAtIndex(index);
index++;
if (tokens.matchesContextualAtIndex(index, ContextualKeyword._as)) {
index++;
name = tokens.identifierNameAtIndex(index);
index++;
}
importedNames.add(name);
if (tokens.matchesAtIndex(index, [tt.comma, tt.braceR])) {
return;
} else if (tokens.matchesAtIndex(index, [tt.braceR])) {
return;
} else if (tokens.matchesAtIndex(index, [tt.comma])) {
index++;
} else {
throw new Error(`Unexpected token: ${JSON.stringify(tokens.tokens[index])}`);
}
}
}
54 changes: 54 additions & 0 deletions test/typescript-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1270,4 +1270,58 @@ describe("typescript transform", () => {
`,
);
});

it("properly elides CJS imports that only have value references in shadowed names", () => {
assertTypeScriptResult(
`
import T from './T';

const x: T = 3;

function foo() {
let T = 3;
console.log(T);
}
`,
`"use strict";${IMPORT_DEFAULT_PREFIX}


const x = 3;

function foo() {
let T = 3;
console.log(T);
}
`,
);
});

it("properly elides ESM imports that only have value references in shadowed names", () => {
assertTypeScriptESMResult(
`
import T, {a as b, c} from './T';
import {d, e} from './foo';

const x: T = 3;
console.log(e);

function foo() {
let T = 3, b = 4, c = 5, d = 6;
console.log(T, b, c, d);
}
`,
`

import { e} from './foo';

const x = 3;
console.log(e);

function foo() {
let T = 3, b = 4, c = 5, d = 6;
console.log(T, b, c, d);
}
`,
);
});
});