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

Use singular array element variable name in autofix for no-for-loop rule #745

Merged
merged 6 commits into from May 27, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -40,6 +40,7 @@
"eslint-utils": "^2.0.0",
"import-modules": "^2.0.0",
"lodash": "^4.17.15",
"pluralize": "^8.0.0",
"read-pkg-up": "^7.0.1",
"regexp-tree": "^0.1.21",
"reserved-words": "^0.1.2",
Expand Down
16 changes: 15 additions & 1 deletion rules/no-for-loop.js
Expand Up @@ -3,6 +3,7 @@ const getDocumentationUrl = require('./utils/get-documentation-url');
const isLiteralValue = require('./utils/is-literal-value');
const {flatten} = require('lodash');
const avoidCapture = require('./utils/avoid-capture');
const {singular} = require('pluralize');

const defaultElementName = 'element';
const isLiteralZero = node => isLiteralValue(node, 0);
Expand Down Expand Up @@ -267,6 +268,19 @@ const getChildScopesRecursive = scope => [
...flatten(scope.childScopes.map(scope => getChildScopesRecursive(scope)))
];

const getSingularName = originalName => {
const singularName = singular(originalName);
if (
!singularName ||
singularName === originalName
) {
// Bail if we can't produce a singular name that differs from the original name.
return;
}

return singularName;
};
bmish marked this conversation as resolved.
Show resolved Hide resolved

const create = context => {
const sourceCode = context.getSourceCode();
const {scopeManager} = sourceCode;
Expand Down Expand Up @@ -342,7 +356,7 @@ const create = context => {

const index = indexIdentifierName;
const element = elementIdentifierName ||
avoidCapture(defaultElementName, getChildScopesRecursive(bodyScope), context.parserOptions.ecmaVersion);
avoidCapture(getSingularName(arrayIdentifierName) || defaultElementName, getChildScopesRecursive(bodyScope), context.parserOptions.ecmaVersion);
const array = arrayIdentifierName;

let declarationElement = element;
Expand Down
39 changes: 39 additions & 0 deletions test/no-for-loop.js
Expand Up @@ -649,6 +649,45 @@ ruleTester.run('no-for-loop', rule, {
console.log(element);
console.log(element_);
}
`),

// Singularization:
...[
['plugin', 'plugins'], // Simple
['person', 'people'], // Irregular
['largeCity', 'largeCities'], // CamelCase
['LARGE_CITY', 'LARGE_CITIES'], // Caps, snake_case
['element', 'list'] // No singular version
].map(([elementName, arrayName]) =>
testCase(
`for(const i = 0; i < ${arrayName}.length; i++) {console.log(${arrayName}[i])}`,
`for(const ${elementName} of ${arrayName}) {console.log(${elementName})}`
)
),

// Singularization (avoid using reserved JavaScript keywords):
testCase(outdent`
for (let i = 0; i < cases.length; i++) {
console.log(cases[i]);
}
`, outdent`
for (const case_ of cases) {
console.log(case_);
}
`),
// Singularization (avoid variable name collision):
testCase(outdent`
for (let i = 0; i < cities.length; i++) {
console.log(cities[i]);
const city = foo();
console.log(city);
}
`, outdent`
for (const city_ of cities) {
console.log(city_);
const city = foo();
console.log(city);
}
`)
]
});
Expand Down