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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: produce valid code when when fixing properties accessed with square brackets #1131

Merged
merged 6 commits into from
May 29, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: use helper for wrapping text in quotes
  • Loading branch information
G-Rath committed May 29, 2022
commit 395ff634e2522a247921d4235636c9f1391c4a90
15 changes: 7 additions & 8 deletions src/rules/no-alias-methods.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import { createRule, isExpectCall, parseExpectCall } from './utils';
import {
createRule,
isExpectCall,
parseExpectCall,
replaceAccessorFixer,
} from './utils';

export default createRule({
name: __filename,
Expand Down Expand Up @@ -58,12 +62,7 @@ export default createRule({
},
node: matcher.node.property,
fix: fixer => [
fixer.replaceText(
matcher.node.property,
matcher.node.property.type === AST_NODE_TYPES.Identifier
? canonical
: `'${canonical}'`,
),
replaceAccessorFixer(fixer, matcher.node.property, canonical),
],
});
}
Expand Down
9 changes: 4 additions & 5 deletions src/rules/prefer-strict-equal.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import {
EqualityMatcher,
createRule,
isExpectCall,
isParsedEqualityMatcherCall,
parseExpectCall,
replaceAccessorFixer,
} from './utils';

export default createRule({
Expand Down Expand Up @@ -45,11 +45,10 @@ export default createRule({
{
messageId: 'suggestReplaceWithStrictEqual',
fix: fixer => [
fixer.replaceText(
replaceAccessorFixer(
fixer,
matcher.node.property,
matcher.node.property.type === AST_NODE_TYPES.Identifier
? EqualityMatcher.toStrictEqual
: `'${EqualityMatcher.toStrictEqual}'`,
EqualityMatcher.toStrictEqual,
),
],
},
Expand Down
9 changes: 4 additions & 5 deletions src/rules/prefer-to-be.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
isIdentifier,
isParsedEqualityMatcherCall,
parseExpectCall,
replaceAccessorFixer,
} from './utils';

const isNullLiteral = (node: TSESTree.Node): node is TSESTree.NullLiteral =>
Expand Down Expand Up @@ -69,11 +70,9 @@ const reportPreferToBe = (
context.report({
messageId: `useToBe${whatToBe}`,
fix(fixer) {
const r = `toBe${whatToBe}`;
const text =
matcher.node.property.type === AST_NODE_TYPES.Identifier ? r : `'${r}'`;

const fixes = [fixer.replaceText(matcher.node.property, text)];
const fixes = [
replaceAccessorFixer(fixer, matcher.node.property, `toBe${whatToBe}`),
];

if (matcher.arguments?.length && whatToBe !== '') {
fixes.push(fixer.remove(matcher.arguments[0]));
Expand Down
8 changes: 2 additions & 6 deletions src/rules/prefer-todo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
isFunction,
isStringNode,
parseJestFnCall,
replaceAccessorFixer,
} from './utils';

function isEmptyFunction(node: TSESTree.CallExpressionArgument) {
Expand All @@ -24,12 +25,7 @@ function createTodoFixer(
fixer: TSESLint.RuleFixer,
) {
if (jestFnCall.members.length) {
const text =
jestFnCall.members[0].type === AST_NODE_TYPES.Identifier
? 'todo'
: "'todo'";

return [fixer.replaceText(jestFnCall.members[0], text)];
return [replaceAccessorFixer(fixer, jestFnCall.members[0], 'todo')];
}

return [
Expand Down
17 changes: 17 additions & 0 deletions src/rules/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,20 @@ export const getTestCallExpressionsFromDeclaredVariables = (
[],
);
};

/**
* Replaces an accessor node with the given `text`, surrounding it in quotes if required.
*
* This ensures that fixes produce valid code when replacing both dot-based and
* bracket-based property accessors.
*/
export const replaceAccessorFixer = (
fixer: TSESLint.RuleFixer,
node: AccessorNode,
text: string,
) => {
return fixer.replaceText(
node,
node.type === AST_NODE_TYPES.Identifier ? text : `'${text}'`,
);
};