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

Fix TS export elision for destructured variables #564

Merged
merged 1 commit into from
Dec 2, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/parser/traverser/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -837,13 +837,19 @@ function parseObjectProperty(isPattern: boolean, isBlockScope: boolean): void {
// If we're in a destructuring, we've now discovered that the key was actually an assignee, so
// we need to tag it as a declaration with the appropriate scope. Otherwise, we might need to
// transform it on access, so mark it as a normal object shorthand.
let identifierRole;
if (isPattern) {
state.tokens[state.tokens.length - 1].identifierRole = isBlockScope
? IdentifierRole.ObjectShorthandBlockScopedDeclaration
: IdentifierRole.ObjectShorthandFunctionScopedDeclaration;
if (state.scopeDepth === 0) {
identifierRole = IdentifierRole.ObjectShorthandTopLevelDeclaration;
} else if (isBlockScope) {
identifierRole = IdentifierRole.ObjectShorthandBlockScopedDeclaration;
} else {
identifierRole = IdentifierRole.ObjectShorthandFunctionScopedDeclaration;
}
} else {
state.tokens[state.tokens.length - 1].identifierRole = IdentifierRole.ObjectShorthand;
identifierRole = IdentifierRole.ObjectShorthand;
}
state.tokens[state.tokens.length - 1].identifierRole = identifierRole;

// Regardless of whether we know this to be a pattern or if we're in an ambiguous context, allow
// parsing as if there's a default value.
Expand Down
10 changes: 6 additions & 4 deletions src/parser/traverser/lval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ export function parseImportedIdentifier(): void {
}

export function markPriorBindingIdentifier(isBlockScope: boolean): void {
let identifierRole;
if (state.scopeDepth === 0) {
state.tokens[state.tokens.length - 1].identifierRole = IdentifierRole.TopLevelDeclaration;
identifierRole = IdentifierRole.TopLevelDeclaration;
} else if (isBlockScope) {
identifierRole = IdentifierRole.BlockScopedDeclaration;
} else {
state.tokens[state.tokens.length - 1].identifierRole = isBlockScope
? IdentifierRole.BlockScopedDeclaration
: IdentifierRole.FunctionScopedDeclaration;
identifierRole = IdentifierRole.FunctionScopedDeclaration;
}
state.tokens[state.tokens.length - 1].identifierRole = identifierRole;
}

// Parses lvalue (assignable) atom.
Expand Down
28 changes: 28 additions & 0 deletions test/typescript-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1595,6 +1595,34 @@ describe("typescript transform", () => {
);
});

it("preserves exported variables assigned with a destructure", () => {
assertTypeScriptImportResult(
`
const o = {x: 1};
const {x} = o;
const {x: y} = o;
type z = number;
export {x, y, z};
`,
{
expectedCJSResult: `"use strict";${ESMODULE_PREFIX}
const o = {x: 1};
const {x} = o;
const {x: y} = o;

exports.x = x; exports.y = y;
`,
expectedESMResult: `
const o = {x: 1};
const {x} = o;
const {x: y} = o;

export {x, y,};
`,
},
);
});

it("elides export default when the value is an identifier declared as a type", () => {
assertTypeScriptImportResult(
`
Expand Down