sindresorhus / eslint-plugin-unicorn

@@ -3,14 +3,10 @@
Loading
3 3
const {appendArgument} = require('./fix/index.js');
4 4
5 5
const ERROR = 'error';
6 -
const SUGGESTION_TARGET_LOCATION_ORIGIN = 'target-location-origin';
7 -
const SUGGESTION_SELF_LOCATION_ORIGIN = 'self-location-origin';
8 -
const SUGGESTION_STAR = 'star';
6 +
const SUGGESTION = 'suggestion';
9 7
const messages = {
10 8
	[ERROR]: 'Missing the `targetOrigin` argument.',
11 -
	[SUGGESTION_TARGET_LOCATION_ORIGIN]: 'Use `{{target}}.location.origin`.',
12 -
	[SUGGESTION_SELF_LOCATION_ORIGIN]: 'Use `self.location.origin`.',
13 -
	[SUGGESTION_STAR]: 'Use `"*"`.',
9 +
	[SUGGESTION]: 'Use `{{code}}`.',
14 10
};
15 11
16 12
/** @param {import('eslint').Rule.RuleContext} context */
@@ -19,25 +15,21 @@
Loading
19 15
	return {
20 16
		[methodCallSelector({method: 'postMessage', argumentsLength: 1})](node) {
21 17
			const [penultimateToken, lastToken] = sourceCode.getLastTokens(node, 2);
22 -
			const suggestions = [];
18 +
			const replacements = [];
23 19
			const target = node.callee.object;
24 20
			if (target.type === 'Identifier') {
25 21
				const {name} = target;
26 22
27 -
				suggestions.push({
28 -
					messageId: SUGGESTION_TARGET_LOCATION_ORIGIN,
29 -
					data: {target: name},
30 -
					code: `${target.name}.location.origin`,
31 -
				});
23 +
				replacements.push(`${name}.location.origin`);
32 24
33 25
				if (name !== 'self' && name !== 'window' && name !== 'globalThis') {
34 -
					suggestions.push({messageId: SUGGESTION_SELF_LOCATION_ORIGIN, code: 'self.location.origin'});
26 +
					replacements.push('self.location.origin');
35 27
				}
36 28
			} else {
37 -
				suggestions.push({messageId: SUGGESTION_SELF_LOCATION_ORIGIN, code: 'self.location.origin'});
29 +
				replacements.push('self.location.origin');
38 30
			}
39 31
40 -
			suggestions.push({messageId: SUGGESTION_STAR, code: '\'*\''});
32 +
			replacements.push('\'*\'');
41 33
42 34
			return {
43 35
				loc: {
@@ -45,9 +37,9 @@
Loading
45 37
					end: lastToken.loc.end,
46 38
				},
47 39
				messageId: ERROR,
48 -
				suggest: suggestions.map(({messageId, data, code}) => ({
49 -
					messageId,
50 -
					data,
40 +
				suggest: replacements.map(code => ({
41 +
					messageId: SUGGESTION,
42 +
					data: {code},
51 43
					/** @param {import('eslint').Rule.RuleFixer} fixer */
52 44
					fix: fixer => appendArgument(fixer, node, code, sourceCode),
53 45
				})),

@@ -10,7 +10,7 @@
Loading
10 10
const messages = {
11 11
	[ERROR]: '`new Buffer()` is deprecated, use `Buffer.{{method}}()` instead.',
12 12
	[ERROR_UNKNOWN]: '`new Buffer()` is deprecated, use `Buffer.alloc()` or `Buffer.from()` instead.',
13 -
	[SUGGESTION]: 'Switch to `Buffer.{{method}}()`.',
13 +
	[SUGGESTION]: 'Switch to `Buffer.{{replacement}}()`.',
14 14
};
15 15
16 16
const inferMethod = (bufferArguments, scope) => {
@@ -69,10 +69,10 @@
Loading
69 69
			return {
70 70
				node,
71 71
				messageId: ERROR_UNKNOWN,
72 -
				suggest: ['from', 'alloc'].map(method => ({
72 +
				suggest: ['from', 'alloc'].map(replacement => ({
73 73
					messageId: SUGGESTION,
74 -
					data: {method},
75 -
					fix: fix(node, sourceCode, method),
74 +
					data: {replacement},
75 +
					fix: fix(node, sourceCode, replacement),
76 76
				})),
77 77
			};
78 78
		},

@@ -95,14 +95,13 @@
Loading
95 95
			}
96 96
97 97
			const {fix: autoFix, message = defaultMessage, match, suggest, regex} = replacement;
98 -
			const messageData = {
99 -
				match,
100 -
				suggest,
101 -
			};
102 98
			const problem = {
103 99
				node,
104 100
				message,
105 -
				data: messageData,
101 +
				data: {
102 +
					match,
103 +
					suggest,
104 +
				},
106 105
			};
107 106
108 107
			const fixed = string.replace(regex, suggest);
@@ -123,7 +122,6 @@
Loading
123 122
				problem.suggest = [
124 123
					{
125 124
						messageId: SUGGESTION_MESSAGE_ID,
126 -
						data: messageData,
127 125
						fix,
128 126
					},
129 127
				];

@@ -76,10 +76,6 @@
Loading
76 76
					problem.suggest = [
77 77
						{
78 78
							messageId: SUGGESTION_BITWISE,
79 -
							data: {
80 -
								operator,
81 -
								value: right.raw,
82 -
							},
83 79
							fix,
84 80
						},
85 81
					];

@@ -167,9 +167,6 @@
Loading
167 167
			problem.suggest = [
168 168
				{
169 169
					messageId: MESSAGE_ID_SUGGESTION,
170 -
					data: {
171 -
						name: node.name,
172 -
					},
173 170
					fix,
174 171
				},
175 172
			];

@@ -133,7 +133,6 @@
Loading
133 133
			problem.suggest = [
134 134
				{
135 135
					messageId: MESSAGE_ID_SUGGESTION,
136 -
					data: problem.data,
137 136
					fix,
138 137
				},
139 138
			];

@@ -64,7 +64,6 @@
Loading
64 64
		problem.suggest = [
65 65
			{
66 66
				messageId: MESSAGE_ID_SUGGESTION,
67 -
				data: problem.data,
68 67
				fix,
69 68
			},
70 69
		];

@@ -65,18 +65,16 @@
Loading
65 65
			return;
66 66
		}
67 67
68 -
		const messageData = {
69 -
			value,
70 -
			replacement,
71 -
		};
72 -
73 68
		/** @param {import('eslint').Rule.RuleFixer} fixer */
74 69
		const fix = fixer => replaceStringLiteral(fixer, node, replacement);
75 70
76 71
		const problem = {
77 72
			node,
78 73
			messageId: MESSAGE_ID_ERROR,
79 -
			data: messageData,
74 +
			data: {
75 +
				value,
76 +
				replacement,
77 +
			},
80 78
		};
81 79
82 80
		if (isFsReadFileEncoding(node)) {
@@ -87,7 +85,6 @@
Loading
87 85
		problem.suggest = [
88 86
			{
89 87
				messageId: MESSAGE_ID_SUGGESTION,
90 -
				data: messageData,
91 88
				fix: fixer => replaceStringLiteral(fixer, node, replacement),
92 89
			},
93 90
		];

@@ -56,6 +56,11 @@
Loading
56 56
					if (suggest.fix) {
57 57
						suggest.fix = wrapFixFunction(suggest.fix);
58 58
					}
59 +
60 +
					suggest.data = {
61 +
						...problem.data,
62 +
						...suggest.data,
63 +
					};
59 64
				}
60 65
			}
61 66

@@ -73,7 +73,6 @@
Loading
73 73
			suggest: [
74 74
				{
75 75
					messageId: SUGGESTION_ID_ARRAY_SOME,
76 -
					data: {method: methodNode.name},
77 76
					* fix(fixer) {
78 77
						yield fixer.replaceText(methodNode, 'some');
79 78
Files Coverage
configs 100.00%
rules 99.87%
scripts 73.33%
index.js 100.00%
Project Totals (203 files) 99.70%
1
coverage:
2
  status:
3
    project:
4
      default:
5
        target: 99%
6
        threshold: 1%
Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading