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

chore(eslint-plugin): add basic parent node type augmentation #1416

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
5 changes: 2 additions & 3 deletions packages/eslint-plugin/src/rules/array-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ export default util.createRule<Options, MessageIds>({
}

const isReadonly =
node.parent &&
node.parent.type === AST_NODE_TYPES.TSTypeOperator &&
node.parent.operator === 'readonly';

Expand All @@ -215,10 +214,10 @@ export default util.createRule<Options, MessageIds>({
defaultOption === 'generic'
? 'errorStringGeneric'
: 'errorStringGenericSimple';
const typeOpNode = isReadonly ? node.parent! : null;
const typeOpNode = isReadonly ? node.parent : null;

context.report({
node: isReadonly ? node.parent! : node,
node: isReadonly ? node.parent : node,
messageId,
data: {
type: getMessageType(node.elementType),
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/rules/camelcase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export default util.createRule<Options, MessageIds>({

// Check TypeScript specific nodes
const parent = node.parent;
if (parent && isTSPropertyType(parent)) {
if (isTSPropertyType(parent)) {
if (properties === 'always' && isUnderscored(name)) {
report(node);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ export default util.createRule<Options, MessageIds>({

if (
options.objectLiteralTypeAssertions === 'allow-as-parameter' &&
node.parent &&
(node.parent.type === AST_NODE_TYPES.NewExpression ||
node.parent.type === AST_NODE_TYPES.CallExpression ||
node.parent.type === AST_NODE_TYPES.OptionalCallExpression ||
Expand Down
37 changes: 17 additions & 20 deletions packages/eslint-plugin/src/rules/explicit-function-return-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,32 +315,29 @@ export default util.createRule<Options, MessageIds>({
function checkFunctionExpressionReturnType(
node: TSESTree.ArrowFunctionExpression | TSESTree.FunctionExpression,
): void {
// Should always have a parent; checking just in case
/* istanbul ignore else */ if (node.parent) {
if (options.allowTypedFunctionExpressions) {
if (
util.isTypeAssertion(node.parent) ||
isVariableDeclaratorWithTypeAnnotation(node.parent) ||
isClassPropertyWithTypeAnnotation(node.parent) ||
isPropertyOfObjectWithType(node.parent) ||
isFunctionArgument(node.parent, node) ||
isConstructorArgument(node.parent)
) {
return;
}
}

if (options.allowTypedFunctionExpressions) {
if (
options.allowExpressions &&
node.parent.type !== AST_NODE_TYPES.VariableDeclarator &&
node.parent.type !== AST_NODE_TYPES.MethodDefinition &&
node.parent.type !== AST_NODE_TYPES.ExportDefaultDeclaration &&
node.parent.type !== AST_NODE_TYPES.ClassProperty
util.isTypeAssertion(node.parent) ||
isVariableDeclaratorWithTypeAnnotation(node.parent) ||
isClassPropertyWithTypeAnnotation(node.parent) ||
isPropertyOfObjectWithType(node.parent) ||
isFunctionArgument(node.parent, node) ||
isConstructorArgument(node.parent)
) {
return;
}
}

if (
options.allowExpressions &&
node.parent.type !== AST_NODE_TYPES.VariableDeclarator &&
node.parent.type !== AST_NODE_TYPES.MethodDefinition &&
node.parent.type !== AST_NODE_TYPES.ExportDefaultDeclaration &&
node.parent.type !== AST_NODE_TYPES.ClassProperty
) {
return;
}

// https://github.com/typescript-eslint/typescript-eslint/issues/653
if (
node.type === AST_NODE_TYPES.ArrowFunctionExpression &&
Expand Down
12 changes: 4 additions & 8 deletions packages/eslint-plugin/src/rules/indent-new-do-not-use/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -931,18 +931,14 @@ export default createRule<Options, MessageIds>({
) {
let blockIndentLevel;

if (node.parent && isOuterIIFE(node.parent)) {
if (isOuterIIFE(node.parent)) {
blockIndentLevel = options.outerIIFEBody;
} else if (
node.parent &&
(node.parent.type === AST_NODE_TYPES.FunctionExpression ||
node.parent.type === AST_NODE_TYPES.ArrowFunctionExpression)
node.parent.type === AST_NODE_TYPES.FunctionExpression ||
node.parent.type === AST_NODE_TYPES.ArrowFunctionExpression
) {
blockIndentLevel = options.FunctionExpression.body;
} else if (
node.parent &&
node.parent.type === AST_NODE_TYPES.FunctionDeclaration
) {
} else if (node.parent.type === AST_NODE_TYPES.FunctionDeclaration) {
blockIndentLevel = options.FunctionDeclaration.body;
} else {
blockIndentLevel = 1;
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin/src/rules/indent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ export default util.createRule<Options, MessageIds>({
operator: 'extends',
left: node.checkType as any,
right: node.extendsType as any,
parent: node,

// location data
range: [node.checkType.range[0], node.extendsType.range[1]],
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/rules/no-empty-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export default util.createRule<Options, MessageIds>({
const parent = node.parent;
if (
isBodyEmpty(node) &&
parent?.type === AST_NODE_TYPES.MethodDefinition &&
parent.type === AST_NODE_TYPES.MethodDefinition &&
parent.kind === 'constructor'
) {
const { accessibility } = parent;
Expand Down
6 changes: 3 additions & 3 deletions packages/eslint-plugin/src/rules/no-magic-numbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export default util.createRule<Options, MessageIds>({
let raw = node.raw;

if (
node.parent?.type === AST_NODE_TYPES.UnaryExpression &&
node.parent.type === AST_NODE_TYPES.UnaryExpression &&
// the base rule only shows the operator for negative numbers
// https://github.com/eslint/eslint/blob/9dfc8501fb1956c90dc11e6377b4cb38a6bea65d/lib/rules/no-magic-numbers.js#L126
node.parent.operator === '-'
Expand Down Expand Up @@ -114,9 +114,9 @@ export default util.createRule<Options, MessageIds>({
/**
* Gets the true parent of the literal, handling prefixed numbers (-1 / +1)
*/
function getLiteralParent(node: TSESTree.Literal): TSESTree.Node | undefined {
function getLiteralParent(node: TSESTree.Literal): TSESTree.Node {
if (
node.parent?.type === AST_NODE_TYPES.UnaryExpression &&
node.parent.type === AST_NODE_TYPES.UnaryExpression &&
['-', '+'].includes(node.parent.operator)
) {
return node.parent.parent;
Expand Down
7 changes: 2 additions & 5 deletions packages/eslint-plugin/src/rules/no-misused-new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export default util.createRule({
): void {
if (
isMatchingParentType(
node.parent!.parent as TSESTree.TSInterfaceDeclaration,
node.parent.parent as TSESTree.TSInterfaceDeclaration,
node.returnType,
)
) {
Expand All @@ -95,10 +95,7 @@ export default util.createRule({
node: TSESTree.MethodDefinition,
): void {
if (node.value.type === AST_NODE_TYPES.TSEmptyBodyFunctionExpression) {
if (
node.parent &&
isMatchingParentType(node.parent.parent, node.value.returnType)
) {
if (isMatchingParentType(node.parent.parent, node.value.returnType)) {
context.report({
node,
messageId: 'errorMessageClass',
Expand Down
3 changes: 1 addition & 2 deletions packages/eslint-plugin/src/rules/no-namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ export default util.createRule<Options, MessageIds>({
node: TSESTree.TSModuleDeclaration,
): void {
if (
(node.parent &&
node.parent.type === AST_NODE_TYPES.TSModuleDeclaration) ||
node.parent.type === AST_NODE_TYPES.TSModuleDeclaration ||
(allowDefinitionFiles && util.isDefinitionFile(filename)) ||
(allowDeclarations && node.declare === true)
) {
Expand Down
82 changes: 40 additions & 42 deletions packages/eslint-plugin/src/rules/no-non-null-assertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,60 +58,58 @@ export default util.createRule<[], MessageIds>({
};
}

if (node.parent) {
if (
(node.parent.type === AST_NODE_TYPES.MemberExpression ||
node.parent.type === AST_NODE_TYPES.OptionalMemberExpression) &&
node.parent.object === node
) {
if (!node.parent.optional) {
if (node.parent.computed) {
// it is x![y]?.z
suggest.push({
messageId: 'suggestOptionalChain',
fix: convertTokenToOptional('?.'),
});
} else {
// it is x!.y?.z
suggest.push({
messageId: 'suggestOptionalChain',
fix: convertTokenToOptional('?'),
});
}
if (
(node.parent.type === AST_NODE_TYPES.MemberExpression ||
node.parent.type === AST_NODE_TYPES.OptionalMemberExpression) &&
node.parent.object === node
) {
if (!node.parent.optional) {
if (node.parent.computed) {
// it is x![y]?.z
suggest.push({
messageId: 'suggestOptionalChain',
fix: convertTokenToOptional('?.'),
});
} else {
if (node.parent.computed) {
// it is x!?.[y].z
suggest.push({
messageId: 'suggestOptionalChain',
fix: removeToken(),
});
} else {
// it is x!?.y.z
suggest.push({
messageId: 'suggestOptionalChain',
fix: removeToken(),
});
}
// it is x!.y?.z
suggest.push({
messageId: 'suggestOptionalChain',
fix: convertTokenToOptional('?'),
});
}
} else if (
(node.parent.type === AST_NODE_TYPES.CallExpression ||
node.parent.type === AST_NODE_TYPES.OptionalCallExpression) &&
node.parent.callee === node
) {
if (!node.parent.optional) {
// it is x.y?.z!()
} else {
if (node.parent.computed) {
// it is x!?.[y].z
suggest.push({
messageId: 'suggestOptionalChain',
fix: convertTokenToOptional('?.'),
fix: removeToken(),
});
} else {
// it is x.y.z!?.()
// it is x!?.y.z
suggest.push({
messageId: 'suggestOptionalChain',
fix: removeToken(),
});
}
}
} else if (
(node.parent.type === AST_NODE_TYPES.CallExpression ||
node.parent.type === AST_NODE_TYPES.OptionalCallExpression) &&
node.parent.callee === node
) {
if (!node.parent.optional) {
// it is x.y?.z!()
suggest.push({
messageId: 'suggestOptionalChain',
fix: convertTokenToOptional('?.'),
});
} else {
// it is x.y.z!?.()
suggest.push({
messageId: 'suggestOptionalChain',
fix: removeToken(),
});
}
}

context.report({
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/rules/no-require-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default util.createRule({
node: TSESTree.Identifier,
): void {
context.report({
node: node.parent!,
node: node.parent,
messageId: 'noRequireImports',
});
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,6 @@ export default util.createRule<Options, MessageIds>({
node: ts.Expression,
): ts.Type | undefined {
const parent = node.parent;
if (!parent) {
return;
}

if (isCallExpression(parent) || isNewExpression(parent)) {
if (node === parent.expression) {
Expand Down
1 change: 0 additions & 1 deletion packages/eslint-plugin/src/rules/no-var-requires.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export default util.createRule<Options, MessageIds>({
if (
node.callee.type === AST_NODE_TYPES.Identifier &&
node.callee.name === 'require' &&
node.parent &&
(node.parent.type === AST_NODE_TYPES.VariableDeclarator ||
node.parent.type === AST_NODE_TYPES.CallExpression ||
node.parent.type === AST_NODE_TYPES.OptionalCallExpression)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export default util.createRule<Options, MessageIds>({
function* fix(
fixer: TSESLint.RuleFixer,
): IterableIterator<TSESLint.RuleFix> {
if (node.parent && util.isLogicalOrOperator(node.parent)) {
if (util.isLogicalOrOperator(node.parent)) {
// '&&' and '??' operations cannot be mixed without parentheses (e.g. a && b ?? c)
if (
node.left.type === AST_NODE_TYPES.LogicalExpression &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ export default createRule({
].join(', ')](
node: TSESTree.MemberExpression | TSESTree.OptionalMemberExpression,
): void {
let parentNode = node.parent!;
let parentNode = node.parent;
let indexNode: TSESTree.Node | null = null;
if (
parentNode.type === AST_NODE_TYPES.CallExpression ||
Expand All @@ -352,7 +352,7 @@ export default createRule({
if (parentNode.arguments.length === 1) {
indexNode = parentNode.arguments[0];
}
parentNode = parentNode.parent!;
parentNode = parentNode.parent;
} else {
indexNode = node.property;
}
Expand Down Expand Up @@ -398,7 +398,7 @@ export default createRule({
const callNode = node.parent as
| TSESTree.CallExpression
| TSESTree.OptionalCallExpression;
const parentNode = callNode.parent!;
const parentNode = callNode.parent;

if (
callNode.arguments.length !== 1 ||
Expand Down Expand Up @@ -430,10 +430,10 @@ export default createRule({
'BinaryExpression > :matches(CallExpression, OptionalCallExpression).left > :matches(MemberExpression, OptionalMemberExpression).callee[property.name="lastIndexOf"][computed=false]'(
node: TSESTree.MemberExpression | TSESTree.OptionalMemberExpression,
): void {
const callNode = node.parent! as
const callNode = node.parent as
| TSESTree.CallExpression
| TSESTree.OptionalCallExpression;
const parentNode = callNode.parent!;
const parentNode = callNode.parent;

if (
callNode.arguments.length !== 1 ||
Expand Down Expand Up @@ -523,10 +523,10 @@ export default createRule({
].join(', ')](
node: TSESTree.MemberExpression | TSESTree.OptionalMemberExpression,
): void {
const callNode = node.parent! as
const callNode = node.parent as
| TSESTree.CallExpression
| TSESTree.OptionalCallExpression;
const parentNode = callNode.parent!;
const parentNode = callNode.parent;
if (
!isEqualityComparison(parentNode) ||
parentNode.left !== callNode ||
Expand Down
6 changes: 1 addition & 5 deletions packages/eslint-plugin/src/rules/promise-function-async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,7 @@ export default util.createRule<Options, MessageIds>({
'FunctionExpression[async = false]'(
node: TSESTree.FunctionExpression,
): void {
if (
node.parent &&
'kind' in node.parent &&
node.parent.kind === 'method'
) {
if ('kind' in node.parent && node.parent.kind === 'method') {
if (checkMethodDeclarations) {
validateNode(node.parent);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/rules/quotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default util.createRule<Options, MessageIds>({
function isAllowedAsNonBacktick(node: TSESTree.Literal): boolean {
const parent = node.parent;

switch (parent?.type) {
switch (parent.type) {
case AST_NODE_TYPES.TSAbstractMethodDefinition:
case AST_NODE_TYPES.TSMethodSignature:
case AST_NODE_TYPES.TSPropertySignature:
Expand Down