Skip to content

Commit

Permalink
More tests, only use computed name emit when all names are visible
Browse files Browse the repository at this point in the history
  • Loading branch information
weswigham committed Oct 1, 2024
1 parent 22c5c89 commit 587f9e0
Show file tree
Hide file tree
Showing 45 changed files with 829 additions and 169 deletions.
40 changes: 18 additions & 22 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7211,29 +7211,25 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
function indexInfoToObjectComputedNamesOrSignatureDeclaration(indexInfo: IndexInfo, context: NodeBuilderContext, typeNode: TypeNode | undefined): [IndexSignatureDeclaration] | PropertySignature[] {
if (indexInfo.components) {
// Index info is derived from object or class computed property names (plus explicit named members) - we can clone those instead of writing out the result computed index signature
let unusableResult = false;
const result = map(indexInfo.components, e => {
if (e.name && isComputedPropertyName(e.name) && isEntityNameExpression(e.name.expression)) {
trackComputedName(e.name.expression, context.enclosingDeclaration, context);
}
else {
// Computed name didn't take the form `[a.b.c]: something` - bail on using the computed name.
// TODO: Issue isolated declarations error on this fallback?
unusableResult = true;
}
return setTextRange(
context,
factory.createPropertySignature(
indexInfo.isReadonly ? [factory.createModifier(SyntaxKind.ReadonlyKeyword)] : undefined,
e.name,
/*questionToken*/ undefined,
typeNode || typeToTypeNodeHelper(getTypeOfSymbol(e.symbol), context),
),
e,
);
const allComponentComputedNamesSerializable = every(indexInfo.components, e => {
return !!(e.name && isComputedPropertyName(e.name) && isEntityNameExpression(e.name.expression) && context.enclosingDeclaration && isEntityNameVisible(e.name.expression, context.enclosingDeclaration, /*shouldComputeAliasToMakeVisible*/ false)?.accessibility === SymbolAccessibility.Accessible);
});
if (!unusableResult) {
return result;
if (allComponentComputedNamesSerializable) {
// Only use computed name serialization form if all components are visible and take the `a.b.c` form
return map(indexInfo.components, e => {
// Still need to track visibility even if we've already checked it to paint references as used
trackComputedName(e.name.expression as EntityNameExpression, context.enclosingDeclaration, context);
return setTextRange(
context,
factory.createPropertySignature(
indexInfo.isReadonly ? [factory.createModifier(SyntaxKind.ReadonlyKeyword)] : undefined,
e.name,
/*questionToken*/ undefined,
typeNode || typeToTypeNodeHelper(getTypeOfSymbol(e.symbol), context),
),
e,
);
});
}
}
return [indexInfoToIndexSignatureDeclarationHelper(indexInfo, context, typeNode)];
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/FunctionDeclaration8_es6.types
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

=== FunctionDeclaration8_es6.ts ===
var v = { [yield]: foo }
>v : { [yield]: any; }
> : ^^ ^^^ ^^
>{ [yield]: foo } : { [yield]: any; }
> : ^^ ^^^ ^^
>v : { [x: number]: any; }
> : ^^^^^^^^^^^^^^^^^^^^^
>{ [yield]: foo } : { [x: number]: any; }
> : ^^^^^^^^^^^^^^^^^^^^^
>[yield] : any
> : ^^^
>yield : any
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

=== asyncFunctionDeclaration8_es2017.ts ===
var v = { [await]: foo }
>v : { [await]: any; }
> : ^^ ^^^ ^^
>{ [await]: foo } : { [await]: any; }
> : ^^ ^^^ ^^
>v : { [x: number]: any; }
> : ^^^^^^^^^^^^^^^^^^^^^
>{ [await]: foo } : { [x: number]: any; }
> : ^^^^^^^^^^^^^^^^^^^^^
>[await] : any
> : ^^^
>await : any
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/asyncFunctionDeclaration8_es5.types
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

=== asyncFunctionDeclaration8_es5.ts ===
var v = { [await]: foo }
>v : { [await]: any; }
> : ^^ ^^^ ^^
>{ [await]: foo } : { [await]: any; }
> : ^^ ^^^ ^^
>v : { [x: number]: any; }
> : ^^^^^^^^^^^^^^^^^^^^^
>{ [await]: foo } : { [x: number]: any; }
> : ^^^^^^^^^^^^^^^^^^^^^
>[await] : any
> : ^^^
>await : any
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/asyncFunctionDeclaration8_es6.types
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

=== asyncFunctionDeclaration8_es6.ts ===
var v = { [await]: foo }
>v : { [await]: any; }
> : ^^ ^^^ ^^
>{ [await]: foo } : { [await]: any; }
> : ^^ ^^^ ^^
>v : { [x: number]: any; }
> : ^^^^^^^^^^^^^^^^^^^^^
>{ [await]: foo } : { [x: number]: any; }
> : ^^^^^^^^^^^^^^^^^^^^^
>[await] : any
> : ^^^
>await : any
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/complicatedPrivacy.types
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ module m1 {
export function f4(arg1:
>f4 : (arg1: { [number]: C1; }) => void
> : ^ ^^ ^^^^^^^^^
>arg1 : { [number]: C1; }
> : ^^ ^^ ^^
>arg1 : { [x: number]: C1; }
> : ^^^^^^^^^^^^^^^^^^^^
{
[number]: C1; // Used to be indexer, now it is a computed property
>[number] : C1
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/computedPropertyNames51_ES5.types
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ function f<T, K extends keyof T>() {
> : ^

var v = {
>v : { [t]: number; [k]: number; }
> : ^^ ^^^^^^ ^ ^^^^^^ ^^
>{ [t]: 0, [k]: 1 } : { [t]: number; [k]: number; }
> : ^^ ^^^^^^ ^ ^^^^^^ ^^
>v : { [x: string]: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^
>{ [t]: 0, [k]: 1 } : { [x: string]: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^

[t]: 0,
>[t] : number
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/computedPropertyNames51_ES6.types
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ function f<T, K extends keyof T>() {
> : ^

var v = {
>v : { [t]: number; [k]: number; }
> : ^^ ^^^^^^ ^ ^^^^^^ ^^
>{ [t]: 0, [k]: 1 } : { [t]: number; [k]: number; }
> : ^^ ^^^^^^ ^ ^^^^^^ ^^
>v : { [x: string]: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^
>{ [t]: 0, [k]: 1 } : { [x: string]: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^

[t]: 0,
>[t] : number
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/computedPropertyNames8_ES5.types
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ function f<T, U extends string>() {
> : ^

var v = {
>v : { [t]: number; [u]: number; }
> : ^^ ^^^^^^ ^ ^^^^^^ ^^
>{ [t]: 0, [u]: 1 } : { [t]: number; [u]: number; }
> : ^^ ^^^^^^ ^ ^^^^^^ ^^
>v : { [x: string]: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^
>{ [t]: 0, [u]: 1 } : { [x: string]: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^

[t]: 0,
>[t] : number
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/computedPropertyNames8_ES6.types
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ function f<T, U extends string>() {
> : ^

var v = {
>v : { [t]: number; [u]: number; }
> : ^^ ^^^^^^ ^ ^^^^^^ ^^
>{ [t]: 0, [u]: 1 } : { [t]: number; [u]: number; }
> : ^^ ^^^^^^ ^ ^^^^^^ ^^
>v : { [x: string]: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^
>{ [t]: 0, [u]: 1 } : { [x: string]: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^

[t]: 0,
>[t] : number
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//// [tests/cases/compiler/declarationEmitComputedNamesInaccessible.ts] ////

//// [declarationEmitComputedNamesInaccessible.ts]
export function f1() {
const localClassFieldName = Math.random() > 0.5 ? "g1" : "g2";
const localOtherField = localClassFieldName === "g1" ? "g2" : "g1";
const localStaticField = Math.random() > 0.5 ? "s1" : "s2";
return class ParameterizedHolder {
[localClassFieldName]() {
return "value";
}
[localOtherField]() {
return 42;
}
static [localStaticField]() {
return { static: true };
}
static [localStaticField]() {
return { static: "sometimes" };
}
}
}

//// [declarationEmitComputedNamesInaccessible.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.f1 = f1;
function f1() {
var localClassFieldName = Math.random() > 0.5 ? "g1" : "g2";
var localOtherField = localClassFieldName === "g1" ? "g2" : "g1";
var localStaticField = Math.random() > 0.5 ? "s1" : "s2";
return /** @class */ (function () {
function ParameterizedHolder() {
}
ParameterizedHolder.prototype[localClassFieldName] = function () {
return "value";
};
ParameterizedHolder.prototype[localOtherField] = function () {
return 42;
};
ParameterizedHolder[localStaticField] = function () {
return { static: true };
};
ParameterizedHolder[localStaticField] = function () {
return { static: "sometimes" };
};
return ParameterizedHolder;
}());
}


//// [declarationEmitComputedNamesInaccessible.d.ts]
export declare function f1(): {
new (): {
[x: string]: (() => string) | (() => number);
};
[x: string]: {
[x: string]: (() => string) | (() => number);
} | (() => {
static: boolean;
}) | (() => {
static: string;
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//// [tests/cases/compiler/declarationEmitComputedNamesInaccessible.ts] ////

=== declarationEmitComputedNamesInaccessible.ts ===
export function f1() {
>f1 : Symbol(f1, Decl(declarationEmitComputedNamesInaccessible.ts, 0, 0))

const localClassFieldName = Math.random() > 0.5 ? "g1" : "g2";
>localClassFieldName : Symbol(localClassFieldName, Decl(declarationEmitComputedNamesInaccessible.ts, 1, 9))
>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))

const localOtherField = localClassFieldName === "g1" ? "g2" : "g1";
>localOtherField : Symbol(localOtherField, Decl(declarationEmitComputedNamesInaccessible.ts, 2, 9))
>localClassFieldName : Symbol(localClassFieldName, Decl(declarationEmitComputedNamesInaccessible.ts, 1, 9))

const localStaticField = Math.random() > 0.5 ? "s1" : "s2";
>localStaticField : Symbol(localStaticField, Decl(declarationEmitComputedNamesInaccessible.ts, 3, 9))
>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))

return class ParameterizedHolder {
>ParameterizedHolder : Symbol(ParameterizedHolder, Decl(declarationEmitComputedNamesInaccessible.ts, 4, 10))

[localClassFieldName]() {
>[localClassFieldName] : Symbol(ParameterizedHolder[localClassFieldName], Decl(declarationEmitComputedNamesInaccessible.ts, 4, 38))
>localClassFieldName : Symbol(localClassFieldName, Decl(declarationEmitComputedNamesInaccessible.ts, 1, 9))

return "value";
}
[localOtherField]() {
>[localOtherField] : Symbol(ParameterizedHolder[localOtherField], Decl(declarationEmitComputedNamesInaccessible.ts, 7, 9))
>localOtherField : Symbol(localOtherField, Decl(declarationEmitComputedNamesInaccessible.ts, 2, 9))

return 42;
}
static [localStaticField]() {
>[localStaticField] : Symbol(ParameterizedHolder[localStaticField], Decl(declarationEmitComputedNamesInaccessible.ts, 10, 9))
>localStaticField : Symbol(localStaticField, Decl(declarationEmitComputedNamesInaccessible.ts, 3, 9))

return { static: true };
>static : Symbol(static, Decl(declarationEmitComputedNamesInaccessible.ts, 12, 20))
}
static [localStaticField]() {
>[localStaticField] : Symbol(ParameterizedHolder[localStaticField], Decl(declarationEmitComputedNamesInaccessible.ts, 13, 9))
>localStaticField : Symbol(localStaticField, Decl(declarationEmitComputedNamesInaccessible.ts, 3, 9))

return { static: "sometimes" };
>static : Symbol(static, Decl(declarationEmitComputedNamesInaccessible.ts, 15, 20))
}
}
}
Loading

0 comments on commit 587f9e0

Please sign in to comment.