Skip to content
Open
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
10 changes: 8 additions & 2 deletions tsc/internal/transformers/declarations/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -887,18 +887,24 @@ 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
}
}
return true
}

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))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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;



Expand All @@ -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;
}
Expand All @@ -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;
Expand All @@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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))

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"

Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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;