TypeScript: improve handling of computed properties (#1532)

* fix(typescript): improve handling of computed properties

* test(typescript): add Symbol computed property test

* fix(typescript): do not print brackets for literals
master
Lucas Azzola 2017-05-07 01:00:26 +10:00 committed by Christopher Chedeau
parent c2bdcbe996
commit 4629db6b0d
8 changed files with 115 additions and 15 deletions

View File

@ -445,6 +445,14 @@ FPp.needsParens = function() {
case "AssignmentExpression":
if (parent.type === "ArrowFunctionExpression" && parent.body === node) {
return true;
} else if (
parent.type === "ClassProperty" &&
parent.key === node &&
parent.computed
) {
return false;
} else if (parent.type === "TSPropertySignature" && parent.name === node) {
return false;
} else if (
parent.type === "ForStatement" &&
(parent.init === node || parent.update === node)

View File

@ -2093,10 +2093,20 @@ function genericPrintNoParens(path, options, print, args) {
case "TSArrayType":
return concat([path.call(print, "elementType"), "[]"]);
case "TSPropertySignature":
parts.push(
printTypeScriptModifiers(path, options, print),
path.call(print, "name")
);
var computed = !namedTypes.Identifier.check(n.name) &&
!namedTypes.Literal.check(n.name);
parts.push(printTypeScriptModifiers(path, options, print));
if (computed) {
parts.push("[");
}
parts.push(path.call(print, "name"));
if (computed) {
parts.push("]");
}
if (n.typeAnnotation) {
parts.push(": ");

View File

@ -7,17 +7,6 @@ var results = number[];
`;
exports[`commentsInterface.ts 1`] = `
interface i2 {
foo: (/**param help*/b: number) => string;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
interface i2 {
foo: (/**param help*/ b: number) => string
}
`;
exports[`checkInfiniteExpansionTermination.ts 1`] = `
// Regression test for #1002
// Before fix this code would cause infinite loop
@ -55,6 +44,17 @@ values = values2;
`;
exports[`commentsInterface.ts 1`] = `
interface i2 {
foo: (/**param help*/b: number) => string;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
interface i2 {
foo: (/**param help*/ b: number) => string
}
`;
exports[`functionOverloadsOnGenericArity1.ts 1`] = `
// overloading on arity not allowed
interface C {
@ -82,6 +82,26 @@ interface C {
`;
exports[`indexSignatureWithInitializer.ts 1`] = `
// These used to be indexers, now they are computed properties
interface I {
[x = '']: string;
}
class C {
[x = 0]: string
}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// These used to be indexers, now they are computed properties
interface I {
[x = ""]: string
}
class C {
[x = 0]: string;
}
`;
exports[`mappedTypeWithCombinedTypeMappers.ts 1`] = `
// Repro from #13351

View File

@ -0,0 +1,8 @@
// These used to be indexers, now they are computed properties
interface I {
[x = '']: string;
}
class C {
[x = 0]: string
}

View File

@ -0,0 +1,39 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`string.ts 1`] = `
interface I {
"string": "I";
}
type T = {
"string": "T";
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
interface I {
"string": "I"
}
type T = {
"string": "T"
};
`;
exports[`symbol.ts 1`] = `
interface I {
[Symbol.toStringTag]: "I";
}
type T = {
[Symbol.toStringTag]: "T";
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
interface I {
[Symbol.toStringTag]: "I"
}
type T = {
[Symbol.toStringTag]: "T"
};
`;

View File

@ -0,0 +1 @@
run_spec(__dirname, { parser: "typescript" });

View File

@ -0,0 +1,7 @@
interface I {
"string": "I";
}
type T = {
"string": "T";
}

View File

@ -0,0 +1,7 @@
interface I {
[Symbol.toStringTag]: "I";
}
type T = {
[Symbol.toStringTag]: "T";
}