Skip to content

Commit

Permalink
Make suggestions inherit error message data (#1911)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Sep 21, 2022
1 parent 550c412 commit bc5dbd8
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 55 deletions.
1 change: 0 additions & 1 deletion rules/explicit-length-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ function create(context) {
problem.suggest = [
{
messageId: MESSAGE_ID_SUGGESTION,
data: problem.data,
fix,
},
];
Expand Down
8 changes: 4 additions & 4 deletions rules/no-new-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const SUGGESTION = 'suggestion';
const messages = {
[ERROR]: '`new Buffer()` is deprecated, use `Buffer.{{method}}()` instead.',
[ERROR_UNKNOWN]: '`new Buffer()` is deprecated, use `Buffer.alloc()` or `Buffer.from()` instead.',
[SUGGESTION]: 'Switch to `Buffer.{{method}}()`.',
[SUGGESTION]: 'Switch to `Buffer.{{replacement}}()`.',
};

const inferMethod = (bufferArguments, scope) => {
Expand Down Expand Up @@ -69,10 +69,10 @@ const create = context => {
return {
node,
messageId: ERROR_UNKNOWN,
suggest: ['from', 'alloc'].map(method => ({
suggest: ['from', 'alloc'].map(replacement => ({
messageId: SUGGESTION,
data: {method},
fix: fix(node, sourceCode, method),
data: {replacement},
fix: fix(node, sourceCode, replacement),
})),
};
},
Expand Down
1 change: 0 additions & 1 deletion rules/prefer-array-some.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ const create = context => ({
suggest: [
{
messageId: SUGGESTION_ID_ARRAY_SOME,
data: {method: methodNode.name},
* fix(fixer) {
yield fixer.replaceText(methodNode, 'some');

Expand Down
4 changes: 0 additions & 4 deletions rules/prefer-math-trunc.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,6 @@ const create = context => {
problem.suggest = [
{
messageId: SUGGESTION_BITWISE,
data: {
operator,
value: right.raw,
},
fix,
},
];
Expand Down
1 change: 0 additions & 1 deletion rules/prefer-number-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ function checkProperty({node, path: [name]}, sourceCode) {
problem.suggest = [
{
messageId: MESSAGE_ID_SUGGESTION,
data: problem.data,
fix,
},
];
Expand Down
3 changes: 0 additions & 3 deletions rules/prefer-set-has.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,6 @@ const create = context => ({
problem.suggest = [
{
messageId: MESSAGE_ID_SUGGESTION,
data: {
name: node.name,
},
fix,
},
];
Expand Down
28 changes: 10 additions & 18 deletions rules/require-post-message-target-origin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@ const {methodCallSelector} = require('./selectors/index.js');
const {appendArgument} = require('./fix/index.js');

const ERROR = 'error';
const SUGGESTION_TARGET_LOCATION_ORIGIN = 'target-location-origin';
const SUGGESTION_SELF_LOCATION_ORIGIN = 'self-location-origin';
const SUGGESTION_STAR = 'star';
const SUGGESTION = 'suggestion';
const messages = {
[ERROR]: 'Missing the `targetOrigin` argument.',
[SUGGESTION_TARGET_LOCATION_ORIGIN]: 'Use `{{target}}.location.origin`.',
[SUGGESTION_SELF_LOCATION_ORIGIN]: 'Use `self.location.origin`.',
[SUGGESTION_STAR]: 'Use `"*"`.',
[SUGGESTION]: 'Use `{{code}}`.',
};

/** @param {import('eslint').Rule.RuleContext} context */
Expand All @@ -19,35 +15,31 @@ function create(context) {
return {
[methodCallSelector({method: 'postMessage', argumentsLength: 1})](node) {
const [penultimateToken, lastToken] = sourceCode.getLastTokens(node, 2);
const suggestions = [];
const replacements = [];
const target = node.callee.object;
if (target.type === 'Identifier') {
const {name} = target;

suggestions.push({
messageId: SUGGESTION_TARGET_LOCATION_ORIGIN,
data: {target: name},
code: `${target.name}.location.origin`,
});
replacements.push(`${name}.location.origin`);

if (name !== 'self' && name !== 'window' && name !== 'globalThis') {
suggestions.push({messageId: SUGGESTION_SELF_LOCATION_ORIGIN, code: 'self.location.origin'});
replacements.push('self.location.origin');
}
} else {
suggestions.push({messageId: SUGGESTION_SELF_LOCATION_ORIGIN, code: 'self.location.origin'});
replacements.push('self.location.origin');
}

suggestions.push({messageId: SUGGESTION_STAR, code: '\'*\''});
replacements.push('\'*\'');

return {
loc: {
start: penultimateToken.loc.end,
end: lastToken.loc.end,
},
messageId: ERROR,
suggest: suggestions.map(({messageId, data, code}) => ({
messageId,
data,
suggest: replacements.map(code => ({
messageId: SUGGESTION,
data: {code},
/** @param {import('eslint').Rule.RuleFixer} fixer */
fix: fixer => appendArgument(fixer, node, code, sourceCode),
})),
Expand Down
10 changes: 4 additions & 6 deletions rules/string-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,13 @@ const create = context => {
}

const {fix: autoFix, message = defaultMessage, match, suggest, regex} = replacement;
const messageData = {
match,
suggest,
};
const problem = {
node,
message,
data: messageData,
data: {
match,
suggest,
},
};

const fixed = string.replace(regex, suggest);
Expand All @@ -123,7 +122,6 @@ const create = context => {
problem.suggest = [
{
messageId: SUGGESTION_MESSAGE_ID,
data: messageData,
fix,
},
];
Expand Down
11 changes: 4 additions & 7 deletions rules/text-encoding-identifier-case.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,16 @@ const create = () => ({
return;
}

const messageData = {
value,
replacement,
};

/** @param {import('eslint').Rule.RuleFixer} fixer */
const fix = fixer => replaceStringLiteral(fixer, node, replacement);

const problem = {
node,
messageId: MESSAGE_ID_ERROR,
data: messageData,
data: {
value,
replacement,
},
};

if (isFsReadFileEncoding(node)) {
Expand All @@ -87,7 +85,6 @@ const create = () => ({
problem.suggest = [
{
messageId: MESSAGE_ID_SUGGESTION,
data: messageData,
fix: fixer => replaceStringLiteral(fixer, node, replacement),
},
];
Expand Down
5 changes: 5 additions & 0 deletions rules/utils/rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ function reportListenerProblems(listener, context) {
if (suggest.fix) {
suggest.fix = wrapFixFunction(suggest.fix);
}

suggest.data = {
...problem.data,
...suggest.data,
};
}
}

Expand Down
20 changes: 10 additions & 10 deletions test/snapshots/require-post-message-target-origin.mjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Generated by [AVA](https://avajs.dev).
1 | window.postMessage(message, window.location.origin)␊
--------------------------------------------------------------------------------␊
Suggestion 2/2: Use \`"*"\`.␊
Suggestion 2/2: Use \`'*'\`.␊
1 | window.postMessage(message, '*')␊
`

Expand All @@ -36,7 +36,7 @@ Generated by [AVA](https://avajs.dev).
1 | self.postMessage(message, self.location.origin)␊
--------------------------------------------------------------------------------␊
Suggestion 2/2: Use \`"*"\`.␊
Suggestion 2/2: Use \`'*'\`.␊
1 | self.postMessage(message, '*')␊
`

Expand All @@ -54,7 +54,7 @@ Generated by [AVA](https://avajs.dev).
1 | globalThis.postMessage(message, globalThis.location.origin)␊
--------------------------------------------------------------------------------␊
Suggestion 2/2: Use \`"*"\`.␊
Suggestion 2/2: Use \`'*'\`.␊
1 | globalThis.postMessage(message, '*')␊
`

Expand All @@ -76,7 +76,7 @@ Generated by [AVA](https://avajs.dev).
1 | foo.postMessage(message , self.location.origin)␊
--------------------------------------------------------------------------------␊
Suggestion 3/3: Use \`"*"\`.␊
Suggestion 3/3: Use \`'*'\`.␊
1 | foo.postMessage(message , '*')␊
`

Expand All @@ -98,7 +98,7 @@ Generated by [AVA](https://avajs.dev).
1 | foo.postMessage( ((message)) , self.location.origin)␊
--------------------------------------------------------------------------------␊
Suggestion 3/3: Use \`"*"\`.␊
Suggestion 3/3: Use \`'*'\`.␊
1 | foo.postMessage( ((message)) , '*')␊
`

Expand All @@ -120,7 +120,7 @@ Generated by [AVA](https://avajs.dev).
1 | foo.postMessage(message, self.location.origin,)␊
--------------------------------------------------------------------------------␊
Suggestion 3/3: Use \`"*"\`.␊
Suggestion 3/3: Use \`'*'\`.␊
1 | foo.postMessage(message, '*',)␊
`

Expand All @@ -142,7 +142,7 @@ Generated by [AVA](https://avajs.dev).
1 | foo.postMessage(message , self.location.origin,)␊
--------------------------------------------------------------------------------␊
Suggestion 3/3: Use \`"*"\`.␊
Suggestion 3/3: Use \`'*'\`.␊
1 | foo.postMessage(message , '*',)␊
`

Expand All @@ -160,7 +160,7 @@ Generated by [AVA](https://avajs.dev).
1 | foo.window.postMessage(message, self.location.origin)␊
--------------------------------------------------------------------------------␊
Suggestion 2/2: Use \`"*"\`.␊
Suggestion 2/2: Use \`'*'\`.␊
1 | foo.window.postMessage(message, '*')␊
`

Expand All @@ -178,7 +178,7 @@ Generated by [AVA](https://avajs.dev).
1 | document.defaultView.postMessage(message, self.location.origin)␊
--------------------------------------------------------------------------------␊
Suggestion 2/2: Use \`"*"\`.␊
Suggestion 2/2: Use \`'*'\`.␊
1 | document.defaultView.postMessage(message, '*')␊
`

Expand All @@ -196,6 +196,6 @@ Generated by [AVA](https://avajs.dev).
1 | getWindow().postMessage(message, self.location.origin)␊
--------------------------------------------------------------------------------␊
Suggestion 2/2: Use \`"*"\`.␊
Suggestion 2/2: Use \`'*'\`.␊
1 | getWindow().postMessage(message, '*')␊
`
Binary file modified test/snapshots/require-post-message-target-origin.mjs.snap
Binary file not shown.

0 comments on commit bc5dbd8

Please sign in to comment.