diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 4e1874f653eba..b187037abcb56 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -1939,7 +1939,9 @@ namespace ts { export function isRequireVariableDeclaration(node: Node, requireStringLiteralLikeArgument: true): node is RequireVariableDeclaration; export function isRequireVariableDeclaration(node: Node, requireStringLiteralLikeArgument: boolean): node is VariableDeclaration; export function isRequireVariableDeclaration(node: Node, requireStringLiteralLikeArgument: boolean): node is VariableDeclaration { - node = getRootDeclaration(node); + if (node.kind === SyntaxKind.BindingElement) { + node = node.parent.parent; + } return isVariableDeclaration(node) && !!node.initializer && isRequireCall(getLeftmostAccessExpression(node.initializer), requireStringLiteralLikeArgument); } diff --git a/tests/baselines/reference/nestedDestructuringOfRequire.errors.txt b/tests/baselines/reference/nestedDestructuringOfRequire.errors.txt new file mode 100644 index 0000000000000..9a7625e35a802 --- /dev/null +++ b/tests/baselines/reference/nestedDestructuringOfRequire.errors.txt @@ -0,0 +1,18 @@ +tests/cases/conformance/salsa/main.js(5,1): error TS2304: Cannot find name 'chalk'. + + +==== tests/cases/conformance/salsa/main.js (1 errors) ==== + const { + chalk: { grey } + } = require('./mod1'); + grey + chalk + ~~~~~ +!!! error TS2304: Cannot find name 'chalk'. + +==== tests/cases/conformance/salsa/mod1.js (0 errors) ==== + const chalk = { + grey: {} + }; + module.exports.chalk = chalk + \ No newline at end of file diff --git a/tests/baselines/reference/nestedDestructuringOfRequire.js b/tests/baselines/reference/nestedDestructuringOfRequire.js new file mode 100644 index 0000000000000..516921273e150 --- /dev/null +++ b/tests/baselines/reference/nestedDestructuringOfRequire.js @@ -0,0 +1,33 @@ +//// [tests/cases/conformance/salsa/nestedDestructuringOfRequire.ts] //// + +//// [mod1.js] +const chalk = { + grey: {} +}; +module.exports.chalk = chalk + +//// [main.js] +const { + chalk: { grey } +} = require('./mod1'); +grey +chalk + + +//// [mod1.js] +var chalk = { + grey: {} +}; +module.exports.chalk = chalk; +//// [main.js] +var grey = require('./mod1').chalk.grey; +grey; +chalk; + + +//// [mod1.d.ts] +export namespace chalk { + const grey: {}; +} +//// [main.d.ts] +export {}; diff --git a/tests/baselines/reference/nestedDestructuringOfRequire.symbols b/tests/baselines/reference/nestedDestructuringOfRequire.symbols new file mode 100644 index 0000000000000..88261e51d1e1f --- /dev/null +++ b/tests/baselines/reference/nestedDestructuringOfRequire.symbols @@ -0,0 +1,31 @@ +=== tests/cases/conformance/salsa/main.js === +const { + chalk: { grey } +>chalk : Symbol(chalk, Decl(mod1.js, 2, 2)) +>grey : Symbol(grey, Decl(main.js, 1, 12)) + +} = require('./mod1'); +>require : Symbol(require) +>'./mod1' : Symbol("tests/cases/conformance/salsa/mod1", Decl(mod1.js, 0, 0)) + +grey +>grey : Symbol(grey, Decl(main.js, 1, 12)) + +chalk + +=== tests/cases/conformance/salsa/mod1.js === +const chalk = { +>chalk : Symbol(chalk, Decl(mod1.js, 0, 5)) + + grey: {} +>grey : Symbol(grey, Decl(mod1.js, 0, 15)) + +}; +module.exports.chalk = chalk +>module.exports.chalk : Symbol(chalk, Decl(mod1.js, 2, 2)) +>module.exports : Symbol(chalk, Decl(mod1.js, 2, 2)) +>module : Symbol(module, Decl(mod1.js, 2, 2)) +>exports : Symbol("tests/cases/conformance/salsa/mod1", Decl(mod1.js, 0, 0)) +>chalk : Symbol(chalk, Decl(mod1.js, 2, 2)) +>chalk : Symbol(chalk, Decl(mod1.js, 0, 5)) + diff --git a/tests/baselines/reference/nestedDestructuringOfRequire.types b/tests/baselines/reference/nestedDestructuringOfRequire.types new file mode 100644 index 0000000000000..fd97ba75a6591 --- /dev/null +++ b/tests/baselines/reference/nestedDestructuringOfRequire.types @@ -0,0 +1,36 @@ +=== tests/cases/conformance/salsa/main.js === +const { + chalk: { grey } +>chalk : any +>grey : {} + +} = require('./mod1'); +>require('./mod1') : typeof import("tests/cases/conformance/salsa/mod1") +>require : any +>'./mod1' : "./mod1" + +grey +>grey : {} + +chalk +>chalk : any + +=== tests/cases/conformance/salsa/mod1.js === +const chalk = { +>chalk : { grey: {}; } +>{ grey: {}} : { grey: {}; } + + grey: {} +>grey : {} +>{} : {} + +}; +module.exports.chalk = chalk +>module.exports.chalk = chalk : { grey: {}; } +>module.exports.chalk : { grey: {}; } +>module.exports : typeof import("tests/cases/conformance/salsa/mod1") +>module : { "\"tests/cases/conformance/salsa/mod1\"": typeof import("tests/cases/conformance/salsa/mod1"); } +>exports : typeof import("tests/cases/conformance/salsa/mod1") +>chalk : { grey: {}; } +>chalk : { grey: {}; } + diff --git a/tests/cases/conformance/salsa/nestedDestructuringOfRequire.ts b/tests/cases/conformance/salsa/nestedDestructuringOfRequire.ts new file mode 100644 index 0000000000000..bf6b3a4858809 --- /dev/null +++ b/tests/cases/conformance/salsa/nestedDestructuringOfRequire.ts @@ -0,0 +1,17 @@ +// @allowJs: true +// @checkJs: true +// @outDir: out +// @declaration: true + +// @filename: mod1.js +const chalk = { + grey: {} +}; +module.exports.chalk = chalk + +// @filename: main.js +const { + chalk: { grey } +} = require('./mod1'); +grey +chalk