Skip to content

Commit

Permalink
Add an internal ESLint rule prefer-negative-boolean-attribute (#1725)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Feb 11, 2022
1 parent e8ab176 commit 9de8a44
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 1 deletion.
24 changes: 24 additions & 0 deletions package.json
Expand Up @@ -74,6 +74,7 @@
"eslint": "^8.6.0",
"eslint-ava-rule-tester": "^4.0.0",
"eslint-plugin-eslint-plugin": "^4.1.0",
"eslint-plugin-internal-rules": "file:./scripts/internal-rules/",
"eslint-remote-tester": "^2.0.1",
"eslint-remote-tester-repositories": "^0.0.3",
"execa": "^6.0.0",
Expand Down Expand Up @@ -155,7 +156,30 @@
"eslint-plugin/require-meta-has-suggestions": "off",
"eslint-plugin/require-meta-schema": "off"
}
},
{
"files": [
"rules/**/*.js"
],
"plugins": [
"internal-rules"
],
"rules": {
"internal-rules/prefer-negative-boolean-attribute": "error"
}
}
]
},
"npmpackagejsonlint": {
"rules": {
"prefer-caret-version-devDependencies": [
"error",
{
"exceptions": [
"eslint-plugin-internal-rules"
]
}
]
}
}
}
2 changes: 1 addition & 1 deletion rules/throw-new-error.js
Expand Up @@ -22,7 +22,7 @@ const selector = [
// `throw lib.FooError()`
[
'[callee.type="MemberExpression"]',
'[callee.computed=false]',
'[callee.computed!=true]',
'[callee.property.type="Identifier"]',
`[callee.property.name=/${customError.source}/]`,
].join(''),
Expand Down
7 changes: 7 additions & 0 deletions scripts/internal-rules/index.js
@@ -0,0 +1,7 @@
'use strict';

module.exports = {
rules: {
'prefer-negative-boolean-attribute': require('./prefer-negative-boolean-attribute.js'),
},
};
6 changes: 6 additions & 0 deletions scripts/internal-rules/package.json
@@ -0,0 +1,6 @@
{
"private": true,
"name": "eslint-plugin-internal-rules",
"description": "Internal rules",
"license": "MIT"
}
44 changes: 44 additions & 0 deletions scripts/internal-rules/prefer-negative-boolean-attribute.js
@@ -0,0 +1,44 @@
'use strict';
const path = require('path');

const messageId = path.basename(__filename, '.js');

const shouldReport = (string, value) => {
const index = string.indexOf(`=${value}]`);

if (index === -1) {
return false;
}

return string[index - 1] !== '!';
};

module.exports = {
create(context) {
return {
'TemplateElement, Literal'(node) {
const string = node.value;
if (typeof string !== 'string') {
return;
}

for (const value of [true, false]) {
if (shouldReport(string, value)) {
context.report({
node,
messageId,
data: {
preferred: String(!value),
},
});
}
}
},
};
},
meta: {
messages: {
[messageId]: 'Prefer use `[…!={{preferred}}]` in esquery selector.',
},
},
};

0 comments on commit 9de8a44

Please sign in to comment.