-
-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
118 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,18 @@ | ||
'use strict'; | ||
|
||
const pluginName = 'internal-rules'; | ||
|
||
const rules = [ | ||
'prefer-negative-boolean-attribute', | ||
'prefer-disallow-over-forbid', | ||
]; | ||
|
||
module.exports = { | ||
rules: { | ||
'prefer-negative-boolean-attribute': require('./prefer-negative-boolean-attribute.js'), | ||
rules: Object.fromEntries(rules.map(id => [id, require(`./${id}.js`)])), | ||
configs: { | ||
all: { | ||
plugins: [pluginName], | ||
rules: Object.fromEntries(rules.map(id => [`${pluginName}/${id}`, 'error'])), | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
{ | ||
"private": true, | ||
"name": "eslint-plugin-internal-rules", | ||
"version": "0.0.0", | ||
"description": "Internal rules", | ||
"license": "MIT" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
'use strict'; | ||
const path = require('path'); | ||
const {matches} = require('../../rules/selectors/index.js'); | ||
const toLocation = require('../../rules/utils/to-location.js'); | ||
|
||
const messageId = path.basename(__filename, '.js'); | ||
|
||
const messageSelector = [ | ||
matches([ | ||
// `const messages = {...}`; | ||
[ | ||
'VariableDeclarator', | ||
'[id.type="Identifier"]', | ||
'[id.name="messages"]', | ||
], | ||
// `{meta: {messages = {...}}}`; | ||
[ | ||
'Property', | ||
'[key.type="Identifier"]', | ||
'[key.name="messages"]', | ||
], | ||
]), | ||
' > ', | ||
'ObjectExpression.init', | ||
' > ', | ||
'Property.properties', | ||
' > ', | ||
'Literal.value', | ||
].join(''); | ||
|
||
const descriptionSelector = [ | ||
'Property', | ||
'[computed!=true]', | ||
' > ', | ||
'Literal.value', | ||
].join(''); | ||
|
||
const selector = matches([messageSelector, descriptionSelector]); | ||
|
||
const words = [ | ||
{word: 'forbid', replacement: 'disallow'}, | ||
{word: 'forbidden', replacement: 'disallowed'}, | ||
]; | ||
|
||
module.exports = { | ||
create(context) { | ||
return { | ||
[selector](node) { | ||
const {value} = node; | ||
if (typeof value !== 'string') { | ||
return; | ||
} | ||
|
||
const message = node.raw.slice(1, -1); | ||
const lowerCased = message.toLowerCase(); | ||
|
||
for (let {word, replacement} of words) { | ||
const index = lowerCased.indexOf(word); | ||
|
||
if (index === -1) { | ||
continue; | ||
} | ||
|
||
const original = message.slice(index, index + word.length); | ||
|
||
if (/^[A-Z]/.test(original)) { | ||
replacement = replacement[0].toUpperCase() + replacement.slice(1); | ||
} | ||
|
||
const start = node.range[0] + index + 1; | ||
const range = [start, start + word.length]; | ||
context.report({ | ||
node, | ||
loc: toLocation(range, context.getSourceCode()), | ||
messageId, | ||
data: {original, replacement}, | ||
fix: fixer => fixer.replaceTextRange(range, replacement), | ||
}); | ||
|
||
// Only report one problem | ||
return; | ||
} | ||
}, | ||
}; | ||
}, | ||
meta: { | ||
fixable: 'code', | ||
messages: { | ||
[messageId]: 'Prefer use `{{replacement}}` over `{{original}}` in error message and rule description.', | ||
}, | ||
}, | ||
}; |