diff --git a/tsc/internal/transformers/declarations/transform.go b/tsc/internal/transformers/declarations/transform.go index 986d56b7b8f28..739c685201841 100644 --- a/tsc/internal/transformers/declarations/transform.go +++ b/tsc/internal/transformers/declarations/transform.go @@ -836,7 +836,14 @@ func (tx *DeclarationTransformer) transformTypeParameterDeclaration(input *ast.T func (tx *DeclarationTransformer) transformVariableDeclaration(input *ast.VariableDeclaration) *ast.Node { if tx.state.currentSourceFile.CommonJSModuleIndicator != nil && ast.IsVariableDeclarationInitializedToRequire(input.AsNode()) { - return tx.transformCjsRequireVariableDeclaration(input) + if canTransformCjsRequireToImport(input.AsNode()) { + return tx.transformCjsRequireVariableDeclaration(input) + } + // bindings with dynamic property names can't be named imports, so declare those and any nested or rest + // bindings with their types; the remaining bindings are imported by transformVariableStatement + return tx.recreateBindingElements(core.Filter(input.Name().AsBindingPattern().Elements.Nodes, func(elem *ast.Node) bool { + return !isCjsRequireImportableBinding(elem) + })) } if ast.IsBindingPattern(input.Name()) && hasAnyBindingInitializers(input.Name().AsBindingPattern()) { return tx.recreateBindingPattern(input.Name().AsBindingPattern()) @@ -868,6 +875,24 @@ func hasAnyBindingInitializers(bindingPattern *ast.BindingPattern) bool { return false } +func canTransformCjsRequireToImport(decl *ast.Node) bool { + name := decl.Name() + if !ast.IsObjectBindingPattern(name) { + return true + } + for _, elem := range name.AsBindingPattern().Elements.Nodes { + if propertyName := elem.PropertyName(); propertyName != nil && ast.IsDynamicName(propertyName) { + return false + } + } + return true +} + +func isCjsRequireImportableBinding(elem *ast.Node) bool { + propertyName := elem.PropertyName() + return ast.IsIdentifier(elem.Name()) && elem.AsBindingElement().DotDotDotToken == nil && (propertyName == nil || !ast.IsDynamicName(propertyName)) +} + func (tx *DeclarationTransformer) transformCjsRequireVariableDeclaration(input *ast.VariableDeclaration) *ast.Node { specifier := tx.rewriteModuleSpecifier(input.AsNode(), input.Initializer.AsCallExpression().Arguments.Nodes[0]) if ast.IsIdentifier(input.Name()) { @@ -880,12 +905,33 @@ func (tx *DeclarationTransformer) transformCjsRequireVariableDeclaration(input * // `const {x, y: z} = require("something")` -> `import {x, y as z} from "something"` b := input.Name().AsBindingPattern() + importsAllBindings := canTransformCjsRequireToImport(input.AsNode()) var importSpecifiers []*ast.Node for _, elem := range b.Elements.Nodes { if !ast.IsIdentifier(elem.Name()) { continue // nested destructuring, bail } - importSpecifiers = append(importSpecifiers, tx.Factory().NewImportSpecifier(false, elem.PropertyName(), elem.Name())) + if !importsAllBindings && !isCjsRequireImportableBinding(elem) { + continue // declared as a variable by transformVariableDeclaration + } + propertyName := elem.PropertyName() + if propertyName != nil && !ast.IsIdentifier(propertyName) && !ast.IsStringLiteral(propertyName) { + // numeric and literal computed keys aren't valid import names, so normalize them to an identifier or string + text := ast.GetPropertyNameForPropertyNameNode(propertyName) + if ast.IsComputedPropertyName(propertyName) && ast.IsSignedNumericLiteral(propertyName.Expression()) { + // the property key is the string form of the number, so `[-0]` names property "0" + text = jsnum.FromString(text).String() + } + if scanner.IsIdentifierText(text, core.LanguageVariantStandard) { + propertyName = tx.Factory().NewIdentifier(text) + } else { + propertyName = tx.Factory().NewStringLiteral(text, ast.TokenFlagsNone) + } + } + importSpecifiers = append(importSpecifiers, tx.Factory().NewImportSpecifier(false, propertyName, elem.Name())) + } + if !importsAllBindings && len(importSpecifiers) == 0 { + return nil } return tx.Factory().NewImportDeclaration( nil, @@ -901,8 +947,12 @@ func (tx *DeclarationTransformer) transformCjsRequireVariableDeclaration(input * } func (tx *DeclarationTransformer) recreateBindingPattern(input *ast.BindingPattern) *ast.Node { + return tx.recreateBindingElements(input.Elements.Nodes) +} + +func (tx *DeclarationTransformer) recreateBindingElements(elements []*ast.Node) *ast.Node { var results []*ast.Node - for _, elem := range input.Elements.Nodes { + for _, elem := range elements { result := tx.recreateBindingElement(elem.AsBindingElement()) if result == nil { continue @@ -2258,15 +2308,29 @@ func (tx *DeclarationTransformer) transformVariableStatement(input *ast.Variable if tx.state.currentSourceFile.CommonJSModuleIndicator != nil { var normalDeclarations []*ast.Node var imports []*ast.Node + var partialImports []*ast.Node for _, n := range inputNodes { - if ast.IsVariableDeclarationInitializedToRequire(n) { + if !ast.IsVariableDeclarationInitializedToRequire(n) { + normalDeclarations = append(normalDeclarations, n) + } else if canTransformCjsRequireToImport(n) { imports = append(imports, n) } else { + // dynamic, nested and rest bindings are declared as variables, and static identifier bindings are imported normalDeclarations = append(normalDeclarations, n) + partialImports = append(partialImports, n) } } inputNodes = normalDeclarations extraImports, _ = tx.Visitor().VisitSlice(imports) + for _, n := range partialImports { + // match visitDeclarationSubtree's @internal and visibility checks + if tx.shouldStripInternal(n) || isDeclarationAndNotVisible(tx.EmitContext(), tx.resolver, n) { + continue + } + if imp := tx.transformCjsRequireVariableDeclaration(n.AsVariableDeclaration()); imp != nil { + extraImports = append(extraImports, imp) + } + } } nodes, _ := tx.Visitor().VisitSlice(inputNodes) @@ -2684,6 +2748,10 @@ func (tx *DeclarationTransformer) getNameExpressionPreferringIdentifier(nameExpr return result } } + if ast.IsNoSubstitutionTemplateLiteral(nameExpr) { + // template literals aren't valid module export names + return tx.Factory().NewStringLiteral(nameExpr.Text(), ast.TokenFlagsNone) + } return nameExpr } diff --git a/tsc/testdata/baselines/reference/compiler/declarationEmitCommonjsRequireComputedKey.js b/tsc/testdata/baselines/reference/compiler/declarationEmitCommonjsRequireComputedKey.js new file mode 100644 index 0000000000000..7a3ebaedbae12 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitCommonjsRequireComputedKey.js @@ -0,0 +1,66 @@ +//// [tests/cases/compiler/declarationEmitCommonjsRequireComputedKey.ts] //// + +//// [a.js] +exports.value = 123; +exports.text = "text"; +exports.other = true; +exports.nested = { value: 1 }; +class Obj { + x = 1; +} +exports.Obj = Obj; + +//// [b.js] +const key = "value"; +const {[key]: dynamic, ["text"]: text, other, nested: {value}, ...rest} = require("./a"), + sibling = require("./a"); +exports.dynamic = dynamic; +exports.text = text; +exports.other = other; +exports.value = value; +exports.rest = rest; +exports.sibling = sibling; + +const {[key]: extra, Obj} = require("./a"); +/** @param {Obj} o */ +function useObj(o) {} +exports.Obj = Obj; +exports.extra = extra; +exports.useObj = useObj; + +const {[key]: unusedDynamic, text: unusedText} = require("./a"), keep = 1; +exports.keep = keep; + + + + +//// [a.d.ts] +export declare var value: 123; +export declare var text: "text"; +export declare var other: true; +export declare var nested: { + value: number; +}; +export { Obj }; +declare class Obj { + x: number; +} +//// [b.d.ts] +export { dynamic }; +export { text }; +export { other }; +export { value }; +export { rest }; +export { sibling }; +export { Obj }; +export { extra }; +export { useObj }; +export { keep }; +import sibling = require("./a"); +import { text as text, other } from "./a"; +declare const dynamic: any, value: number, rest: any; +import { Obj } from "./a"; +declare const extra: any; +/** @param {Obj} o */ +declare function useObj(o: Obj): void; +declare const keep = 1; diff --git a/tsc/testdata/baselines/reference/compiler/declarationEmitCommonjsRequireComputedKey.symbols b/tsc/testdata/baselines/reference/compiler/declarationEmitCommonjsRequireComputedKey.symbols new file mode 100644 index 0000000000000..76370666c6841 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitCommonjsRequireComputedKey.symbols @@ -0,0 +1,138 @@ +//// [tests/cases/compiler/declarationEmitCommonjsRequireComputedKey.ts] //// + +=== b.js === +const key = "value"; +>key : Symbol(key, Decl(b.js, 0, 5)) + +const {[key]: dynamic, ["text"]: text, other, nested: {value}, ...rest} = require("./a"), +>key : Symbol(key, Decl(b.js, 0, 5)) +>dynamic : Symbol(dynamic, Decl(b.js, 1, 7)) +>"text" : Symbol(text, Decl(b.js, 1, 22)) +>text : Symbol(text, Decl(b.js, 1, 22)) +>other : Symbol(other, Decl(b.js, 1, 38)) +>nested : Symbol(sibling.nested, Decl(a.js, 2, 21)) +>value : Symbol(value, Decl(b.js, 1, 55)) +>rest : Symbol(rest, Decl(b.js, 1, 62)) +>require : Symbol(require) +>"./a" : Symbol(sibling, Decl(a.js, 0, 0)) + + sibling = require("./a"); +>sibling : Symbol(sibling, Decl(b.js, 1, 89)) +>require : Symbol(require) +>"./a" : Symbol(sibling, Decl(a.js, 0, 0)) + +exports.dynamic = dynamic; +>exports.dynamic : Symbol(dynamic, Decl(b.js, 2, 29)) +>exports : Symbol(exports, Decl(b.js, 0, 0)) +>dynamic : Symbol(dynamic, Decl(b.js, 2, 29)) +>dynamic : Symbol(dynamic, Decl(b.js, 1, 7)) + +exports.text = text; +>exports.text : Symbol(text, Decl(b.js, 3, 26)) +>exports : Symbol(exports, Decl(b.js, 0, 0)) +>text : Symbol(text, Decl(b.js, 3, 26)) +>text : Symbol(text, Decl(b.js, 1, 22)) + +exports.other = other; +>exports.other : Symbol(other, Decl(b.js, 4, 20)) +>exports : Symbol(exports, Decl(b.js, 0, 0)) +>other : Symbol(other, Decl(b.js, 4, 20)) +>other : Symbol(other, Decl(b.js, 1, 38)) + +exports.value = value; +>exports.value : Symbol(value, Decl(b.js, 5, 22)) +>exports : Symbol(exports, Decl(b.js, 0, 0)) +>value : Symbol(value, Decl(b.js, 5, 22)) +>value : Symbol(value, Decl(b.js, 1, 55)) + +exports.rest = rest; +>exports.rest : Symbol(rest, Decl(b.js, 6, 22)) +>exports : Symbol(exports, Decl(b.js, 0, 0)) +>rest : Symbol(rest, Decl(b.js, 6, 22)) +>rest : Symbol(rest, Decl(b.js, 1, 62)) + +exports.sibling = sibling; +>exports.sibling : Symbol(sibling, Decl(b.js, 7, 20)) +>exports : Symbol(exports, Decl(b.js, 0, 0)) +>sibling : Symbol(sibling, Decl(b.js, 7, 20)) +>sibling : Symbol(sibling, Decl(b.js, 1, 89)) + +const {[key]: extra, Obj} = require("./a"); +>key : Symbol(key, Decl(b.js, 0, 5)) +>extra : Symbol(extra, Decl(b.js, 10, 7)) +>Obj : Symbol(Obj, Decl(b.js, 10, 20)) +>require : Symbol(require) +>"./a" : Symbol(sibling, Decl(a.js, 0, 0)) + +/** @param {Obj} o */ +function useObj(o) {} +>useObj : Symbol(useObj, Decl(b.js, 10, 43)) +>o : Symbol(o, Decl(b.js, 12, 16)) + +exports.Obj = Obj; +>exports.Obj : Symbol(Obj, Decl(b.js, 12, 21)) +>exports : Symbol(exports, Decl(b.js, 0, 0)) +>Obj : Symbol(Obj, Decl(b.js, 12, 21)) +>Obj : Symbol(Obj, Decl(b.js, 10, 20)) + +exports.extra = extra; +>exports.extra : Symbol(extra, Decl(b.js, 13, 18)) +>exports : Symbol(exports, Decl(b.js, 0, 0)) +>extra : Symbol(extra, Decl(b.js, 13, 18)) +>extra : Symbol(extra, Decl(b.js, 10, 7)) + +exports.useObj = useObj; +>exports.useObj : Symbol(useObj, Decl(b.js, 14, 22)) +>exports : Symbol(exports, Decl(b.js, 0, 0)) +>useObj : Symbol(useObj, Decl(b.js, 14, 22)) +>useObj : Symbol(useObj, Decl(b.js, 10, 43)) + +const {[key]: unusedDynamic, text: unusedText} = require("./a"), keep = 1; +>key : Symbol(key, Decl(b.js, 0, 5)) +>unusedDynamic : Symbol(unusedDynamic, Decl(b.js, 17, 7)) +>text : Symbol(unusedText, Decl(a.js, 0, 20)) +>unusedText : Symbol(unusedText, Decl(b.js, 17, 28)) +>require : Symbol(require) +>"./a" : Symbol(sibling, Decl(a.js, 0, 0)) +>keep : Symbol(keep, Decl(b.js, 17, 64)) + +exports.keep = keep; +>exports.keep : Symbol(keep, Decl(b.js, 17, 74)) +>exports : Symbol(exports, Decl(b.js, 0, 0)) +>keep : Symbol(keep, Decl(b.js, 17, 74)) +>keep : Symbol(keep, Decl(b.js, 17, 64)) + +=== a.js === +exports.value = 123; +>exports.value : Symbol(value, Decl(a.js, 0, 0)) +>exports : Symbol(exports, Decl(a.js, 0, 0)) +>value : Symbol(value, Decl(a.js, 0, 0)) + +exports.text = "text"; +>exports.text : Symbol(text, Decl(a.js, 0, 20)) +>exports : Symbol(exports, Decl(a.js, 0, 0)) +>text : Symbol(text, Decl(a.js, 0, 20)) + +exports.other = true; +>exports.other : Symbol(other, Decl(a.js, 1, 22)) +>exports : Symbol(exports, Decl(a.js, 0, 0)) +>other : Symbol(other, Decl(a.js, 1, 22)) + +exports.nested = { value: 1 }; +>exports.nested : Symbol(nested, Decl(a.js, 2, 21)) +>exports : Symbol(exports, Decl(a.js, 0, 0)) +>nested : Symbol(nested, Decl(a.js, 2, 21)) +>value : Symbol(value, Decl(a.js, 3, 18)) + +class Obj { +>Obj : Symbol(Obj, Decl(a.js, 3, 30)) + + x = 1; +>x : Symbol(Obj.x, Decl(a.js, 4, 11)) +} +exports.Obj = Obj; +>exports.Obj : Symbol(Obj, Decl(a.js, 6, 1)) +>exports : Symbol(exports, Decl(a.js, 0, 0)) +>Obj : Symbol(Obj, Decl(a.js, 6, 1)) +>Obj : Symbol(Obj, Decl(a.js, 3, 30)) + diff --git a/tsc/testdata/baselines/reference/compiler/declarationEmitCommonjsRequireComputedKey.types b/tsc/testdata/baselines/reference/compiler/declarationEmitCommonjsRequireComputedKey.types new file mode 100644 index 0000000000000..321e53f44a6cf --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitCommonjsRequireComputedKey.types @@ -0,0 +1,165 @@ +//// [tests/cases/compiler/declarationEmitCommonjsRequireComputedKey.ts] //// + +=== b.js === +const key = "value"; +>key : "value" +>"value" : "value" + +const {[key]: dynamic, ["text"]: text, other, nested: {value}, ...rest} = require("./a"), +>key : "value" +>dynamic : any +>"text" : "text" +>text : any +>other : true +>nested : any +>value : number +>rest : any +>require("./a") : typeof sibling +>require : any +>"./a" : "./a" + + sibling = require("./a"); +>sibling : typeof sibling +>require("./a") : typeof sibling +>require : any +>"./a" : "./a" + +exports.dynamic = dynamic; +>exports.dynamic = dynamic : error +>exports.dynamic : error +>exports : typeof import("./b") +>dynamic : any +>dynamic : error + +exports.text = text; +>exports.text = text : error +>exports.text : error +>exports : typeof import("./b") +>text : any +>text : error + +exports.other = other; +>exports.other = other : true +>exports.other : true +>exports : typeof import("./b") +>other : true +>other : true + +exports.value = value; +>exports.value = value : number +>exports.value : number +>exports : typeof import("./b") +>value : number +>value : number + +exports.rest = rest; +>exports.rest = rest : error +>exports.rest : error +>exports : typeof import("./b") +>rest : any +>rest : error + +exports.sibling = sibling; +>exports.sibling = sibling : typeof sibling +>exports.sibling : typeof sibling +>exports : typeof import("./b") +>sibling : typeof sibling +>sibling : typeof sibling + +const {[key]: extra, Obj} = require("./a"); +>key : "value" +>extra : any +>Obj : typeof Obj +>require("./a") : typeof sibling +>require : any +>"./a" : "./a" + +/** @param {Obj} o */ +function useObj(o) {} +>useObj : (o: Obj) => void +>o : Obj + +exports.Obj = Obj; +>exports.Obj = Obj : typeof Obj +>exports.Obj : typeof Obj +>exports : typeof import("./b") +>Obj : typeof Obj +>Obj : typeof Obj + +exports.extra = extra; +>exports.extra = extra : error +>exports.extra : error +>exports : typeof import("./b") +>extra : any +>extra : error + +exports.useObj = useObj; +>exports.useObj = useObj : (o: Obj) => void +>exports.useObj : (o: Obj) => void +>exports : typeof import("./b") +>useObj : (o: Obj) => void +>useObj : (o: Obj) => void + +const {[key]: unusedDynamic, text: unusedText} = require("./a"), keep = 1; +>key : "value" +>unusedDynamic : any +>text : any +>unusedText : "text" +>require("./a") : typeof sibling +>require : any +>"./a" : "./a" +>keep : 1 +>1 : 1 + +exports.keep = keep; +>exports.keep = keep : 1 +>exports.keep : 1 +>exports : typeof import("./b") +>keep : 1 +>keep : 1 + +=== a.js === +exports.value = 123; +>exports.value = 123 : 123 +>exports.value : 123 +>exports : typeof import("./a") +>value : 123 +>123 : 123 + +exports.text = "text"; +>exports.text = "text" : "text" +>exports.text : "text" +>exports : typeof import("./a") +>text : "text" +>"text" : "text" + +exports.other = true; +>exports.other = true : true +>exports.other : true +>exports : typeof import("./a") +>other : true +>true : true + +exports.nested = { value: 1 }; +>exports.nested = { value: 1 } : { value: number; } +>exports.nested : { value: number; } +>exports : typeof import("./a") +>nested : { value: number; } +>{ value: 1 } : { value: number; } +>value : number +>1 : 1 + +class Obj { +>Obj : Obj + + x = 1; +>x : number +>1 : 1 +} +exports.Obj = Obj; +>exports.Obj = Obj : typeof Obj +>exports.Obj : typeof Obj +>exports : typeof import("./a") +>Obj : typeof Obj +>Obj : typeof Obj + diff --git a/tsc/testdata/baselines/reference/compiler/declarationEmitCommonjsRequireNumericKey.js b/tsc/testdata/baselines/reference/compiler/declarationEmitCommonjsRequireNumericKey.js new file mode 100644 index 0000000000000..f5f372ead9ea4 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitCommonjsRequireNumericKey.js @@ -0,0 +1,48 @@ +//// [tests/cases/compiler/declarationEmitCommonjsRequireNumericKey.ts] //// + +//// [a.js] +exports[0] = 123; +exports["a b"] = "text"; +exports.named = true; +exports[1] = { value: 1 }; +exports["-0"] = "negative zero"; + +//// [b.js] +const {0: numeric, "a b": text, named} = require("./a"); +const {[1]: computedNumeric, [`a b`]: computedTemplate} = require("./a"); +exports.numeric = numeric; +exports.text = text; +exports.named = named; +exports.computedNumeric = computedNumeric; +exports.computedTemplate = computedTemplate; + +const {[-0]: signedZero, ["-0"]: negativeZeroString} = require("./a"); +exports.signedZero = signedZero; +exports.negativeZeroString = negativeZeroString; + + + + +//// [a.d.ts] +declare const _exported: 123; +export { _exported as "0" }; +declare const _exported_1: "text"; +export { _exported_1 as "a b" }; +export declare var named: true; +declare const _exported_2: { + value: number; +}; +export { _exported_2 as "1" }; +declare const _exported_3: "negative zero"; +export { _exported_3 as "-0" }; +//// [b.d.ts] +export { numeric }; +export { text }; +export { named }; +export { computedNumeric }; +export { computedTemplate }; +export { signedZero }; +export { negativeZeroString }; +import { "0" as numeric, "a b" as text, named } from "./a"; +import { "1" as computedNumeric, "a b" as computedTemplate } from "./a"; +import { "0" as signedZero, "-0" as negativeZeroString } from "./a"; diff --git a/tsc/testdata/baselines/reference/compiler/declarationEmitCommonjsRequireNumericKey.symbols b/tsc/testdata/baselines/reference/compiler/declarationEmitCommonjsRequireNumericKey.symbols new file mode 100644 index 0000000000000..e481c5650cb24 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitCommonjsRequireNumericKey.symbols @@ -0,0 +1,90 @@ +//// [tests/cases/compiler/declarationEmitCommonjsRequireNumericKey.ts] //// + +=== b.js === +const {0: numeric, "a b": text, named} = require("./a"); +>numeric : Symbol(numeric, Decl(b.js, 0, 7)) +>text : Symbol(text, Decl(b.js, 0, 18)) +>named : Symbol(named, Decl(b.js, 0, 31)) +>require : Symbol(require) +>"./a" : Symbol("./a", Decl(a.js, 0, 0)) + +const {[1]: computedNumeric, [`a b`]: computedTemplate} = require("./a"); +>1 : Symbol(computedNumeric, Decl(b.js, 1, 7)) +>computedNumeric : Symbol(computedNumeric, Decl(b.js, 1, 7)) +>`a b` : Symbol(computedTemplate, Decl(b.js, 1, 28)) +>computedTemplate : Symbol(computedTemplate, Decl(b.js, 1, 28)) +>require : Symbol(require) +>"./a" : Symbol("./a", Decl(a.js, 0, 0)) + +exports.numeric = numeric; +>exports.numeric : Symbol(numeric, Decl(b.js, 1, 73)) +>exports : Symbol(exports, Decl(b.js, 0, 0)) +>numeric : Symbol(numeric, Decl(b.js, 1, 73)) +>numeric : Symbol(numeric, Decl(b.js, 0, 7)) + +exports.text = text; +>exports.text : Symbol(text, Decl(b.js, 2, 26)) +>exports : Symbol(exports, Decl(b.js, 0, 0)) +>text : Symbol(text, Decl(b.js, 2, 26)) +>text : Symbol(text, Decl(b.js, 0, 18)) + +exports.named = named; +>exports.named : Symbol(named, Decl(b.js, 3, 20)) +>exports : Symbol(exports, Decl(b.js, 0, 0)) +>named : Symbol(named, Decl(b.js, 3, 20)) +>named : Symbol(named, Decl(b.js, 0, 31)) + +exports.computedNumeric = computedNumeric; +>exports.computedNumeric : Symbol(computedNumeric, Decl(b.js, 4, 22)) +>exports : Symbol(exports, Decl(b.js, 0, 0)) +>computedNumeric : Symbol(computedNumeric, Decl(b.js, 4, 22)) +>computedNumeric : Symbol(computedNumeric, Decl(b.js, 1, 7)) + +exports.computedTemplate = computedTemplate; +>exports.computedTemplate : Symbol(computedTemplate, Decl(b.js, 5, 42)) +>exports : Symbol(exports, Decl(b.js, 0, 0)) +>computedTemplate : Symbol(computedTemplate, Decl(b.js, 5, 42)) +>computedTemplate : Symbol(computedTemplate, Decl(b.js, 1, 28)) + +const {[-0]: signedZero, ["-0"]: negativeZeroString} = require("./a"); +>signedZero : Symbol(signedZero, Decl(b.js, 8, 7)) +>"-0" : Symbol(negativeZeroString, Decl(b.js, 8, 24)) +>negativeZeroString : Symbol(negativeZeroString, Decl(b.js, 8, 24)) +>require : Symbol(require) +>"./a" : Symbol("./a", Decl(a.js, 0, 0)) + +exports.signedZero = signedZero; +>exports.signedZero : Symbol(signedZero, Decl(b.js, 8, 70)) +>exports : Symbol(exports, Decl(b.js, 0, 0)) +>signedZero : Symbol(signedZero, Decl(b.js, 8, 70)) +>signedZero : Symbol(signedZero, Decl(b.js, 8, 7)) + +exports.negativeZeroString = negativeZeroString; +>exports.negativeZeroString : Symbol(negativeZeroString, Decl(b.js, 9, 32)) +>exports : Symbol(exports, Decl(b.js, 0, 0)) +>negativeZeroString : Symbol(negativeZeroString, Decl(b.js, 9, 32)) +>negativeZeroString : Symbol(negativeZeroString, Decl(b.js, 8, 24)) + +=== a.js === +exports[0] = 123; +>exports : Symbol(exports, Decl(a.js, 0, 0)) +>0 : Symbol(0, Decl(a.js, 0, 0)) + +exports["a b"] = "text"; +>exports : Symbol(exports, Decl(a.js, 0, 0)) +>"a b" : Symbol("a b", Decl(a.js, 0, 17)) + +exports.named = true; +>exports.named : Symbol(named, Decl(a.js, 1, 24)) +>exports : Symbol(exports, Decl(a.js, 0, 0)) +>named : Symbol(named, Decl(a.js, 1, 24)) + +exports[1] = { value: 1 }; +>exports : Symbol(exports, Decl(a.js, 0, 0)) +>1 : Symbol(1, Decl(a.js, 2, 21)) +>value : Symbol(value, Decl(a.js, 3, 14)) + +exports["-0"] = "negative zero"; +>exports : Symbol(exports, Decl(a.js, 0, 0)) +>"-0" : Symbol("-0", Decl(a.js, 3, 26)) + diff --git a/tsc/testdata/baselines/reference/compiler/declarationEmitCommonjsRequireNumericKey.types b/tsc/testdata/baselines/reference/compiler/declarationEmitCommonjsRequireNumericKey.types new file mode 100644 index 0000000000000..6ab1803d3b7ef --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitCommonjsRequireNumericKey.types @@ -0,0 +1,117 @@ +//// [tests/cases/compiler/declarationEmitCommonjsRequireNumericKey.ts] //// + +=== b.js === +const {0: numeric, "a b": text, named} = require("./a"); +>numeric : any +>text : "text" +>named : true +>require("./a") : typeof import("./a") +>require : any +>"./a" : "./a" + +const {[1]: computedNumeric, [`a b`]: computedTemplate} = require("./a"); +>1 : 1 +>computedNumeric : any +>`a b` : "a b" +>computedTemplate : any +>require("./a") : typeof import("./a") +>require : any +>"./a" : "./a" + +exports.numeric = numeric; +>exports.numeric = numeric : error +>exports.numeric : error +>exports : typeof import("./b") +>numeric : any +>numeric : error + +exports.text = text; +>exports.text = text : "text" +>exports.text : "text" +>exports : typeof import("./b") +>text : "text" +>text : "text" + +exports.named = named; +>exports.named = named : true +>exports.named : true +>exports : typeof import("./b") +>named : true +>named : true + +exports.computedNumeric = computedNumeric; +>exports.computedNumeric = computedNumeric : error +>exports.computedNumeric : error +>exports : typeof import("./b") +>computedNumeric : any +>computedNumeric : error + +exports.computedTemplate = computedTemplate; +>exports.computedTemplate = computedTemplate : error +>exports.computedTemplate : error +>exports : typeof import("./b") +>computedTemplate : any +>computedTemplate : error + +const {[-0]: signedZero, ["-0"]: negativeZeroString} = require("./a"); +>-0 : 0 +>0 : 0 +>signedZero : any +>"-0" : "-0" +>negativeZeroString : any +>require("./a") : typeof import("./a") +>require : any +>"./a" : "./a" + +exports.signedZero = signedZero; +>exports.signedZero = signedZero : error +>exports.signedZero : error +>exports : typeof import("./b") +>signedZero : any +>signedZero : error + +exports.negativeZeroString = negativeZeroString; +>exports.negativeZeroString = negativeZeroString : error +>exports.negativeZeroString : error +>exports : typeof import("./b") +>negativeZeroString : any +>negativeZeroString : error + +=== a.js === +exports[0] = 123; +>exports[0] = 123 : 123 +>exports[0] : 123 +>exports : typeof import("./a") +>0 : 0 +>123 : 123 + +exports["a b"] = "text"; +>exports["a b"] = "text" : "text" +>exports["a b"] : "text" +>exports : typeof import("./a") +>"a b" : "a b" +>"text" : "text" + +exports.named = true; +>exports.named = true : true +>exports.named : true +>exports : typeof import("./a") +>named : true +>true : true + +exports[1] = { value: 1 }; +>exports[1] = { value: 1 } : { value: number; } +>exports[1] : { value: number; } +>exports : typeof import("./a") +>1 : 1 +>{ value: 1 } : { value: number; } +>value : number +>1 : 1 + +exports["-0"] = "negative zero"; +>exports["-0"] = "negative zero" : "negative zero" +>exports["-0"] : "negative zero" +>exports : typeof import("./a") +>"-0" : "-0" +>"negative zero" : "negative zero" + diff --git a/tsc/testdata/baselines/reference/compiler/declarationEmitCommonjsTemplateExportName.js b/tsc/testdata/baselines/reference/compiler/declarationEmitCommonjsTemplateExportName.js new file mode 100644 index 0000000000000..ff01d3e167c49 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitCommonjsTemplateExportName.js @@ -0,0 +1,23 @@ +//// [tests/cases/compiler/declarationEmitCommonjsTemplateExportName.ts] //// + +//// [a.js] +const value = 123; +exports[`class`] = value; +exports[`ordinary`] = value; +exports[`default`] = value; +exports[`a b`] = value; +Object.defineProperty(exports, `a b`, { value }); +Object.defineProperty(exports, `other name`, { value }); + + + + +//// [a.d.ts] +export { value as "class" }; +export { value as ordinary }; +export { value as default }; +declare const _exported: number; +export { _exported as "a b" }; +declare const _exported_1: number; +export { _exported_1 as "other name" }; +declare const value = 123; diff --git a/tsc/testdata/baselines/reference/compiler/declarationEmitCommonjsTemplateExportName.symbols b/tsc/testdata/baselines/reference/compiler/declarationEmitCommonjsTemplateExportName.symbols new file mode 100644 index 0000000000000..154bf6ca1b9eb --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitCommonjsTemplateExportName.symbols @@ -0,0 +1,42 @@ +//// [tests/cases/compiler/declarationEmitCommonjsTemplateExportName.ts] //// + +=== a.js === +const value = 123; +>value : Symbol(value, Decl(a.js, 0, 5)) + +exports[`class`] = value; +>exports : Symbol(exports, Decl(a.js, 0, 0)) +>`class` : Symbol(`class`, Decl(a.js, 0, 18)) +>value : Symbol(value, Decl(a.js, 0, 5)) + +exports[`ordinary`] = value; +>exports : Symbol(exports, Decl(a.js, 0, 0)) +>`ordinary` : Symbol(`ordinary`, Decl(a.js, 1, 25)) +>value : Symbol(value, Decl(a.js, 0, 5)) + +exports[`default`] = value; +>exports : Symbol(exports, Decl(a.js, 0, 0)) +>`default` : Symbol(`default`, Decl(a.js, 2, 28)) +>value : Symbol(value, Decl(a.js, 0, 5)) + +exports[`a b`] = value; +>exports : Symbol(exports, Decl(a.js, 0, 0)) +>`a b` : Symbol(`a b`, Decl(a.js, 3, 27), Decl(a.js, 4, 23)) +>value : Symbol(value, Decl(a.js, 0, 5)) + +Object.defineProperty(exports, `a b`, { value }); +>Object.defineProperty : Symbol(ObjectConstructor.defineProperty, Decl(lib.es5.d.ts, --, --)) +>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) +>defineProperty : Symbol(ObjectConstructor.defineProperty, Decl(lib.es5.d.ts, --, --)) +>exports : Symbol(exports, Decl(a.js, 0, 0)) +>`a b` : Symbol(`a b`, Decl(a.js, 3, 27), Decl(a.js, 4, 23)) +>value : Symbol(value, Decl(a.js, 5, 39)) + +Object.defineProperty(exports, `other name`, { value }); +>Object.defineProperty : Symbol(ObjectConstructor.defineProperty, Decl(lib.es5.d.ts, --, --)) +>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) +>defineProperty : Symbol(ObjectConstructor.defineProperty, Decl(lib.es5.d.ts, --, --)) +>exports : Symbol(exports, Decl(a.js, 0, 0)) +>`other name` : Symbol(`other name`, Decl(a.js, 5, 49)) +>value : Symbol(value, Decl(a.js, 6, 46)) + diff --git a/tsc/testdata/baselines/reference/compiler/declarationEmitCommonjsTemplateExportName.types b/tsc/testdata/baselines/reference/compiler/declarationEmitCommonjsTemplateExportName.types new file mode 100644 index 0000000000000..617855037ac61 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitCommonjsTemplateExportName.types @@ -0,0 +1,55 @@ +//// [tests/cases/compiler/declarationEmitCommonjsTemplateExportName.ts] //// + +=== a.js === +const value = 123; +>value : 123 +>123 : 123 + +exports[`class`] = value; +>exports[`class`] = value : 123 +>exports[`class`] : 123 +>exports : typeof import("./a") +>`class` : "class" +>value : 123 + +exports[`ordinary`] = value; +>exports[`ordinary`] = value : 123 +>exports[`ordinary`] : 123 +>exports : typeof import("./a") +>`ordinary` : "ordinary" +>value : 123 + +exports[`default`] = value; +>exports[`default`] = value : 123 +>exports[`default`] : 123 +>exports : typeof import("./a") +>`default` : "default" +>value : 123 + +exports[`a b`] = value; +>exports[`a b`] = value : 123 +>exports[`a b`] : number +>exports : typeof import("./a") +>`a b` : "a b" +>value : 123 + +Object.defineProperty(exports, `a b`, { value }); +>Object.defineProperty(exports, `a b`, { value }) : typeof import("./a") +>Object.defineProperty : (o: T, p: PropertyKey, attributes: PropertyDescriptor & ThisType) => T +>Object : ObjectConstructor +>defineProperty : (o: T, p: PropertyKey, attributes: PropertyDescriptor & ThisType) => T +>exports : typeof import("./a") +>`a b` : "a b" +>{ value } : { value: number; } +>value : number + +Object.defineProperty(exports, `other name`, { value }); +>Object.defineProperty(exports, `other name`, { value }) : typeof import("./a") +>Object.defineProperty : (o: T, p: PropertyKey, attributes: PropertyDescriptor & ThisType) => T +>Object : ObjectConstructor +>defineProperty : (o: T, p: PropertyKey, attributes: PropertyDescriptor & ThisType) => T +>exports : typeof import("./a") +>`other name` : "other name" +>{ value } : { value: number; } +>value : number + diff --git a/tsc/testdata/tests/cases/compiler/declarationEmitCommonjsRequireComputedKey.ts b/tsc/testdata/tests/cases/compiler/declarationEmitCommonjsRequireComputedKey.ts new file mode 100644 index 0000000000000..edd6c3549ae96 --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/declarationEmitCommonjsRequireComputedKey.ts @@ -0,0 +1,35 @@ +// @allowJs: true +// @declaration: true +// @emitDeclarationOnly: true +// @outDir: out + +// @filename: a.js +exports.value = 123; +exports.text = "text"; +exports.other = true; +exports.nested = { value: 1 }; +class Obj { + x = 1; +} +exports.Obj = Obj; + +// @filename: b.js +const key = "value"; +const {[key]: dynamic, ["text"]: text, other, nested: {value}, ...rest} = require("./a"), + sibling = require("./a"); +exports.dynamic = dynamic; +exports.text = text; +exports.other = other; +exports.value = value; +exports.rest = rest; +exports.sibling = sibling; + +const {[key]: extra, Obj} = require("./a"); +/** @param {Obj} o */ +function useObj(o) {} +exports.Obj = Obj; +exports.extra = extra; +exports.useObj = useObj; + +const {[key]: unusedDynamic, text: unusedText} = require("./a"), keep = 1; +exports.keep = keep; diff --git a/tsc/testdata/tests/cases/compiler/declarationEmitCommonjsRequireNumericKey.ts b/tsc/testdata/tests/cases/compiler/declarationEmitCommonjsRequireNumericKey.ts new file mode 100644 index 0000000000000..6c6f3be17d092 --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/declarationEmitCommonjsRequireNumericKey.ts @@ -0,0 +1,24 @@ +// @allowJs: true +// @declaration: true +// @emitDeclarationOnly: true +// @outDir: out + +// @filename: a.js +exports[0] = 123; +exports["a b"] = "text"; +exports.named = true; +exports[1] = { value: 1 }; +exports["-0"] = "negative zero"; + +// @filename: b.js +const {0: numeric, "a b": text, named} = require("./a"); +const {[1]: computedNumeric, [`a b`]: computedTemplate} = require("./a"); +exports.numeric = numeric; +exports.text = text; +exports.named = named; +exports.computedNumeric = computedNumeric; +exports.computedTemplate = computedTemplate; + +const {[-0]: signedZero, ["-0"]: negativeZeroString} = require("./a"); +exports.signedZero = signedZero; +exports.negativeZeroString = negativeZeroString; diff --git a/tsc/testdata/tests/cases/compiler/declarationEmitCommonjsTemplateExportName.ts b/tsc/testdata/tests/cases/compiler/declarationEmitCommonjsTemplateExportName.ts new file mode 100644 index 0000000000000..88e05ac53f6ba --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/declarationEmitCommonjsTemplateExportName.ts @@ -0,0 +1,13 @@ +// @allowJs: true +// @declaration: true +// @emitDeclarationOnly: true +// @outDir: out + +// @filename: a.js +const value = 123; +exports[`class`] = value; +exports[`ordinary`] = value; +exports[`default`] = value; +exports[`a b`] = value; +Object.defineProperty(exports, `a b`, { value }); +Object.defineProperty(exports, `other name`, { value });