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
52 changes: 37 additions & 15 deletions rules/prefer-set-size.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {findVariable} from '@eslint-community/eslint-utils';
import {fixSpaceAroundKeyword} from './fix/index.js';
import {isNewExpression, isMemberExpression} from './ast/index.js';
import {isNewExpression, isMemberExpression, isMethodCall} from './ast/index.js';

const MESSAGE_ID = 'prefer-set-size';
const messages = {
Expand Down Expand Up @@ -36,24 +36,49 @@ function isSet(node, scope) {
&& isNewSet(definition.node.init);
}

// `[...set].length` -> `set.size`
function fix(context, lengthAccessNodes) {
function getSetNode(memberExpressionObject) {
// `[...set].length`
if (
memberExpressionObject.type === 'ArrayExpression'
&& memberExpressionObject.elements.length === 1
&& memberExpressionObject.elements[0]?.type === 'SpreadElement'
) {
return memberExpressionObject.elements[0].argument;
}

// `Array.from(set).length`
if (
isMethodCall(memberExpressionObject, {
object: 'Array',
method: 'from',
argumentsLength: 1,
optionalCall: false,
optionalMember: false,
})
) {
return memberExpressionObject.arguments[0];
}
}

function createFix(context, lengthAccessNode, set) {
const {sourceCode} = context;
const {
object: arrayExpression,
object: array,
property,
} = lengthAccessNodes;
const set = arrayExpression.elements[0].argument;
} = lengthAccessNode;

if (sourceCode.getCommentsInside(arrayExpression).length > sourceCode.getCommentsInside(set).length) {
if (sourceCode.getCommentsInside(array).length > sourceCode.getCommentsInside(set).length) {
return;
}

/** @param {import('eslint').Rule.RuleFixer} fixer */
return function * (fixer) {
yield fixer.replaceText(property, 'size');
yield fixer.replaceText(arrayExpression, sourceCode.getText(set));
yield fixSpaceAroundKeyword(fixer, lengthAccessNodes, context);
yield fixer.replaceText(array, sourceCode.getText(set));

if (array.type === 'ArrayExpression') {
yield fixSpaceAroundKeyword(fixer, lengthAccessNode, context);
}
};
}

Expand All @@ -67,22 +92,19 @@ const create = context => {
property: 'length',
optional: false,
})
|| node.object.type !== 'ArrayExpression'
|| node.object.elements.length !== 1
|| node.object.elements[0]?.type !== 'SpreadElement'
) {
return;
}

