Skip to content

Commit 55ca5e9

Browse files
authored
Fixes crash on chained property access on require (microsoft#40135)
From the user tests: ```js const x = require('y').z.ka ``` would cause the crash because I forgot to call getLeftMmostPropertyAccessExpression in one place. Note that this doesn't fix the alias, it just stops the crash.
1 parent 44d2350 commit 55ca5e9

File tree

5 files changed

+104
-1
lines changed

5 files changed

+104
-1
lines changed

Diff for: src/compiler/checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2394,7 +2394,7 @@ namespace ts {
23942394

23952395
function getTargetOfImportEqualsDeclaration(node: ImportEqualsDeclaration | VariableDeclaration, dontResolveAlias: boolean): Symbol | undefined {
23962396
if (isVariableDeclaration(node) && node.initializer && isPropertyAccessExpression(node.initializer)) {
2397-
const name = (node.initializer.expression as CallExpression).arguments[0] as StringLiteral;
2397+
const name = (getLeftmostPropertyAccessExpression(node.initializer.expression) as CallExpression).arguments[0] as StringLiteral;
23982398
return isIdentifier(node.initializer.name)
23992399
? getPropertyOfType(resolveExternalModuleTypeByLiteral(name), node.initializer.name.escapedText)
24002400
: undefined;
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//// [tests/cases/conformance/salsa/requireTwoPropertyAccesses.ts] ////
2+
3+
//// [mod.js]
4+
// @declaration
5+
module.exports = {
6+
x: {
7+
y: "value"
8+
}
9+
}
10+
//// [requireTwoPropertyAccesses.js]
11+
const value = require("./mod").x.y
12+
console.log(value)
13+
14+
15+
//// [mod.js]
16+
// @declaration
17+
module.exports = {
18+
x: {
19+
y: "value"
20+
}
21+
};
22+
//// [requireTwoPropertyAccesses.js]
23+
var value = require("./mod").x.y;
24+
console.log(value);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
=== tests/cases/conformance/salsa/requireTwoPropertyAccesses.js ===
2+
const value = require("./mod").x.y
3+
>value : Symbol(value, Decl(requireTwoPropertyAccesses.js, 0, 5))
4+
>require("./mod").x.y : Symbol(y, Decl(mod.js, 2, 8))
5+
>require("./mod").x : Symbol(x, Decl(mod.js, 1, 18))
6+
>require : Symbol(require)
7+
>"./mod" : Symbol("tests/cases/conformance/salsa/mod", Decl(mod.js, 0, 0))
8+
>x : Symbol(x, Decl(mod.js, 1, 18))
9+
>y : Symbol(y, Decl(mod.js, 2, 8))
10+
11+
console.log(value)
12+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
13+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
14+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
15+
>value : Symbol(value, Decl(requireTwoPropertyAccesses.js, 0, 5))
16+
17+
=== tests/cases/conformance/salsa/mod.js ===
18+
// @declaration
19+
module.exports = {
20+
>module.exports : Symbol("tests/cases/conformance/salsa/mod", Decl(mod.js, 0, 0))
21+
>module : Symbol(export=, Decl(mod.js, 0, 0))
22+
>exports : Symbol(export=, Decl(mod.js, 0, 0))
23+
24+
x: {
25+
>x : Symbol(x, Decl(mod.js, 1, 18))
26+
27+
y: "value"
28+
>y : Symbol(y, Decl(mod.js, 2, 8))
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
=== tests/cases/conformance/salsa/requireTwoPropertyAccesses.js ===
2+
const value = require("./mod").x.y
3+
>value : error
4+
>require("./mod").x.y : string
5+
>require("./mod").x : { y: string; }
6+
>require("./mod") : { x: { y: string; }; }
7+
>require : any
8+
>"./mod" : "./mod"
9+
>x : { y: string; }
10+
>y : string
11+
12+
console.log(value)
13+
>console.log(value) : void
14+
>console.log : (...data: any[]) => void
15+
>console : Console
16+
>log : (...data: any[]) => void
17+
>value : error
18+
19+
=== tests/cases/conformance/salsa/mod.js ===
20+
// @declaration
21+
module.exports = {
22+
>module.exports = { x: { y: "value" }} : { x: { y: string; }; }
23+
>module.exports : { x: { y: string; }; }
24+
>module : { "\"tests/cases/conformance/salsa/mod\"": { x: { y: string; }; }; }
25+
>exports : { x: { y: string; }; }
26+
>{ x: { y: "value" }} : { x: { y: string; }; }
27+
28+
x: {
29+
>x : { y: string; }
30+
>{ y: "value" } : { y: string; }
31+
32+
y: "value"
33+
>y : string
34+
>"value" : "value"
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// @declaration
2+
// @outdir: out
3+
// @checkjs: true
4+
// @allowjs: true
5+
// @filename: mod.js
6+
module.exports = {
7+
x: {
8+
y: "value"
9+
}
10+
}
11+
// @filename: requireTwoPropertyAccesses.js
12+
const value = require("./mod").x.y
13+
console.log(value)

0 commit comments

Comments
 (0)