Skip to content

Commit

Permalink
Make suggestion inherit error message data
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Sep 21, 2022
1 parent 7c8abf5 commit 8b6fa6c
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 43 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
1 change: 0 additions & 1 deletion rules/no-new-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ const create = context => {
messageId: ERROR_UNKNOWN,
suggest: ['from', 'alloc'].map(method => ({
messageId: SUGGESTION,
data: {method},
fix: fix(node, sourceCode, method),
})),
};
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
29 changes: 27 additions & 2 deletions rules/prefer-number-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,33 @@ function checkProperty({node, path: [name]}, sourceCode) {
yield * fixSpaceAroundKeyword(fixer, parent, sourceCode);
};

return problem;
if (property === 'NEGATIVE_INFINITY') {
problem.node = parent;
problem.data.description = '-Infinity';
problem.fix = function * (fixer) {
yield fixer.replaceText(parent, 'Number.NEGATIVE_INFINITY');
yield * fixSpaceAroundKeyword(fixer, parent, sourceCode);
};

yield problem;
continue;
}

const fix = fixer => replaceReferenceIdentifier(node, `Number.${property}`, fixer, sourceCode);
const isSafeToFix = globalObjects[name];

if (isSafeToFix) {
problem.fix = fix;
} else {
problem.suggest = [
{
messageId: MESSAGE_ID_SUGGESTION,
fix,
},
];
}

yield problem;
}

const fix = fixer => replaceReferenceIdentifier(node, `Number.${property}`, fixer, sourceCode);
Expand All @@ -64,7 +90,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 @@ -173,9 +173,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 @@ -66,18 +66,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 @@ -88,7 +86,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

0 comments on commit 8b6fa6c

Please sign in to comment.