Skip to content

Commit

Permalink
feat(typescript-estree): computed members discriminated unions (#1349)
Browse files Browse the repository at this point in the history
  • Loading branch information
bradzacher committed Dec 19, 2019
1 parent bf4ad5b commit 013df9a
Show file tree
Hide file tree
Showing 15 changed files with 3,782 additions and 1,823 deletions.
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,
};

0 comments on commit 013df9a

Please sign in to comment.