Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions rules/catch-error-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@ const isPromiseCatchParameter = node =>
method: 'then',
argumentsLength: 2,
optionalCall: false,
optionalMember: false,
})
|| isMethodCall(node.parent.parent, {
method: 'catch',
argumentsLength: 1,
optionalCall: false,
optionalMember: false,
})
)
&& node.parent.parent.arguments.at(-1) === node.parent;
Expand Down
3 changes: 1 addition & 2 deletions rules/import-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,7 @@ const create = context => {
isCallExpression(node, {
name: 'require',
argumentsLength: 1,
optionalCall: false,
optionalMember: false,
optional: false,
})
&& (node.parent.type === 'ExpressionStatement' && node.parent.expression === node)
)) {
Expand Down
1 change: 0 additions & 1 deletion rules/no-array-method-this-argument.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ const create = context => {
],
argumentsLength: 2,
optionalCall: false,
optionalMember: false,
})
|| isNodeMatches(callExpression.callee, ignored)
|| isNodeValueNotFunction(callExpression.arguments[0])
Expand Down
1 change: 0 additions & 1 deletion rules/no-invalid-remove-event-listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const create = context => ({
method: 'removeEventListener',
minimumArguments: 2,
optionalCall: false,
optionalMember: false,
})
&& callExpression.arguments[0].type !== 'SpreadElement'
&& (
Expand Down
1 change: 0 additions & 1 deletion rules/no-null.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ const create = context => {
method: 'insertBefore',
argumentsLength: 2,
optionalCall: false,
optionalMember: false,
})
&& node.parent.arguments[1] === node
)
Expand Down
16 changes: 11 additions & 5 deletions rules/prefer-array-find.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ const messages = {
[SUGGESTION_LOGICAL_OR_OPERATOR]: 'Replace `.filter(…)` with `.find(…) || …`.',
};

