Skip to content

Commit 68021c1

Browse files
committed
WIP: get rid of caching of resolved symbol, add transform tests
1 parent 1238db9 commit 68021c1

File tree

5 files changed

+251
-6
lines changed

5 files changed

+251
-6
lines changed

src/compiler/checker.ts

+38-6
Original file line numberDiff line numberDiff line change
@@ -43070,10 +43070,10 @@ namespace ts {
4307043070
}
4307143071
const node = getParseTreeNode(nodeIn, isIdentifier);
4307243072
if (node) {
43073-
const symbol = getReferencedValueSymbol(node);
43073+
const symbol = getReferencedValueSymbol(node); // >> Change it here
4307443074
// We should only get the declaration of an alias if there isn't a local value
4307543075
// declaration for the symbol
43076-
if (isNonLocalAlias(symbol, /*excludes*/ SymbolFlags.Value) && !getTypeOnlyAliasDeclaration(symbol)) {
43076+
if (isNonLocalAlias(symbol, /*excludes*/ SymbolFlags.Value) && !getTypeOnlyAliasDeclaration(symbol)) { // >> Probably use the default excludes now
4307743077
return getDeclarationOfAliasSymbol(symbol);
4307843078
}
4307943079
}
@@ -43456,10 +43456,10 @@ namespace ts {
4345643456
}
4345743457

4345843458
function getReferencedValueSymbol(reference: Identifier, startInDeclarationContainer?: boolean): Symbol | undefined {
43459-
const resolvedSymbol = getNodeLinks(reference).resolvedSymbol;
43460-
if (resolvedSymbol) {
43461-
return resolvedSymbol;
43462-
}
43459+
// const resolvedSymbol = getNodeLinks(reference).resolvedSymbol;
43460+
// if (resolvedSymbol) {
43461+
// return resolvedSymbol;
43462+
// }
4346343463

4346443464
let location: Node = reference;
4346543465
if (startInDeclarationContainer) {
@@ -43474,6 +43474,38 @@ namespace ts {
4347443474
return resolveName(location, reference.escapedText, SymbolFlags.Value | SymbolFlags.ExportValue | SymbolFlags.Alias, /*nodeNotFoundMessage*/ undefined, /*nameArg*/ undefined, /*isUse*/ true);
4347543475
}
4347643476

43477+
// function getReferencedAnySymbolOld(reference: Identifier, startInDeclarationContainer?: boolean): Symbol | undefined {
43478+
// // >> Can't use cached resolved symbol because it might be `unknown` symbol
43479+
// // const resolvedSymbol = getNodeLinks(reference).resolvedSymbol;
43480+
// // if (resolvedSymbol) {
43481+
// // return resolvedSymbol;
43482+
// // }
43483+
43484+
// let location: Node = reference;
43485+
// if (startInDeclarationContainer) {
43486+
// // When resolving the name of a declaration as a value, we need to start resolution
43487+
// // at a point outside of the declaration.
43488+
// const parent = reference.parent;
43489+
// if (isDeclaration(parent) && reference === parent.name) {
43490+
// location = getDeclarationContainer(parent);
43491+
// }
43492+
// }
43493+
43494+
// // return resolveName(location, reference.escapedText, SymbolFlags.Value | SymbolFlags.ExportValue | SymbolFlags.Alias, /*nodeNotFoundMessage*/ undefined, /*nameArg*/ undefined, /*isUse*/ true);
43495+
// return resolveNameHelper(
43496+
// location,
43497+
// reference.escapedText,
43498+
// SymbolFlags.Value | SymbolFlags.ExportValue | SymbolFlags.Alias,
43499+
// /*nameNotFoundMessage*/ undefined,
43500+
// /*nameArg*/ undefined,
43501+
// /*isUse*/ true,
43502+
// /*excludeGlobals*/ false,
43503+
// /*getSpellingSuggestions*/ true,
43504+
// (symbol, name, _meaning) => getSymbol(symbol, name, SymbolFlags.All),
43505+
// // /*reportErrors*/ false,
43506+
// );
43507+
// }
43508+
4347743509
function getReferencedValueDeclaration(referenceIn: Identifier): Declaration | undefined {
4347843510
if (!isGeneratedIdentifier(referenceIn)) {
4347943511
const reference = getParseTreeNode(referenceIn, isIdentifier);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//// [tests/cases/compiler/elidedJSImport2.ts] ////
2+
3+
//// [index.js]
4+
import { Foo } from "./other.js";
5+
import * as other from "./other.js";
6+
import defaultFoo from "./other.js";
7+
8+
const x = new Foo();
9+
const y = other.Foo();
10+
const z = new defaultFoo();
11+
12+
//// [other.d.ts]
13+
export interface Foo {
14+
bar: number;
15+
}
16+
17+
export default interface Bar {
18+
foo: number;
19+
}
20+
21+
//// [other.js]
22+
export class Foo {
23+
bar = 2.4;
24+
}
25+
26+
export default class Bar {
27+
foo = 1.2;
28+
}
29+
30+
31+
//// [index.js]
32+
"use strict";
33+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
34+
if (k2 === undefined) k2 = k;
35+
var desc = Object.getOwnPropertyDescriptor(m, k);
36+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
37+
desc = { enumerable: true, get: function() { return m[k]; } };
38+
}
39+
Object.defineProperty(o, k2, desc);
40+
}) : (function(o, m, k, k2) {
41+
if (k2 === undefined) k2 = k;
42+
o[k2] = m[k];
43+
}));
44+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
45+
Object.defineProperty(o, "default", { enumerable: true, value: v });
46+
}) : function(o, v) {
47+
o["default"] = v;
48+
});
49+
var __importStar = (this && this.__importStar) || function (mod) {
50+
if (mod && mod.__esModule) return mod;
51+
var result = {};
52+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
53+
__setModuleDefault(result, mod);
54+
return result;
55+
};
56+
var __importDefault = (this && this.__importDefault) || function (mod) {
57+
return (mod && mod.__esModule) ? mod : { "default": mod };
58+
};
59+
Object.defineProperty(exports, "__esModule", { value: true });
60+
const other_js_1 = require("./other.js");
61+
const other = __importStar(require("./other.js"));
62+
const other_js_2 = __importDefault(require("./other.js"));
63+
const x = new other_js_1.Foo();
64+
const y = other.Foo();
65+
const z = new other_js_2.default();
66+
//// [other.js]
67+
"use strict";
68+
Object.defineProperty(exports, "__esModule", { value: true });
69+
exports.Foo = void 0;
70+
class Foo {
71+
bar = 2.4;
72+
}
73+
exports.Foo = Foo;
74+
class Bar {
75+
foo = 1.2;
76+
}
77+
exports.default = Bar;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
=== tests/cases/compiler/index.js ===
2+
import { Foo } from "./other.js";
3+
>Foo : Symbol(Foo, Decl(index.js, 0, 8))
4+
5+
import * as other from "./other.js";
6+
>other : Symbol(other, Decl(index.js, 1, 6))
7+
8+
import defaultFoo from "./other.js";
9+
>defaultFoo : Symbol(defaultFoo, Decl(index.js, 2, 6))
10+
11+
const x = new Foo();
12+
>x : Symbol(x, Decl(index.js, 4, 5))
13+
14+
const y = other.Foo();
15+
>y : Symbol(y, Decl(index.js, 5, 5))
16+
>other : Symbol(other, Decl(index.js, 1, 6))
17+
18+
const z = new defaultFoo();
19+
>z : Symbol(z, Decl(index.js, 6, 5))
20+
21+
=== tests/cases/compiler/other.d.ts ===
22+
export interface Foo {
23+
>Foo : Symbol(Foo, Decl(other.d.ts, 0, 0))
24+
25+
bar: number;
26+
>bar : Symbol(Foo.bar, Decl(other.d.ts, 0, 22))
27+
}
28+
29+
export default interface Bar {
30+
>Bar : Symbol(Bar, Decl(other.d.ts, 2, 1))
31+
32+
foo: number;
33+
>foo : Symbol(Bar.foo, Decl(other.d.ts, 4, 30))
34+
}
35+
36+
=== tests/cases/compiler/other.js ===
37+
export class Foo {
38+
>Foo : Symbol(Foo, Decl(other.js, 0, 0))
39+
40+
bar = 2.4;
41+
>bar : Symbol(Foo.bar, Decl(other.js, 0, 18))
42+
}
43+
44+
export default class Bar {
45+
>Bar : Symbol(Bar, Decl(other.js, 2, 1))
46+
47+
foo = 1.2;
48+
>foo : Symbol(Bar.foo, Decl(other.js, 4, 26))
49+
}
50+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
=== tests/cases/compiler/index.js ===
2+
import { Foo } from "./other.js";
3+
>Foo : any
4+
5+
import * as other from "./other.js";
6+
>other : typeof other
7+
8+
import defaultFoo from "./other.js";
9+
>defaultFoo : any
10+
11+
const x = new Foo();
12+
>x : error
13+
>new Foo() : error
14+
>Foo : error
15+
16+
const y = other.Foo();
17+
>y : error
18+
>other.Foo() : error
19+
>other.Foo : error
20+
>other : typeof other
21+
>Foo : any
22+
23+
const z = new defaultFoo();
24+
>z : error
25+
>new defaultFoo() : error
26+
>defaultFoo : error
27+
28+
=== tests/cases/compiler/other.d.ts ===
29+
export interface Foo {
30+
bar: number;
31+
>bar : number
32+
}
33+
34+
export default interface Bar {
35+
foo: number;
36+
>foo : number
37+
}
38+
39+
=== tests/cases/compiler/other.js ===
40+
export class Foo {
41+
>Foo : Foo
42+
43+
bar = 2.4;
44+
>bar : number
45+
>2.4 : 2.4
46+
}
47+
48+
export default class Bar {
49+
>Bar : Bar
50+
51+
foo = 1.2;
52+
>foo : number
53+
>1.2 : 1.2
54+
}
55+
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// @allowJs: true
2+
// @checkJs: false
3+
// @module: node16
4+
// @outDir: out
5+
6+
// @Filename: index.js
7+
import { Foo } from "./other.js";
8+
import * as other from "./other.js";
9+
import defaultFoo from "./other.js";
10+
11+
const x = new Foo();
12+
const y = other.Foo();
13+
const z = new defaultFoo();
14+
15+
// @Filename: other.d.ts
16+
export interface Foo {
17+
bar: number;
18+
}
19+
20+
export default interface Bar {
21+
foo: number;
22+
}
23+
24+
// @Filename: other.js
25+
export class Foo {
26+
bar = 2.4;
27+
}
28+
29+
export default class Bar {
30+
foo = 1.2;
31+
}

0 commit comments

Comments
 (0)