Skip to content

Commit e7774c6

Browse files
authored
Handle non literal computed name when trying to get the name for object literal property name in json object (#37988)
Fixes #37984
1 parent 0f3f37b commit e7774c6

8 files changed

+119
-5
lines changed

src/compiler/checker.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7192,10 +7192,6 @@ namespace ts {
71927192
return symbol && getSymbolLinks(symbol).type || getTypeForVariableLikeDeclaration(node, /*includeOptionality*/ false);
71937193
}
71947194

7195-
function isComputedNonLiteralName(name: PropertyName): boolean {
7196-
return name.kind === SyntaxKind.ComputedPropertyName && !isStringOrNumericLiteralLike(name.expression);
7197-
}
7198-
71997195
function getRestType(source: Type, properties: PropertyName[], symbol: Symbol | undefined): Type {
72007196
source = filterType(source, t => !(t.flags & TypeFlags.Nullable));
72017197
if (source.flags & TypeFlags.Never) {

src/compiler/commandLineParser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1721,7 +1721,7 @@ namespace ts {
17211721
errors.push(createDiagnosticForNodeInSourceFile(sourceFile, element.name, Diagnostics.String_literal_with_double_quotes_expected));
17221722
}
17231723

1724-
const textOfKey = getTextOfPropertyName(element.name);
1724+
const textOfKey = isComputedNonLiteralName(element.name) ? undefined : getTextOfPropertyName(element.name);
17251725
const keyText = textOfKey && unescapeLeadingUnderscores(textOfKey);
17261726
const option = keyText && knownOptions ? knownOptions.get(keyText) : undefined;
17271727
if (keyText && extraKeyDiagnostics && !option) {

src/compiler/utilities.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,10 @@ namespace ts {
876876
return info.declaration ? declarationNameToString(info.declaration.parameters[0].name) : undefined;
877877
}
878878

879+
export function isComputedNonLiteralName(name: PropertyName): boolean {
880+
return name.kind === SyntaxKind.ComputedPropertyName && !isStringOrNumericLiteralLike(name.expression);
881+
}
882+
879883
export function getTextOfPropertyName(name: PropertyName | NoSubstitutionTemplateLiteral): __String {
880884
switch (name.kind) {
881885
case SyntaxKind.Identifier:
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
tests/cases/compiler/b.json(2,5): error TS1327: String literal with double quotes expected.
2+
3+
4+
==== tests/cases/compiler/file1.ts (0 errors) ====
5+
import b1 = require('./b.json');
6+
let x = b1;
7+
import b2 = require('./b.json');
8+
if (x) {
9+
x = b2;
10+
}
11+
12+
==== tests/cases/compiler/b.json (1 errors) ====
13+
{
14+
[a]: 10
15+
~~~
16+
!!! error TS1327: String literal with double quotes expected.
17+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//// [tests/cases/compiler/requireOfJsonFileWithComputedPropertyName.ts] ////
2+
3+
//// [file1.ts]
4+
import b1 = require('./b.json');
5+
let x = b1;
6+
import b2 = require('./b.json');
7+
if (x) {
8+
x = b2;
9+
}
10+
11+
//// [b.json]
12+
{
13+
[a]: 10
14+
}
15+
16+
//// [b.json]
17+
var _a;
18+
_a = {},
19+
_a[a] = 10,
20+
_a;
21+
//// [file1.js]
22+
"use strict";
23+
exports.__esModule = true;
24+
var b1 = require("./b.json");
25+
var x = b1;
26+
var b2 = require("./b.json");
27+
if (x) {
28+
x = b2;
29+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
=== tests/cases/compiler/file1.ts ===
2+
import b1 = require('./b.json');
3+
>b1 : Symbol(b1, Decl(file1.ts, 0, 0))
4+
5+
let x = b1;
6+
>x : Symbol(x, Decl(file1.ts, 1, 3))
7+
>b1 : Symbol(b1, Decl(file1.ts, 0, 0))
8+
9+
import b2 = require('./b.json');
10+
>b2 : Symbol(b2, Decl(file1.ts, 1, 11))
11+
12+
if (x) {
13+
>x : Symbol(x, Decl(file1.ts, 1, 3))
14+
15+
x = b2;
16+
>x : Symbol(x, Decl(file1.ts, 1, 3))
17+
>b2 : Symbol(b2, Decl(file1.ts, 1, 11))
18+
}
19+
20+
=== tests/cases/compiler/b.json ===
21+
{
22+
[a]: 10
23+
>[a] : Symbol([a], Decl(b.json, 0, 1))
24+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
=== tests/cases/compiler/file1.ts ===
2+
import b1 = require('./b.json');
3+
>b1 : { [x: number]: number; }
4+
5+
let x = b1;
6+
>x : { [x: number]: number; }
7+
>b1 : { [x: number]: number; }
8+
9+
import b2 = require('./b.json');
10+
>b2 : { [x: number]: number; }
11+
12+
if (x) {
13+
>x : { [x: number]: number; }
14+
15+
x = b2;
16+
>x = b2 : { [x: number]: number; }
17+
>x : { [x: number]: number; }
18+
>b2 : { [x: number]: number; }
19+
}
20+
21+
=== tests/cases/compiler/b.json ===
22+
{
23+
>{ [a]: 10} : { [x: number]: number; }
24+
25+
[a]: 10
26+
>[a] : number
27+
>a : any
28+
>10 : 10
29+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// @outdir: out/
2+
// @resolveJsonModule: true
3+
4+
// @Filename: file1.ts
5+
import b1 = require('./b.json');
6+
let x = b1;
7+
import b2 = require('./b.json');
8+
if (x) {
9+
x = b2;
10+
}
11+
12+
// @Filename: b.json
13+
{
14+
[a]: 10
15+
}

0 commit comments

Comments
 (0)