Skip to content

Allow isSymbolAccessible to paint object literal declarations as visible #24668

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
13 changes: 12 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2868,7 +2868,18 @@ namespace ts {
// we are going to see if c can be accessed in scope directly.
// But it can't, hence the accessible is going to be undefined, but that doesn't mean m.c is inaccessible
// It is accessible if the parent m is accessible because then m.c can be accessed through qualification
const parentResult = isAnySymbolAccessible(getContainersOfSymbol(symbol, enclosingDeclaration), enclosingDeclaration, initialSymbol, initialSymbol === symbol ? getQualifiedLeftMeaning(meaning) : meaning, shouldComputeAliasesToMakeVisible);

let containers = getContainersOfSymbol(symbol, enclosingDeclaration);
// If we're trying to reference some object literal in, eg `var a = { x: 1 }`, the symbol for the literal, `__object`, is distinct
// from the symbol of the declaration it is being assigned to. Since we can use the declaration to refer to the literal, however,
// we'd like to make that connection here - potentially causing us to paint the declararation's visibiility, and therefore the literal.
const firstDecl: Node = first(symbol.declarations);
if (!length(containers) && meaning & SymbolFlags.Value && firstDecl && isObjectLiteralExpression(firstDecl)) {
if (firstDecl.parent && isVariableDeclaration(firstDecl.parent) && firstDecl === firstDecl.parent.initializer) {
containers = [getSymbolOfNode(firstDecl.parent)];
}
}
const parentResult = isAnySymbolAccessible(containers, enclosingDeclaration, initialSymbol, initialSymbol === symbol ? getQualifiedLeftMeaning(meaning) : meaning, shouldComputeAliasesToMakeVisible);
if (parentResult) {
return parentResult;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//// [objectLiteralComputedNameNoDeclarationError.ts]
const Foo = {
BANANA: 'banana' as 'banana',
}

export const Baa = {
[Foo.BANANA]: 1
};

//// [objectLiteralComputedNameNoDeclarationError.js]
"use strict";
exports.__esModule = true;
var _a;
var Foo = {
BANANA: 'banana'
};
exports.Baa = (_a = {},
_a[Foo.BANANA] = 1,
_a);


//// [objectLiteralComputedNameNoDeclarationError.d.ts]
declare const Foo: {
BANANA: "banana";
};
export declare const Baa: {
[Foo.BANANA]: number;
};
export {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
=== tests/cases/compiler/objectLiteralComputedNameNoDeclarationError.ts ===
const Foo = {
>Foo : Symbol(Foo, Decl(objectLiteralComputedNameNoDeclarationError.ts, 0, 5))

BANANA: 'banana' as 'banana',
>BANANA : Symbol(BANANA, Decl(objectLiteralComputedNameNoDeclarationError.ts, 0, 13))
}

export const Baa = {
>Baa : Symbol(Baa, Decl(objectLiteralComputedNameNoDeclarationError.ts, 4, 12))

[Foo.BANANA]: 1
>[Foo.BANANA] : Symbol([Foo.BANANA], Decl(objectLiteralComputedNameNoDeclarationError.ts, 4, 20))
>Foo.BANANA : Symbol(BANANA, Decl(objectLiteralComputedNameNoDeclarationError.ts, 0, 13))
>Foo : Symbol(Foo, Decl(objectLiteralComputedNameNoDeclarationError.ts, 0, 5))
>BANANA : Symbol(BANANA, Decl(objectLiteralComputedNameNoDeclarationError.ts, 0, 13))

};
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
=== tests/cases/compiler/objectLiteralComputedNameNoDeclarationError.ts ===
const Foo = {
>Foo : { BANANA: "banana"; }
>{ BANANA: 'banana' as 'banana',} : { BANANA: "banana"; }

BANANA: 'banana' as 'banana',
>BANANA : "banana"
>'banana' as 'banana' : "banana"
>'banana' : "banana"
}

export const Baa = {
>Baa : { [Foo.BANANA]: number; }
>{ [Foo.BANANA]: 1} : { [Foo.BANANA]: number; }

[Foo.BANANA]: 1
>[Foo.BANANA] : number
>Foo.BANANA : "banana"
>Foo : { BANANA: "banana"; }
>BANANA : "banana"
>1 : 1

};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @declaration: true
const Foo = {
BANANA: 'banana' as 'banana',
}

export const Baa = {
[Foo.BANANA]: 1
};