Skip to content
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

feat(typescript-estree): computed members discriminated unions #1349

Merged
merged 7 commits into from Dec 19, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/rules/indent.ts
Expand Up @@ -392,7 +392,7 @@ export default util.createRule<Options, MessageIds>({
computed: false,
method: false,
shorthand: false,
},
} as any,
],

// location data
Expand Down
Expand Up @@ -63,7 +63,7 @@ export default util.createRule<Options, MessageIds>({
) {
return ignoredMethods.has(node.key.quasis[0].value.raw);
}
if (node.key.type === AST_NODE_TYPES.Identifier && !node.computed) {
if (!node.computed && node.key.type === AST_NODE_TYPES.Identifier) {
return ignoredMethods.has(node.key.name);
}

Expand Down
3 changes: 1 addition & 2 deletions packages/eslint-plugin/src/rules/prefer-for-of.ts
Expand Up @@ -149,9 +149,8 @@ export default util.createRule({
// ({ foo: a[i] }) = { foo: 0 }
if (
parent.type === AST_NODE_TYPES.Property &&
parent.parent !== undefined &&
parent.parent.type === AST_NODE_TYPES.ObjectExpression &&
parent.value === node &&
parent.parent?.type === AST_NODE_TYPES.ObjectExpression &&
isAssignee(parent.parent)
) {
return true;
Expand Down
22 changes: 4 additions & 18 deletions packages/eslint-plugin/src/util/misc.ts
Expand Up @@ -107,29 +107,16 @@ function getNameFromMember(
| TSESTree.TSPropertySignature,
sourceCode: TSESLint.SourceCode,
): string {
if (isLiteralOrIdentifier(member.key)) {
if (member.key.type === AST_NODE_TYPES.Identifier) {
return member.key.name;
}
if (member.key.type === AST_NODE_TYPES.Identifier) {
return member.key.name;
}
if (member.key.type === AST_NODE_TYPES.Literal) {
return `${member.key.value}`;
}

return sourceCode.text.slice(...member.key.range);
}

/**
* This covers both actual property names, as well as computed properties that are either
* an identifier or a literal at the top level.
*/
function isLiteralOrIdentifier(
node: TSESTree.Expression,
): node is TSESTree.Literal | TSESTree.Identifier {
return (
node.type === AST_NODE_TYPES.Literal ||
node.type === AST_NODE_TYPES.Identifier
);
}

type ExcludeKeys<
TObj extends Record<string, unknown>,
TKeys extends keyof TObj
Expand All @@ -148,7 +135,6 @@ export {
InferMessageIdsTypeFromRule,
InferOptionsTypeFromRule,
isDefinitionFile,
isLiteralOrIdentifier,
RequireKeys,
upperCaseFirst,
};
120 changes: 120 additions & 0 deletions packages/parser/tests/lib/__snapshots__/typescript.ts.snap
Expand Up @@ -15814,6 +15814,126 @@ Object {
}
`;

exports[`typescript fixtures/basics/export-named-enum-computed.src 1`] = `
Object {
"$id": 3,
"block": Object {
"range": Array [
0,
42,
],
"type": "Program",
},
"childScopes": Array [
Object {
"$id": 2,
"block": Object {
"range": Array [
0,
42,
],
"type": "Program",
},
"childScopes": Array [
Object {
"$id": 1,
"block": Object {
"range": Array [
7,
41,
],
"type": "TSEnumDeclaration",
},
"childScopes": Array [],
"functionExpressionScope": false,
"isStrict": true,
"references": Array [],
"throughReferences": Array [],
"type": "enum",
"upperScope": Object {
"$ref": 2,
},
"variableMap": Object {},
"variableScope": Object {
"$ref": 2,
},
"variables": Array [],
},
],
"functionExpressionScope": false,
"isStrict": true,
"references": Array [],
"throughReferences": Array [],
"type": "module",
"upperScope": Object {
"$ref": 3,
},
"variableMap": Object {
"Foo": Object {
"$ref": 0,
},
},
"variableScope": Object {
"$ref": 2,
},
"variables": Array [
Object {
"$id": 0,
"defs": Array [
Object {
"name": Object {
"name": "Foo",
"range": Array [
12,
15,
],
"type": "Identifier",
},
"node": Object {
"range": Array [
7,
41,
],
"type": "TSEnumDeclaration",
},
"parent": undefined,
"type": "EnumName",
},
],
"eslintUsed": undefined,
"identifiers": Array [
Object {
"name": "Foo",
"range": Array [
12,
15,
],
"type": "Identifier",
},
],
"name": "Foo",
"references": Array [],
"scope": Object {
"$ref": 2,
},
},
],
},
],
"functionExpressionScope": false,
"isStrict": false,
"references": Array [],
"throughReferences": Array [],
"type": "global",
"upperScope": null,
"variableMap": Object {},
"variableScope": Object {
"$ref": 3,
},
"variables": Array [],
}
`;

exports[`typescript fixtures/basics/export-type-alias-declaration.src 1`] = `
Object {
"$id": 1,
Expand Down
@@ -0,0 +1,4 @@
export enum Foo {
['baz'],
[1],
}
3 changes: 3 additions & 0 deletions packages/typescript-estree/src/convert.ts
Expand Up @@ -2489,6 +2489,9 @@ export class Converter {
if (node.initializer) {
result.initializer = this.convertChild(node.initializer);
}
if (node.name.kind === ts.SyntaxKind.ComputedPropertyName) {
result.computed = true;
}
return result;
}

Expand Down
76 changes: 39 additions & 37 deletions packages/typescript-estree/src/semantic-or-syntactic-errors.ts
Expand Up @@ -56,43 +56,45 @@ function whitelistSupportedDiagnostics(
): readonly (ts.DiagnosticWithLocation | ts.Diagnostic)[] {
return diagnostics.filter(diagnostic => {
switch (diagnostic.code) {
case 1013: // ts 3.2 "A rest parameter or binding pattern may not have a trailing comma."
case 1014: // ts 3.2 "A rest parameter must be last in a parameter list."
case 1044: // ts 3.2 "'{0}' modifier cannot appear on a module or namespace element."
case 1045: // ts 3.2 "A '{0}' modifier cannot be used with an interface declaration."
case 1048: // ts 3.2 "A rest parameter cannot have an initializer."
case 1049: // ts 3.2 "A 'set' accessor must have exactly one parameter."
case 1070: // ts 3.2 "'{0}' modifier cannot appear on a type member."
case 1071: // ts 3.2 "'{0}' modifier cannot appear on an index signature."
case 1085: // ts 3.2 "Octal literals are not available when targeting ECMAScript 5 and higher. Use the syntax '{0}'."
case 1090: // ts 3.2 "'{0}' modifier cannot appear on a parameter."
case 1096: // ts 3.2 "An index signature must have exactly one parameter."
case 1097: // ts 3.2 "'{0}' list cannot be empty."
case 1098: // ts 3.3 "Type parameter list cannot be empty."
case 1099: // ts 3.3 "Type argument list cannot be empty."
case 1117: // ts 3.2 "An object literal cannot have multiple properties with the same name in strict mode."
case 1121: // ts 3.2 "Octal literals are not allowed in strict mode."
case 1123: // ts 3.2: "Variable declaration list cannot be empty."
case 1141: // ts 3.2 "String literal expected."
case 1162: // ts 3.2 "An object member cannot be declared optional."
case 1172: // ts 3.2 "'extends' clause already seen."
case 1173: // ts 3.2 "'extends' clause must precede 'implements' clause."
case 1175: // ts 3.2 "'implements' clause already seen."
case 1176: // ts 3.2 "Interface declaration cannot have 'implements' clause."
case 1190: // ts 3.2 "The variable declaration of a 'for...of' statement cannot have an initializer."
case 1200: // ts 3.2 "Line terminator not permitted before arrow."
case 1206: // ts 3.2 "Decorators are not valid here."
case 1211: // ts 3.2 "A class declaration without the 'default' modifier must have a name."
case 1242: // ts 3.2 "'abstract' modifier can only appear on a class, method, or property declaration."
case 1246: // ts 3.2 "An interface property cannot have an initializer."
case 1255: // ts 3.2 "A definite assignment assertion '!' is not permitted in this context."
case 1308: // ts 3.2 "'await' expression is only allowed within an async function."
case 2364: // ts 3.2 "The left-hand side of an assignment expression must be a variable or a property access."
case 2369: // ts 3.2 "A parameter property is only allowed in a constructor implementation."
case 2462: // ts 3.2 "A rest element must be last in a destructuring pattern."
case 8017: // ts 3.2 "Octal literal types must use ES2015 syntax. Use the syntax '{0}'."
case 17012: // ts 3.2 "'{0}' is not a valid meta-property for keyword '{1}'. Did you mean '{2}'?"
case 17013: // ts 3.2 "Meta-property '{0}' is only allowed in the body of a function declaration, function expression, or constructor."
case 1013: // "A rest parameter or binding pattern may not have a trailing comma."
case 1014: // "A rest parameter must be last in a parameter list."
case 1044: // "'{0}' modifier cannot appear on a module or namespace element."
case 1045: // "A '{0}' modifier cannot be used with an interface declaration."
case 1048: // "A rest parameter cannot have an initializer."
case 1049: // "A 'set' accessor must have exactly one parameter."
case 1070: // "'{0}' modifier cannot appear on a type member."
case 1071: // "'{0}' modifier cannot appear on an index signature."
case 1085: // "Octal literals are not available when targeting ECMAScript 5 and higher. Use the syntax '{0}'."
case 1090: // "'{0}' modifier cannot appear on a parameter."
case 1096: // "An index signature must have exactly one parameter."
case 1097: // "'{0}' list cannot be empty."
case 1098: // "Type parameter list cannot be empty."
case 1099: // "Type argument list cannot be empty."
case 1117: // "An object literal cannot have multiple properties with the same name in strict mode."
case 1121: // "Octal literals are not allowed in strict mode."
case 1123: // "Variable declaration list cannot be empty."
case 1141: // "String literal expected."
case 1162: // "An object member cannot be declared optional."
case 1164: // "Computed property names are not allowed in enums."
bradzacher marked this conversation as resolved.
Show resolved Hide resolved
case 1172: // "'extends' clause already seen."
case 1173: // "'extends' clause must precede 'implements' clause."
case 1175: // "'implements' clause already seen."
case 1176: // "Interface declaration cannot have 'implements' clause."
case 1190: // "The variable declaration of a 'for...of' statement cannot have an initializer."
case 1200: // "Line terminator not permitted before arrow."
case 1206: // "Decorators are not valid here."
case 1211: // "A class declaration without the 'default' modifier must have a name."
case 1242: // "'abstract' modifier can only appear on a class, method, or property declaration."
case 1246: // "An interface property cannot have an initializer."
case 1255: // "A definite assignment assertion '!' is not permitted in this context."
case 1308: // "'await' expression is only allowed within an async function."
case 2364: // "The left-hand side of an assignment expression must be a variable or a property access."
case 2369: // "A parameter property is only allowed in a constructor implementation."
case 2452: // "An enum member cannot have a numeric name."
bradzacher marked this conversation as resolved.
Show resolved Hide resolved
case 2462: // "A rest element must be last in a destructuring pattern."
case 8017: // "Octal literal types must use ES2015 syntax. Use the syntax '{0}'."
case 17012: // "'{0}' is not a valid meta-property for keyword '{1}'. Did you mean '{2}'?"
case 17013: // "Meta-property '{0}' is only allowed in the body of a function declaration, function expression, or constructor."
armano2 marked this conversation as resolved.
Show resolved Hide resolved
return true;
}
return false;
Expand Down