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(eslint-plugin): [no-non-null-assert] add suggestion fixer #1260

Merged
merged 1 commit into from
Nov 30, 2019
Merged
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
98 changes: 97 additions & 1 deletion packages/eslint-plugin/src/rules/no-non-null-assertion.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import {
TSESLint,
AST_NODE_TYPES,
} from '@typescript-eslint/experimental-utils';
import * as util from '../util';

export default util.createRule({
type MessageIds = 'noNonNull' | 'suggestOptionalChain';

export default util.createRule<[], MessageIds>({
name: 'no-non-null-assertion',
meta: {
type: 'problem',
Expand All @@ -12,16 +18,106 @@ export default util.createRule({
},
messages: {
noNonNull: 'Forbidden non-null assertion.',
suggestOptionalChain:
'Consider using the optional chain operator `?.` instead. This operator includes runtime checks, so it is safer than the compile-only non-null assertion operator.',
},
schema: [],
},
defaultOptions: [],
create(context) {
const sourceCode = context.getSourceCode();
return {
TSNonNullExpression(node): void {
const suggest: TSESLint.ReportSuggestionArray<MessageIds> = [];
function convertTokenToOptional(
replacement: '?' | '?.',
): TSESLint.ReportFixFunction {
return (fixer: TSESLint.RuleFixer): TSESLint.RuleFix | null => {
const operator = sourceCode.getTokenAfter(
node.expression,
util.isNonNullAssertionPunctuator,
);
if (operator) {
return fixer.replaceText(operator, replacement);
}

return null;
};
}
function removeToken(): TSESLint.ReportFixFunction {
return (fixer: TSESLint.RuleFixer): TSESLint.RuleFix | null => {
const operator = sourceCode.getTokenAfter(
node.expression,
util.isNonNullAssertionPunctuator,
);
if (operator) {
return fixer.remove(operator);
}

return null;
};
}

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('?'),
});
}
} 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(),
});
}
}
} 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({
node,
messageId: 'noNonNull',
suggest,
});
},
};
Expand Down
15 changes: 14 additions & 1 deletion packages/eslint-plugin/src/util/astUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ function isNotOptionalChainPunctuator(
return !isOptionalChainPunctuator(token);
}

function isNonNullAssertionPunctuator(
token: TSESTree.Token | TSESTree.Comment,
): boolean {
return token.type === AST_TOKEN_TYPES.Punctuator && token.value === '!';
}
function isNotNonNullAssertionPunctuator(
token: TSESTree.Token | TSESTree.Comment,
): boolean {
return !isNonNullAssertionPunctuator(token);
}

/**
* Returns true if and only if the node represents: foo?.() or foo.bar?.()
*/
Expand All @@ -32,8 +43,10 @@ function isOptionalOptionalChain(
}

export {
LINEBREAK_MATCHER,
isNonNullAssertionPunctuator,
isNotNonNullAssertionPunctuator,
isNotOptionalChainPunctuator,
isOptionalChainPunctuator,
isOptionalOptionalChain,
LINEBREAK_MATCHER,
};