Skip to content

Commit 0b1183a

Browse files
authored
Allow isSymbolAccessible to paint object literal declarations as visible (microsoft#24668)
* Dont use resolveEntityName for computed property name symbol resolution - use checkExpression and resolvedSymbol instead * Fix lint
1 parent 69c7e67 commit 0b1183a

File tree

5 files changed

+90
-1
lines changed

5 files changed

+90
-1
lines changed

src/compiler/checker.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2871,7 +2871,18 @@ namespace ts {
28712871
// we are going to see if c can be accessed in scope directly.
28722872
// But it can't, hence the accessible is going to be undefined, but that doesn't mean m.c is inaccessible
28732873
// It is accessible if the parent m is accessible because then m.c can be accessed through qualification
2874-
const parentResult = isAnySymbolAccessible(getContainersOfSymbol(symbol, enclosingDeclaration), enclosingDeclaration, initialSymbol, initialSymbol === symbol ? getQualifiedLeftMeaning(meaning) : meaning, shouldComputeAliasesToMakeVisible);
2874+
2875+
let containers = getContainersOfSymbol(symbol, enclosingDeclaration);
2876+
// If we're trying to reference some object literal in, eg `var a = { x: 1 }`, the symbol for the literal, `__object`, is distinct
2877+
// from the symbol of the declaration it is being assigned to. Since we can use the declaration to refer to the literal, however,
2878+
// we'd like to make that connection here - potentially causing us to paint the declararation's visibiility, and therefore the literal.
2879+
const firstDecl: Node = first(symbol.declarations);
2880+
if (!length(containers) && meaning & SymbolFlags.Value && firstDecl && isObjectLiteralExpression(firstDecl)) {
2881+
if (firstDecl.parent && isVariableDeclaration(firstDecl.parent) && firstDecl === firstDecl.parent.initializer) {
2882+
containers = [getSymbolOfNode(firstDecl.parent)];
2883+
}
2884+
}
2885+
const parentResult = isAnySymbolAccessible(containers, enclosingDeclaration, initialSymbol, initialSymbol === symbol ? getQualifiedLeftMeaning(meaning) : meaning, shouldComputeAliasesToMakeVisible);
28752886
if (parentResult) {
28762887
return parentResult;
28772888
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//// [objectLiteralComputedNameNoDeclarationError.ts]
2+
const Foo = {
3+
BANANA: 'banana' as 'banana',
4+
}
5+
6+
export const Baa = {
7+
[Foo.BANANA]: 1
8+
};
9+
10+
//// [objectLiteralComputedNameNoDeclarationError.js]
11+
"use strict";
12+
exports.__esModule = true;
13+
var _a;
14+
var Foo = {
15+
BANANA: 'banana'
16+
};
17+
exports.Baa = (_a = {},
18+
_a[Foo.BANANA] = 1,
19+
_a);
20+
21+
22+
//// [objectLiteralComputedNameNoDeclarationError.d.ts]
23+
declare const Foo: {
24+
BANANA: "banana";
25+
};
26+
export declare const Baa: {
27+
[Foo.BANANA]: number;
28+
};
29+
export {};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
=== tests/cases/compiler/objectLiteralComputedNameNoDeclarationError.ts ===
2+
const Foo = {
3+
>Foo : Symbol(Foo, Decl(objectLiteralComputedNameNoDeclarationError.ts, 0, 5))
4+
5+
BANANA: 'banana' as 'banana',
6+
>BANANA : Symbol(BANANA, Decl(objectLiteralComputedNameNoDeclarationError.ts, 0, 13))
7+
}
8+
9+
export const Baa = {
10+
>Baa : Symbol(Baa, Decl(objectLiteralComputedNameNoDeclarationError.ts, 4, 12))
11+
12+
[Foo.BANANA]: 1
13+
>[Foo.BANANA] : Symbol([Foo.BANANA], Decl(objectLiteralComputedNameNoDeclarationError.ts, 4, 20))
14+
>Foo.BANANA : Symbol(BANANA, Decl(objectLiteralComputedNameNoDeclarationError.ts, 0, 13))
15+
>Foo : Symbol(Foo, Decl(objectLiteralComputedNameNoDeclarationError.ts, 0, 5))
16+
>BANANA : Symbol(BANANA, Decl(objectLiteralComputedNameNoDeclarationError.ts, 0, 13))
17+
18+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
=== tests/cases/compiler/objectLiteralComputedNameNoDeclarationError.ts ===
2+
const Foo = {
3+
>Foo : { BANANA: "banana"; }
4+
>{ BANANA: 'banana' as 'banana',} : { BANANA: "banana"; }
5+
6+
BANANA: 'banana' as 'banana',
7+
>BANANA : "banana"
8+
>'banana' as 'banana' : "banana"
9+
>'banana' : "banana"
10+
}
11+
12+
export const Baa = {
13+
>Baa : { [Foo.BANANA]: number; }
14+
>{ [Foo.BANANA]: 1} : { [Foo.BANANA]: number; }
15+
16+
[Foo.BANANA]: 1
17+
>[Foo.BANANA] : number
18+
>Foo.BANANA : "banana"
19+
>Foo : { BANANA: "banana"; }
20+
>BANANA : "banana"
21+
>1 : 1
22+
23+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// @declaration: true
2+
const Foo = {
3+
BANANA: 'banana' as 'banana',
4+
}
5+
6+
export const Baa = {
7+
[Foo.BANANA]: 1
8+
};

0 commit comments

Comments
 (0)