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 literalsmaster
parent
c2bdcbe996
commit
4629db6b0d
|
@ -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)
|
||||
|
|
|
@ -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(": ");
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
// These used to be indexers, now they are computed properties
|
||||
interface I {
|
||||
[x = '']: string;
|
||||
}
|
||||
|
||||
class C {
|
||||
[x = 0]: string
|
||||
}
|
|
@ -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"
|
||||
};
|
||||
|
||||
`;
|
|
@ -0,0 +1 @@
|
|||
run_spec(__dirname, { parser: "typescript" });
|
|
@ -0,0 +1,7 @@
|
|||
interface I {
|
||||
"string": "I";
|
||||
}
|
||||
|
||||
type T = {
|
||||
"string": "T";
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
interface I {
|
||||
[Symbol.toStringTag]: "I";
|
||||
}
|
||||
|
||||
type T = {
|
||||
[Symbol.toStringTag]: "T";
|
||||
}
|
Loading…
Reference in New Issue