diff --git a/tsc/internal/transformers/declarations/transform.go b/tsc/internal/transformers/declarations/transform.go index 9538225ed249f..441ba7949a875 100644 --- a/tsc/internal/transformers/declarations/transform.go +++ b/tsc/internal/transformers/declarations/transform.go @@ -887,11 +887,14 @@ func hasAnyBindingInitializers(bindingPattern *ast.BindingPattern) bool { func canTransformCjsRequireToImport(decl *ast.Node) bool { name := decl.Name() - if !ast.IsObjectBindingPattern(name) { + if ast.IsIdentifier(name) { return true } + if !ast.IsObjectBindingPattern(name) { + return false + } for _, elem := range name.AsBindingPattern().Elements.Nodes { - if propertyName := elem.PropertyName(); propertyName != nil && ast.IsDynamicName(propertyName) { + if !isCjsRequireImportableBinding(elem) { return false } } @@ -899,6 +902,9 @@ func canTransformCjsRequireToImport(decl *ast.Node) bool { } func isCjsRequireImportableBinding(elem *ast.Node) bool { + if !ast.IsBindingElement(elem) || elem.Parent == nil || !ast.IsObjectBindingPattern(elem.Parent) { + return false + } propertyName := elem.PropertyName() return ast.IsIdentifier(elem.Name()) && elem.AsBindingElement().DotDotDotToken == nil && (propertyName == nil || !ast.IsDynamicName(propertyName)) } diff --git a/tsc/testdata/baselines/reference/compiler/declarationEmitCommonjsRequireComputedKey.js b/tsc/testdata/baselines/reference/compiler/declarationEmitCommonjsRequireComputedKey.js index 7a3ebaedbae12..3788683471a23 100644 --- a/tsc/testdata/baselines/reference/compiler/declarationEmitCommonjsRequireComputedKey.js +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitCommonjsRequireComputedKey.js @@ -9,6 +9,9 @@ class Obj { x = 1; } exports.Obj = Obj; +exports.b = { c: "hello" }; +exports.arr = [1]; +exports.a = "a"; //// [b.js] const key = "value"; @@ -30,6 +33,19 @@ exports.useObj = useObj; const {[key]: unusedDynamic, text: unusedText} = require("./a"), keep = 1; exports.keep = keep; + +// Case 1: Nested object destructuring +const { b: { c } } = require("./a"); +exports.c = c; + +// Case 2: Nested array destructuring +const { arr: [first] } = require("./a"); +exports.first = first; + +// Case 3: Mixed destructuring +const { a, b: { c: c2 } } = require("./a"); +exports.a = a; +exports.c2 = c2; @@ -42,6 +58,11 @@ export declare var nested: { value: number; }; export { Obj }; +export declare var b: { + c: string; +}; +export declare var arr: number[]; +export declare var a: "a"; declare class Obj { x: number; } @@ -56,6 +77,10 @@ export { Obj }; export { extra }; export { useObj }; export { keep }; +export { c }; +export { first }; +export { a }; +export { c2 }; import sibling = require("./a"); import { text as text, other } from "./a"; declare const dynamic: any, value: number, rest: any; @@ -64,3 +89,7 @@ declare const extra: any; /** @param {Obj} o */ declare function useObj(o: Obj): void; declare const keep = 1; +declare const c: string; +declare const first: number; +import { a } from "./a"; +declare const c2: string; diff --git a/tsc/testdata/baselines/reference/compiler/declarationEmitCommonjsRequireComputedKey.symbols b/tsc/testdata/baselines/reference/compiler/declarationEmitCommonjsRequireComputedKey.symbols index 76370666c6841..15ce97e29bb9d 100644 --- a/tsc/testdata/baselines/reference/compiler/declarationEmitCommonjsRequireComputedKey.symbols +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitCommonjsRequireComputedKey.symbols @@ -102,6 +102,53 @@ exports.keep = keep; >keep : Symbol(keep, Decl(b.js, 17, 74)) >keep : Symbol(keep, Decl(b.js, 17, 64)) +// Case 1: Nested object destructuring +const { b: { c } } = require("./a"); +>b : Symbol(sibling.b, Decl(a.js, 7, 18)) +>c : Symbol(c, Decl(b.js, 21, 12)) +>require : Symbol(require) +>"./a" : Symbol(sibling, Decl(a.js, 0, 0)) + +exports.c = c; +>exports.c : Symbol(c, Decl(b.js, 21, 36)) +>exports : Symbol(exports, Decl(b.js, 0, 0)) +>c : Symbol(c, Decl(b.js, 21, 36)) +>c : Symbol(c, Decl(b.js, 21, 12)) + +// Case 2: Nested array destructuring +const { arr: [first] } = require("./a"); +>arr : Symbol(sibling.arr, Decl(a.js, 8, 27)) +>first : Symbol(first, Decl(b.js, 25, 14)) +>require : Symbol(require) +>"./a" : Symbol(sibling, Decl(a.js, 0, 0)) + +exports.first = first; +>exports.first : Symbol(first, Decl(b.js, 25, 40)) +>exports : Symbol(exports, Decl(b.js, 0, 0)) +>first : Symbol(first, Decl(b.js, 25, 40)) +>first : Symbol(first, Decl(b.js, 25, 14)) + +// Case 3: Mixed destructuring +const { a, b: { c: c2 } } = require("./a"); +>a : Symbol(a, Decl(b.js, 29, 7)) +>b : Symbol(sibling.b, Decl(a.js, 7, 18)) +>c : Symbol(c, Decl(a.js, 8, 13)) +>c2 : Symbol(c2, Decl(b.js, 29, 15)) +>require : Symbol(require) +>"./a" : Symbol(sibling, Decl(a.js, 0, 0)) + +exports.a = a; +>exports.a : Symbol(a, Decl(b.js, 29, 43)) +>exports : Symbol(exports, Decl(b.js, 0, 0)) +>a : Symbol(a, Decl(b.js, 29, 43)) +>a : Symbol(a, Decl(b.js, 29, 7)) + +exports.c2 = c2; +>exports.c2 : Symbol(c2, Decl(b.js, 30, 14)) +>exports : Symbol(exports, Decl(b.js, 0, 0)) +>c2 : Symbol(c2, Decl(b.js, 30, 14)) +>c2 : Symbol(c2, Decl(b.js, 29, 15)) + === a.js === exports.value = 123; >exports.value : Symbol(value, Decl(a.js, 0, 0)) @@ -136,3 +183,19 @@ exports.Obj = Obj; >Obj : Symbol(Obj, Decl(a.js, 6, 1)) >Obj : Symbol(Obj, Decl(a.js, 3, 30)) +exports.b = { c: "hello" }; +>exports.b : Symbol(b, Decl(a.js, 7, 18)) +>exports : Symbol(exports, Decl(a.js, 0, 0)) +>b : Symbol(b, Decl(a.js, 7, 18)) +>c : Symbol(c, Decl(a.js, 8, 13)) + +exports.arr = [1]; +>exports.arr : Symbol(arr, Decl(a.js, 8, 27)) +>exports : Symbol(exports, Decl(a.js, 0, 0)) +>arr : Symbol(arr, Decl(a.js, 8, 27)) + +exports.a = "a"; +>exports.a : Symbol(a, Decl(a.js, 9, 18)) +>exports : Symbol(exports, Decl(a.js, 0, 0)) +>a : Symbol(a, Decl(a.js, 9, 18)) + diff --git a/tsc/testdata/baselines/reference/compiler/declarationEmitCommonjsRequireComputedKey.types b/tsc/testdata/baselines/reference/compiler/declarationEmitCommonjsRequireComputedKey.types index 321e53f44a6cf..2306a73b0caa7 100644 --- a/tsc/testdata/baselines/reference/compiler/declarationEmitCommonjsRequireComputedKey.types +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitCommonjsRequireComputedKey.types @@ -118,6 +118,60 @@ exports.keep = keep; >keep : 1 >keep : 1 +// Case 1: Nested object destructuring +const { b: { c } } = require("./a"); +>b : any +>c : string +>require("./a") : typeof sibling +>require : any +>"./a" : "./a" + +exports.c = c; +>exports.c = c : string +>exports.c : string +>exports : typeof import("./b") +>c : string +>c : string + +// Case 2: Nested array destructuring +const { arr: [first] } = require("./a"); +>arr : any +>first : number +>require("./a") : typeof sibling +>require : any +>"./a" : "./a" + +exports.first = first; +>exports.first = first : number +>exports.first : number +>exports : typeof import("./b") +>first : number +>first : number + +// Case 3: Mixed destructuring +const { a, b: { c: c2 } } = require("./a"); +>a : "a" +>b : any +>c : any +>c2 : string +>require("./a") : typeof sibling +>require : any +>"./a" : "./a" + +exports.a = a; +>exports.a = a : "a" +>exports.a : "a" +>exports : typeof import("./b") +>a : "a" +>a : "a" + +exports.c2 = c2; +>exports.c2 = c2 : string +>exports.c2 : string +>exports : typeof import("./b") +>c2 : string +>c2 : string + === a.js === exports.value = 123; >exports.value = 123 : 123 @@ -163,3 +217,27 @@ exports.Obj = Obj; >Obj : typeof Obj >Obj : typeof Obj +exports.b = { c: "hello" }; +>exports.b = { c: "hello" } : { c: string; } +>exports.b : { c: string; } +>exports : typeof import("./a") +>b : { c: string; } +>{ c: "hello" } : { c: string; } +>c : string +>"hello" : "hello" + +exports.arr = [1]; +>exports.arr = [1] : number[] +>exports.arr : number[] +>exports : typeof import("./a") +>arr : number[] +>[1] : number[] +>1 : 1 + +exports.a = "a"; +>exports.a = "a" : "a" +>exports.a : "a" +>exports : typeof import("./a") +>a : "a" +>"a" : "a" + diff --git a/tsc/testdata/tests/cases/compiler/declarationEmitCommonjsRequireComputedKey.ts b/tsc/testdata/tests/cases/compiler/declarationEmitCommonjsRequireComputedKey.ts index edd6c3549ae96..82c63fd177be4 100644 --- a/tsc/testdata/tests/cases/compiler/declarationEmitCommonjsRequireComputedKey.ts +++ b/tsc/testdata/tests/cases/compiler/declarationEmitCommonjsRequireComputedKey.ts @@ -12,6 +12,9 @@ class Obj { x = 1; } exports.Obj = Obj; +exports.b = { c: "hello" }; +exports.arr = [1]; +exports.a = "a"; // @filename: b.js const key = "value"; @@ -33,3 +36,16 @@ exports.useObj = useObj; const {[key]: unusedDynamic, text: unusedText} = require("./a"), keep = 1; exports.keep = keep; + +// Case 1: Nested object destructuring +const { b: { c } } = require("./a"); +exports.c = c; + +// Case 2: Nested array destructuring +const { arr: [first] } = require("./a"); +exports.first = first; + +// Case 3: Mixed destructuring +const { a, b: { c: c2 } } = require("./a"); +exports.a = a; +exports.c2 = c2;