Skip to content

Handle non literal computed name when trying to get the name for object literal property name in json object #37988

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 1 commit into from
Apr 20, 2020
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
4 changes: 0 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7192,10 +7192,6 @@ namespace ts {
return symbol && getSymbolLinks(symbol).type || getTypeForVariableLikeDeclaration(node, /*includeOptionality*/ false);
}

function isComputedNonLiteralName(name: PropertyName): boolean {
return name.kind === SyntaxKind.ComputedPropertyName && !isStringOrNumericLiteralLike(name.expression);
}

function getRestType(source: Type, properties: PropertyName[], symbol: Symbol | undefined): Type {
source = filterType(source, t => !(t.flags & TypeFlags.Nullable));
if (source.flags & TypeFlags.Never) {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1721,7 +1721,7 @@ namespace ts {
errors.push(createDiagnosticForNodeInSourceFile(sourceFile, element.name, Diagnostics.String_literal_with_double_quotes_expected));
}

const textOfKey = getTextOfPropertyName(element.name);
const textOfKey = isComputedNonLiteralName(element.name) ? undefined : getTextOfPropertyName(element.name);
const keyText = textOfKey && unescapeLeadingUnderscores(textOfKey);
const option = keyText && knownOptions ? knownOptions.get(keyText) : undefined;
if (keyText && extraKeyDiagnostics && !option) {
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,10 @@ namespace ts {
return info.declaration ? declarationNameToString(info.declaration.parameters[0].name) : undefined;
}

export function isComputedNonLiteralName(name: PropertyName): boolean {
return name.kind === SyntaxKind.ComputedPropertyName && !isStringOrNumericLiteralLike(name.expression);
}

export function getTextOfPropertyName(name: PropertyName | NoSubstitutionTemplateLiteral): __String {
switch (name.kind) {
case SyntaxKind.Identifier:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
tests/cases/compiler/b.json(2,5): error TS1327: String literal with double quotes expected.


==== tests/cases/compiler/file1.ts (0 errors) ====
import b1 = require('./b.json');
let x = b1;
import b2 = require('./b.json');
if (x) {
x = b2;
}

==== tests/cases/compiler/b.json (1 errors) ====
{
[a]: 10
~~~
!!! error TS1327: String literal with double quotes expected.
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//// [tests/cases/compiler/requireOfJsonFileWithComputedPropertyName.ts] ////

//// [file1.ts]
import b1 = require('./b.json');
let x = b1;
import b2 = require('./b.json');
if (x) {
x = b2;
}

//// [b.json]
{
[a]: 10
}

//// [b.json]
var _a;
_a = {},
_a[a] = 10,
_a;
//// [file1.js]
"use strict";
exports.__esModule = true;
var b1 = require("./b.json");
var x = b1;
var b2 = require("./b.json");
if (x) {
x = b2;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
=== tests/cases/compiler/file1.ts ===
import b1 = require('./b.json');
>b1 : Symbol(b1, Decl(file1.ts, 0, 0))

let x = b1;
>x : Symbol(x, Decl(file1.ts, 1, 3))
>b1 : Symbol(b1, Decl(file1.ts, 0, 0))

import b2 = require('./b.json');
>b2 : Symbol(b2, Decl(file1.ts, 1, 11))

if (x) {
>x : Symbol(x, Decl(file1.ts, 1, 3))

x = b2;
>x : Symbol(x, Decl(file1.ts, 1, 3))
>b2 : Symbol(b2, Decl(file1.ts, 1, 11))
}

=== tests/cases/compiler/b.json ===
{
[a]: 10
>[a] : Symbol([a], Decl(b.json, 0, 1))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
=== tests/cases/compiler/file1.ts ===
import b1 = require('./b.json');
>b1 : { [x: number]: number; }

let x = b1;
>x : { [x: number]: number; }
>b1 : { [x: number]: number; }

import b2 = require('./b.json');
>b2 : { [x: number]: number; }

if (x) {
>x : { [x: number]: number; }

x = b2;
>x = b2 : { [x: number]: number; }
>x : { [x: number]: number; }
>b2 : { [x: number]: number; }
}

=== tests/cases/compiler/b.json ===
{
>{ [a]: 10} : { [x: number]: number; }

[a]: 10
>[a] : number
>a : any
>10 : 10
}
15 changes: 15 additions & 0 deletions tests/cases/compiler/requireOfJsonFileWithComputedPropertyName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// @outdir: out/
// @resolveJsonModule: true

// @Filename: file1.ts
import b1 = require('./b.json');
let x = b1;
import b2 = require('./b.json');
if (x) {
x = b2;
}

// @Filename: b.json
{
[a]: 10
}