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

Deprecate prefer-exponentiation-operator rule #484

2 changes: 2 additions & 0 deletions docs/rules/prefer-exponentiation-operator.md
@@ -1,5 +1,7 @@
# Prefer the exponentiation operator over `Math.pow()`

**This rule is deprecated. Use the built-in ESLint [`prefer-exponentiation-operator`](https://eslint.org/docs/rules/prefer-exponentiation-operator) rule instead.**

Enforces the use of the [exponentiation operator](http://2ality.com/2016/02/exponentiation-operator.html) over [`Math.pow()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/pow).

This rule is fixable.
Expand Down
1 change: 0 additions & 1 deletion index.js
Expand Up @@ -46,7 +46,6 @@ module.exports = {
'unicorn/prefer-add-event-listener': 'error',
'unicorn/prefer-dataset': 'error',
'unicorn/prefer-event-key': 'error',
'unicorn/prefer-exponentiation-operator': 'error',
'unicorn/prefer-flat-map': 'error',
'unicorn/prefer-includes': 'error',
'unicorn/prefer-modern-dom-apis': 'error',
Expand Down
5 changes: 3 additions & 2 deletions readme.md
Expand Up @@ -64,7 +64,6 @@ Configure it in `package.json`.
"unicorn/prefer-add-event-listener": "error",
"unicorn/prefer-dataset": "error",
"unicorn/prefer-event-key": "error",
"unicorn/prefer-exponentiation-operator": "error",
"unicorn/prefer-flat-map": "error",
"unicorn/prefer-includes": "error",
"unicorn/prefer-modern-dom-apis": "error",
Expand Down Expand Up @@ -118,7 +117,6 @@ Configure it in `package.json`.
- [prefer-add-event-listener](docs/rules/prefer-add-event-listener.md) - Prefer `.addEventListener()` and `.removeEventListener()` over `on`-functions. *(partly fixable)*
- [prefer-dataset](docs/rules/prefer-dataset.md) - Prefer using `.dataset` on DOM elements over `.setAttribute(…)`. *(fixable)*
- [prefer-event-key](docs/rules/prefer-event-key.md) - Prefer `KeyboardEvent#key` over `KeyboardEvent#keyCode`. *(partly fixable)*
- [prefer-exponentiation-operator](docs/rules/prefer-exponentiation-operator.md) - Prefer the exponentiation operator over `Math.pow()` *(fixable)*
- [prefer-flat-map](docs/rules/prefer-flat-map.md) - Prefer `.flatMap(…)` over `.map(…).flat()`. *(fixable)*
- [prefer-includes](docs/rules/prefer-includes.md) - Prefer `.includes()` over `.indexOf()` when checking for existence or non-existence. *(fixable)*
- [prefer-modern-dom-apis](docs/rules/prefer-modern-dom-apis.md) - Prefer `.before()` over `.insertBefore()`, `.replaceWith()` over `.replaceChild()`, prefer one of `.before()`, `.after()`, `.append()` or `.prepend()` over `insertAdjacentText()` and `insertAdjacentElement()`. *(fixable)*
Expand All @@ -137,6 +135,9 @@ Configure it in `package.json`.
- [regex-shorthand](docs/rules/regex-shorthand.md) - Enforce the use of regex shorthands to improve readability. *(fixable)*
- [throw-new-error](docs/rules/throw-new-error.md) - Require `new` when throwing an error. *(fixable)*

## Deprecated Rules

- [prefer-exponentiation-operator](docs/rules/prefer-exponentiation-operator.md) - Use the built-in ESLint [`prefer-exponentiation-operator`](https://eslint.org/docs/rules/prefer-exponentiation-operator) rule instead.

## Recommended config

Expand Down
6 changes: 5 additions & 1 deletion rules/prefer-exponentiation-operator.js
Expand Up @@ -58,5 +58,9 @@ module.exports = {
url: getDocumentationUrl(__filename)
},
fixable: 'code'
}
},
deprecated: true,
replacedBy: [
'prefer-exponentiation-operator'
]
};
26 changes: 17 additions & 9 deletions test/package.js
Expand Up @@ -15,6 +15,10 @@ const ignoredRules = [
'no-nested-ternary'
];

const deprecatedRules = [
'prefer-exponentiation-operator'
];

const testSorted = (t, actualOrder, sourceName) => {
actualOrder = actualOrder.filter(x => !ignoredRules.includes(x));
const sortedOrder = actualOrder.slice(0).sort();
Expand All @@ -30,21 +34,22 @@ test('Every rule is defined in index file in alphabetical order', t => {
for (const file of ruleFiles) {
const name = path.basename(file, '.js');
t.truthy(index.rules[name], `'${name}' is not exported in 'index.js'`);
t.truthy(index.configs.recommended.rules[`unicorn/${name}`], `'${name}' is not set in the recommended config`);
if (!deprecatedRules.includes(name)) {
t.truthy(index.configs.recommended.rules[`unicorn/${name}`], `'${name}' is not set in the recommended config`);
}

t.truthy(fs.existsSync(path.join('docs/rules', `${name}.md`)), `There is no documentation for '${name}'`);
t.truthy(fs.existsSync(path.join('test', file)), `There are no tests for '${name}'`);
}

console.log(Object.keys(index.rules).length - ignoredRules.length, ruleFiles.length);

t.is(
Object.keys(index.rules).length,
ruleFiles.length,
'There are more exported rules than rule files.'
);
t.is(
Object.keys(index.configs.recommended.rules).length - ignoredRules.length,
ruleFiles.length,
ruleFiles.length - deprecatedRules.length,
'There are more exported rules in the recommended config than rule files.'
);

Expand All @@ -62,7 +67,7 @@ test('Every rule is defined in readme.md usage and list of rules in alphabetical

t.truthy(usageRules, 'List of rules should be defined in readme.md ## Usage and be valid JSON');

const rulesMatch = /## Rules(.*?)## Recommended config/ms.exec(readme);
const rulesMatch = /## Rules(.*?)## Deprecated Rules/ms.exec(readme);
t.truthy(rulesMatch, 'List of rules should be defined in readme.md in ## Rules before ## Recommended config');
const rulesText = rulesMatch[1];
const re = /- \[(.*?)]\((.*?)\) - (.*)\n/gm;
Expand All @@ -77,14 +82,17 @@ test('Every rule is defined in readme.md usage and list of rules in alphabetical
}
} while (match);

for (const file of ruleFiles) {
const name = path.basename(file, '.js');
const availableRules = ruleFiles
.map(file => path.basename(file, '.js'))
.filter(name => !deprecatedRules.includes(name));

for (const name of availableRules) {
t.truthy(usageRules[`unicorn/${name}`], `'${name}' is not described in the readme.md ## Usage`);
t.truthy(rules.includes(name), `'${name}' is not described in the readme.md ## Rules`);
}

t.is(Object.keys(usageRules).length - ignoredRules.length, ruleFiles.length, 'There are more rules in readme.md ## Usage than rule files.');
t.is(Object.keys(rules).length, ruleFiles.length, 'There are more rules in readme.md ## Rules than rule files.');
t.is(Object.keys(usageRules).length - ignoredRules.length, availableRules.length, 'There are more rules in readme.md ## Usage than rule files.');
t.is(Object.keys(rules).length, availableRules.length, 'There are more rules in readme.md ## Rules than rule files.');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be worth it to add a check that ensures deprecated rules are described in the readme?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's really nessary.


testSorted(t, Object.keys(usageRules), 'readme.md ## Usage rules');
testSorted(t, rules, 'readme.md ## Rules');
Expand Down
5 changes: 5 additions & 0 deletions test/prefer-exponentiation-operator.js
Expand Up @@ -10,6 +10,11 @@ const ruleTester = avaRuleTester(test, {

const message = 'Prefer the exponentiation operator over `Math.pow()`.';

test('deprecated', t => {
t.true(rule.deprecated);
t.deepEqual(rule.replacedBy, ['prefer-exponentiation-operator']);
});

ruleTester.run('prefer-exponentiation-operator', rule, {
valid: [
'a ** b;',
Expand Down