const maybeSet = node.object.elements[0].argument;
if (!isSet(maybeSet, sourceCode.getScope(maybeSet))) {
const set = getSetNode(node.object);
if (!set || !isSet(set, sourceCode.getScope(set))) {
return;
}

return {
node: node.property,
messageId: MESSAGE_ID,
fix: fix(context, node),
fix: createFix(context, node, set),
};
});
};
Expand Down
32 changes: 32 additions & 0 deletions test/prefer-set-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,23 @@ test.snapshot({
'[...foo].length',
'var foo = new Set(); var foo = new Set(); [...foo].length',
'[,].length',
// `Array.from` — valid cases
'Array.from(foo).length',
'Array.from(new NotSet(array)).length',
'Array.from(Set(array)).length',
'Array.from(new Set(array)).notLength',
'Array.from(new Set(array))?.length',
'Array.from(new Set(array))[length]',
'Array.from(new Set(array))["length"]',
'Array.from(new Set(array), mapFn).length',
'Array?.from(new Set(array)).length',
'Array.from?.(new Set(array)).length',
'const foo = new NotSet([]);Array.from(foo).length;',
'let foo = new Set([]);Array.from(foo).length;',
'const {foo} = new Set([]);Array.from(foo).length;',
'const [foo] = new Set([]);Array.from(foo).length;',
'var foo = new Set(); var foo = new Set(); Array.from(foo).length',
'NotArray.from(new Set(array)).length',
],
invalid: [
'[...new Set(array)].length',
Expand All @@ -43,5 +60,20 @@ test.snapshot({
`,
'[/* comment */...new Set(array)].length',
'[...new /* comment */ Set(array)].length',
// `Array.from` — invalid cases
'Array.from(new Set(array)).length',
outdent`
const foo = new Set([]);
console.log(Array.from(foo).length);
`,
'Array.from((( new Set(array) ))).length',
'(( Array.from(new Set(array)) )).length',
'Array.from(/* comment */ new Set(array)).length',
'Array.from(new /* comment */ Set(array)).length',
outdent`
function isUnique(array) {
return Array.from(new Set(array)).length === array.length
}
`,
],
});
139 changes: 139 additions & 0 deletions test/snapshots/prefer-set-size.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,142 @@ Generated by [AVA](https://avajs.dev).
Output:␊
1 | new /* comment */ Set(array).size␊
`

## invalid(10): Array.from(new Set(array)).length

> Input

`␊
1 | Array.from(new Set(array)).length␊
`

> Error 1/1

`␊
Message:␊
> 1 | Array.from(new Set(array)).length␊
| ^^^^^^ Prefer using \`Set#size\` instead of \`Array#length\`.␊
Output:␊
1 | new Set(array).size␊
`

## invalid(11): const foo = new Set([]); console.log(Array.from(foo).length);

> Input

`␊
1 | const foo = new Set([]);␊
2 | console.log(Array.from(foo).length);␊
`

> Error 1/1

`␊
Message:␊
1 | const foo = new Set([]);␊
> 2 | console.log(Array.from(foo).length);␊
| ^^^^^^ Prefer using \`Set#size\` instead of \`Array#length\`.␊
Output:␊
1 | const foo = new Set([]);␊
2 | console.log(foo.size);␊
`

## invalid(12): Array.from((( new Set(array) ))).length

> Input

`␊
1 | Array.from((( new Set(array) ))).length␊
`

> Error 1/1

`␊
Message:␊
> 1 | Array.from((( new Set(array) ))).length␊
| ^^^^^^ Prefer using \`Set#size\` instead of \`Array#length\`.␊
Output:␊
1 | new Set(array).size␊
`

## invalid(13): (( Array.from(new Set(array)) )).length

> Input

`␊
1 | (( Array.from(new Set(array)) )).length␊
`

> Error 1/1

`␊
Message:␊
> 1 | (( Array.from(new Set(array)) )).length␊
| ^^^^^^ Prefer using \`Set#size\` instead of \`Array#length\`.␊
Output:␊
1 | (( new Set(array) )).size␊
`

## invalid(14): Array.from(/* comment */ new Set(array)).length

> Input

`␊
1 | Array.from(/* comment */ new Set(array)).length␊
`

> Error 1/1

`␊
Message:␊
> 1 | Array.from(/* comment */ new Set(array)).length␊
| ^^^^^^ Prefer using \`Set#size\` instead of \`Array#length\`.␊
`

## invalid(15): Array.from(new /* comment */ Set(array)).length

> Input

`␊
1 | Array.from(new /* comment */ Set(array)).length␊
`

> Error 1/1

`␊
Message:␊
> 1 | Array.from(new /* comment */ Set(array)).length␊
| ^^^^^^ Prefer using \`Set#size\` instead of \`Array#length\`.␊
Output:␊
1 | new /* comment */ Set(array).size␊
`

## invalid(16): function isUnique(array) { return Array.from(new Set(array)).length === array.length }

> Input

`␊
1 | function isUnique(array) {␊
2 | return Array.from(new Set(array)).length === array.length␊
3 | }␊
`

> Error 1/1

`␊
Message:␊
1 | function isUnique(array) {␊
> 2 | return Array.from(new Set(array)).length === array.length␊
| ^^^^^^ Prefer using \`Set#size\` instead of \`Array#length\`.␊
3 | }␊
Output:␊
1 | function isUnique(array) {␊
2 | return new Set(array).size === array.length␊
3 | }␊
`
Binary file modified test/snapshots/prefer-set-size.js.snap
Binary file not shown.
Loading