const isArrayFilterCall = node => isMethodCall(node, {
const isArrayFilterCall = (node, options) => isMethodCall(node, {
method: 'filter',
minimumArguments: 1,
maximumArguments: 2,
optionalCall: false,
optionalMember: false,
...options,
});

// Need add `()` to the `AssignmentExpression`
Expand Down Expand Up @@ -184,6 +184,8 @@ const create = context => {
};

// Zero index access
// `array.filter()[0]`
// `array?.filter()[0]`
context.on('MemberExpression', node => {
if (!(
node.computed
Expand All @@ -206,6 +208,7 @@ const create = context => {
});

// `array.filter().shift()`
// `array?.filter().shift()`
context.on('CallExpression', node => {
if (!(
isMethodCall(node, {
Expand Down Expand Up @@ -236,7 +239,7 @@ const create = context => {
&& node.id.elements.length === 1
&& node.id.elements[0]
&& node.id.elements[0].type !== 'RestElement'
&& isArrayFilterCall(node.init)
&& isArrayFilterCall(node.init, {optionalMember: false})
)) {
return;
}
Expand All @@ -255,7 +258,7 @@ const create = context => {
&& node.left.elements.length === 1
&& node.left.elements[0]
&& node.left.elements[0].type !== 'RestElement'
&& isArrayFilterCall(node.right)
&& isArrayFilterCall(node.right, {optionalMember: false})
)) {
return;
}
Expand All @@ -271,7 +274,7 @@ const create = context => {
context.on('VariableDeclarator', node => {
if (!(
node.id.type === 'Identifier'
&& isArrayFilterCall(node.init)
&& isArrayFilterCall(node.init, {optionalMember: false})
&& node.parent.type === 'VariableDeclaration'
&& node.parent.declarations.includes(node)
// Exclude `export const foo = [];`
Expand Down Expand Up @@ -337,6 +340,7 @@ const create = context => {
});

// `array.filter().at(0)`
// `array?.filter().at(0)`
context.on('CallExpression', node => {
if (!(
isMethodCall(node, {
Expand Down Expand Up @@ -367,6 +371,7 @@ const create = context => {
}

// `array.filter().pop()`
// `array?.filter().pop()`
context.on('CallExpression', node => {
if (!(
isMethodCall(node, {
Expand All @@ -391,6 +396,7 @@ const create = context => {
});

// `array.filter().at(-1)`
// `array?.filter().at(-1)`
context.on('CallExpression', node => {
if (!(
isMethodCall(node, {
Expand Down
1 change: 0 additions & 1 deletion rules/prefer-array-flat-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const create = context => ({
&& isMethodCall(callExpression.callee.object, {
method: 'map',
optionalCall: false,
optionalMember: false,
})
)) {
return;
Expand Down
15 changes: 9 additions & 6 deletions rules/prefer-array-flat.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
method: 'flatMap',
argumentsLength: 1,
optionalCall: false,
optionalMember: false,
})) {
return false;
}
Expand All @@ -41,18 +40,20 @@
);
},
getArrayNode: node => node.callee.object,
isOptionalArray: node => node.callee.optional,
description: 'Array#flatMap()',
};

// `array.reduce((a, b) => a.concat(b), [])`
// `array?.reduce((a, b) => a.concat(b), [])`
// `array.reduce((a, b) => [...a, ...b], [])`
// `array?.reduce((a, b) => [...a, ...b], [])`
const arrayReduce = {
testFunction(node) {
if (!isMethodCall(node, {
method: 'reduce',
argumentsLength: 2,
optionalCall: false,
optionalMember: false,
})) {
return false;
}
Expand Down Expand Up @@ -94,6 +95,7 @@
);
},
getArrayNode: node => node.callee.object,
isOptionalArray: node => node.callee.optional,
description: 'Array#reduce()',
};

Expand Down Expand Up @@ -159,7 +161,7 @@
'underscore.flatten',
];

function fix(node, array, sourceCode, shouldSwitchToArray) {
function fix(node, array, sourceCode, shouldSwitchToArray, optional) {

Check warning on line 164 in rules/prefer-array-flat.js

View workflow job for this annotation

GitHub Actions / lint-test (ubuntu-latest)

Function 'fix' has too many parameters (5). Maximum allowed is 4

Check warning on line 164 in rules/prefer-array-flat.js

View workflow job for this annotation

GitHub Actions / lint-test (windows-latest)

Function 'fix' has too many parameters (5). Maximum allowed is 4
if (typeof shouldSwitchToArray === 'function') {
shouldSwitchToArray = shouldSwitchToArray(node);
}
Expand All @@ -177,7 +179,7 @@
fixed = `(${fixed})`;
}

fixed = `${fixed}.flat()`;
fixed = `${fixed}${optional ? '?' : ''}.flat()`;

const tokenBefore = sourceCode.getTokenBefore(node);
if (needsSemicolon(tokenBefore, sourceCode, fixed)) {
Expand Down Expand Up @@ -214,12 +216,13 @@

return {
* CallExpression(node) {
for (const {testFunction, description, getArrayNode, shouldSwitchToArray} of cases) {
for (const {testFunction, description, getArrayNode, shouldSwitchToArray, isOptionalArray} of cases) {
if (!testFunction(node)) {
continue;
}

const array = getArrayNode(node);
const optional = isOptionalArray?.(node);

const data = {
description: typeof description === 'string' ? description : description(node),
Expand All @@ -238,7 +241,7 @@
sourceCode.getCommentsInside(node).length
=== sourceCode.getCommentsInside(array).length
) {
problem.fix = fix(node, array, sourceCode, shouldSwitchToArray);
problem.fix = fix(node, array, sourceCode, shouldSwitchToArray, optional);
}

yield problem;
Expand Down
8 changes: 3 additions & 5 deletions rules/prefer-at.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ function create(context) {
// Index access
context.on('MemberExpression', node => {
if (
node.optional
|| !node.computed
!node.computed
|| isLeftHandSide(node)
) {
return;
Expand Down Expand Up @@ -202,8 +201,9 @@ function create(context) {
}
}

const isOptional = node.optional;
const openingBracketToken = sourceCode.getTokenBefore(indexNode, isOpeningBracketToken);
yield fixer.replaceText(openingBracketToken, '.at(');
yield fixer.replaceText(openingBracketToken, `${isOptional ? '' : '.'}at(`);

const closingBracketToken = sourceCode.getTokenAfter(indexNode, isClosingBracketToken);
yield fixer.replaceText(closingBracketToken, ')');
Expand All @@ -218,7 +218,6 @@ function create(context) {
method: 'charAt',
argumentsLength: 1,
optionalCall: false,
optionalMember: false,
})) {
return;
}
Expand Down Expand Up @@ -254,7 +253,6 @@ function create(context) {
minimumArguments: 1,
maximumArguments: 2,
optionalCall: false,
optionalMember: false,
})) {
return;
}
Expand Down
1 change: 0 additions & 1 deletion rules/prefer-code-point.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const getReplacement = node => {
if (isMethodCall(node, {
method: 'charCodeAt',
optionalCall: false,
optionalMember: false,
})) {
return 'codePointAt';
}
Expand Down
9 changes: 7 additions & 2 deletions rules/prefer-dom-node-dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
const method = callExpression.callee.property.name;

// `foo?.bar = ''` is invalid
// TODO: Remove this restriction if https://github.com/nicolo-ribaudo/ecma262/pull/4 get merged

Check warning on line 17 in rules/prefer-dom-node-dataset.js

View workflow job for this annotation

GitHub Actions / lint-test (ubuntu-latest)

Unexpected 'todo' comment: 'TODO: Remove this restriction if...'

Check warning on line 17 in rules/prefer-dom-node-dataset.js

View workflow job for this annotation

GitHub Actions / lint-test (windows-latest)

Unexpected 'todo' comment: 'TODO: Remove this restriction if...'
if (method === 'setAttribute' && hasOptionalChainElement(callExpression.callee)) {
return;
}
Expand All @@ -33,7 +33,7 @@
const name = dashToCamelCase(nameNode.value.toLowerCase().slice(5));
const {sourceCode} = context;
let text = '';
const datasetText = `${sourceCode.getText(callExpression.callee.object)}.dataset`;
const datasetText = `${sourceCode.getText(callExpression.callee.object)}${callExpression.callee.optional ? '?' : ''}.dataset`;
switch (method) {
case 'setAttribute':
case 'getAttribute':
Expand Down Expand Up @@ -76,11 +76,16 @@
optionalMember: false,
})
|| isMethodCall(callExpression, {
methods: ['getAttribute', 'removeAttribute', 'hasAttribute'],
methods: ['removeAttribute', 'hasAttribute'],
argumentsLength: 1,
optionalCall: false,
optionalMember: false,
})
|| isMethodCall(callExpression, {
method: 'getAttribute',
argumentsLength: 1,
optionalCall: false,
})
)
&& isStringLiteral(callExpression.arguments[0])
)) {
Expand Down
1 change: 0 additions & 1 deletion rules/prefer-string-replace-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ const create = context => ({
methods: ['replace', 'replaceAll'],
argumentsLength: 2,
optionalCall: false,
optionalMember: false,
})) {
return;
}
Expand Down
1 change: 0 additions & 1 deletion rules/require-number-to-fixed-digits-argument.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const create = context => ({
method: 'toFixed',
argumentsLength: 0,
optionalCall: false,
optionalMember: false,
})
|| node.callee.object.type === 'NewExpression'
) {
Expand Down
1 change: 0 additions & 1 deletion rules/require-post-message-target-origin.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ function create(context) {
method: 'postMessage',
argumentsLength: 1,
optionalCall: false,
optionalMember: false,
})) {
return;
}
Expand Down
1 change: 0 additions & 1 deletion rules/shared/simple-array-search-rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ export default function simpleArraySearchRule({method, replacement}) {
method,
argumentsLength: 1,
optionalCall: false,
optionalMember: false,
})
|| !isSimpleCompareCallbackFunction(callExpression.arguments[0])
) {
Expand Down
14 changes: 14 additions & 0 deletions test/catch-error-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,11 @@
}
`,
'obj.catch(error => {})',
'obj.catch?.(error => {})',
'obj.then(undefined, error => {})',
'obj.then(result => {}, error => {})',
'obj.then?.(undefined, error => {})',
'obj.then?.(result => {}, error => {})',
outdent`
const handleError = error => {
obj.catch(error_ => { });
Expand Down Expand Up @@ -321,6 +324,11 @@
output: 'obj.catch(error => error)',
errors: [generateError('err', 'error')],
}),
invalidTestCase({
code: 'obj?.catch(err => err)',
output: 'obj?.catch(error => error)',
errors: [generateError('err', 'error')],
}),
invalidTestCase({
code: 'obj.then(undefined, err => err)',
output: 'obj.then(undefined, error => error)',
Expand All @@ -338,6 +346,12 @@
name: 'err',
errors: [generateError('error', 'err')],
}),
invalidTestCase({
code: 'obj?.then(undefined, error => error.stack)',
output: 'obj?.then(undefined, err => err.stack)',
name: 'err',
errors: [generateError('error', 'err')],
}),
invalidTestCase({
code: outdent`
obj.catch(function (err) {
Expand All @@ -364,7 +378,7 @@
`,
errors: [generateError('err', 'error')],
}),
// TODO: this could fix to `error`

Check warning on line 381 in test/catch-error-name.js

View workflow job for this annotation

GitHub Actions / lint-test (ubuntu-latest)

Unexpected 'todo' comment: 'TODO: this could fix to `error`'

Check warning on line 381 in test/catch-error-name.js

View workflow job for this annotation

GitHub Actions / lint-test (windows-latest)

Unexpected 'todo' comment: 'TODO: this could fix to `error`'
invalidTestCase({
code: outdent`
obj.then(
Expand Down
5 changes: 4 additions & 1 deletion test/no-array-method-this-argument.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ test.snapshot({
'array.unknownMethod(() => {}, thisArgument)',
'new array.map(() => {}, thisArgument)',
'array.map?.(() => {}, thisArgument)',
'array?.map(() => {}, thisArgument)',
'Array.unknownMethod(iterableOrArrayLike, () => {}, thisArgument)',
'new Array.from(iterableOrArrayLike, () => {}, thisArgument)',
'Array.from?.(iterableOrArrayLike, () => {}, thisArgument)',
Expand All @@ -25,6 +24,7 @@ test.snapshot({
'array.map(() => {}, ...thisArgument)',
'array.map(...() => {}, thisArgument)',
'array.map(() => {}, thisArgument, extraArgument)',
'Array?.from(iterableOrArrayLike, () => {}, thisArgument)',
'Array.from()',
'Array.from(iterableOrArrayLike)',
'Array.from(iterableOrArrayLike, () => {},)',
Expand All @@ -39,6 +39,7 @@ test.snapshot({
'Array.fromAsync(iterableOrArrayLike, ...() => {}, thisArgument)',
'Array.fromAsync(...iterableOrArrayLike, () => {}, thisArgument)',
'Array.fromAsync(iterableOrArrayLike, () => {}, thisArgument, extraArgument)',
'Array?.fromAsync(iterableOrArrayLike, () => {}, thisArgument)',

// Ignored
'lodash.every(array, () => {})',
Expand Down Expand Up @@ -79,6 +80,7 @@ test.snapshot({
'array.flatMap(() => {}, thisArgument)',
'array.forEach(() => {}, thisArgument)',
'array.map(() => {}, thisArgument)',
'array?.map(() => {}, thisArgument)',
'Array.from(iterableOrArrayLike, () => {}, thisArgument)',
'Array.fromAsync(iterableOrArrayLike, () => {}, thisArgument)',
// Comma
Expand All @@ -88,6 +90,7 @@ test.snapshot({
'Array.fromAsync(iterableOrArrayLike, () => {}, thisArgument,)',
// Side effect
'array.map(() => {}, thisArgumentHasSideEffect())',
'array?.map(() => {}, thisArgumentHasSideEffect())',
'Array.from(iterableOrArrayLike, () => {}, thisArgumentHasSideEffect())',
'Array.fromAsync(iterableOrArrayLike, () => {}, thisArgumentHasSideEffect())',
],
Expand Down
Loading
Loading