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

Add an internal ESLint rule prefer-negative-boolean-attribute #1725

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
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/",
Copy link
Contributor

Choose a reason for hiding this comment

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

Does eslint not offer a way to load a plugin from a file instead of a module?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This being a loooong story, and the reason is ridiculous, add a plugin path in config, ESLint can't get a plugin name to use...

eslint/eslint#3458

Unless you use the ESLint API, you can't add plugin in other way.

Copy link
Contributor

Choose a reason for hiding this comment

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

You could hack the module resolution like node core does but i guess it's even worse of a hack 😉.

"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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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.',
},
},
};