eslint 5.15.2 → 5.15.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +5 -0
- package/lib/rules/implicit-arrow-linebreak.js +17 -160
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
v5.15.3 - March 18, 2019
|
2
|
+
|
3
|
+
* [`71adc66`](https://github.com/eslint/eslint/commit/71adc665b9649b173adc76f80723b8de20664ae1) Fix: avoid moving comments in implicit-arrow-linebreak (fixes #11521) (#11522) (Teddy Katz)
|
4
|
+
* [`1f715a2`](https://github.com/eslint/eslint/commit/1f715a20c145d8ccc38f3310afccd838495d09d4) Chore: make test-case-property-ordering reasonable (#11511) (Toru Nagashima)
|
5
|
+
|
1
6
|
v5.15.2 - March 15, 2019
|
2
7
|
|
3
8
|
* [`29dbca7`](https://github.com/eslint/eslint/commit/29dbca73d762a809adb2f457b527e144426d54a7) Fix: implicit-arrow-linebreak adds extra characters (fixes #11268) (#11407) (Mark de Dios)
|
@@ -4,10 +4,7 @@
|
|
4
4
|
*/
|
5
5
|
"use strict";
|
6
6
|
|
7
|
-
const {
|
8
|
-
isArrowToken,
|
9
|
-
isParenthesised
|
10
|
-
} = require("../util/ast-utils");
|
7
|
+
const { isCommentToken, isNotOpeningParenToken } = require("../util/ast-utils");
|
11
8
|
|
12
9
|
//------------------------------------------------------------------------------
|
13
10
|
// Rule Definition
|
@@ -38,142 +35,7 @@ module.exports = {
|
|
38
35
|
|
39
36
|
create(context) {
|
40
37
|
const sourceCode = context.getSourceCode();
|
41
|
-
|
42
|
-
//----------------------------------------------------------------------
|
43
|
-
// Helpers
|
44
|
-
//----------------------------------------------------------------------
|
45
|
-
/**
|
46
|
-
* Gets the applicable preference for a particular keyword
|
47
|
-
* @returns {string} The applicable option for the keyword, e.g. 'beside'
|
48
|
-
*/
|
49
|
-
function getOption() {
|
50
|
-
return context.options[0] || "beside";
|
51
|
-
}
|
52
|
-
|
53
|
-
/**
|
54
|
-
* Formats the comments depending on whether it's a line or block comment.
|
55
|
-
* @param {Comment[]} comments The array of comments between the arrow and body
|
56
|
-
* @param {Integer} column The column number of the first token
|
57
|
-
* @returns {string} A string of comment text joined by line breaks
|
58
|
-
*/
|
59
|
-
function formatComments(comments) {
|
60
|
-
|
61
|
-
return `${comments.map(comment => {
|
62
|
-
|
63
|
-
if (comment.type === "Line") {
|
64
|
-
return `//${comment.value}`;
|
65
|
-
}
|
66
|
-
|
67
|
-
return `/*${comment.value}*/`;
|
68
|
-
}).join("\n")}\n`;
|
69
|
-
}
|
70
|
-
|
71
|
-
/**
|
72
|
-
* Finds the first token to prepend comments to depending on the parent type
|
73
|
-
* @param {ASTNode} node The validated node
|
74
|
-
* @returns {Token|Node} The node to prepend comments to
|
75
|
-
*/
|
76
|
-
function findFirstToken(node) {
|
77
|
-
switch (node.parent.type) {
|
78
|
-
case "VariableDeclarator":
|
79
|
-
|
80
|
-
// If the parent is first or only declarator, return the declaration, else, declarator
|
81
|
-
return sourceCode.getFirstToken(
|
82
|
-
node.parent.parent.declarations.length === 1 ||
|
83
|
-
node.parent.parent.declarations[0].id.name === node.parent.id.name
|
84
|
-
? node.parent.parent : node.parent
|
85
|
-
);
|
86
|
-
case "CallExpression":
|
87
|
-
case "Property":
|
88
|
-
|
89
|
-
// find the object key
|
90
|
-
return sourceCode.getFirstToken(node.parent);
|
91
|
-
default:
|
92
|
-
return node;
|
93
|
-
}
|
94
|
-
}
|
95
|
-
|
96
|
-
/**
|
97
|
-
* Helper function for adding parentheses fixes for nodes containing nested arrow functions
|
98
|
-
* @param {Fixer} fixer Fixer
|
99
|
-
* @param {Token} arrow - The arrow token
|
100
|
-
* @param {ASTNode} arrowBody - The arrow function body
|
101
|
-
* @returns {Function[]} autofixer -- wraps function bodies with parentheses
|
102
|
-
*/
|
103
|
-
function addParentheses(fixer, arrow, arrowBody) {
|
104
|
-
const parenthesesFixes = [];
|
105
|
-
let closingParentheses = "";
|
106
|
-
|
107
|
-
let followingBody = arrowBody;
|
108
|
-
let currentArrow = arrow;
|
109
|
-
|
110
|
-
while (currentArrow && followingBody.type !== "BlockStatement") {
|
111
|
-
if (!isParenthesised(sourceCode, followingBody)) {
|
112
|
-
parenthesesFixes.push(
|
113
|
-
fixer.insertTextAfter(currentArrow, " (")
|
114
|
-
);
|
115
|
-
|
116
|
-
closingParentheses = `\n)${closingParentheses}`;
|
117
|
-
}
|
118
|
-
|
119
|
-
currentArrow = sourceCode.getTokenAfter(currentArrow, isArrowToken);
|
120
|
-
|
121
|
-
if (currentArrow) {
|
122
|
-
followingBody = followingBody.body;
|
123
|
-
}
|
124
|
-
}
|
125
|
-
|
126
|
-
return [...parenthesesFixes,
|
127
|
-
fixer.insertTextAfter(arrowBody, closingParentheses)
|
128
|
-
];
|
129
|
-
}
|
130
|
-
|
131
|
-
/**
|
132
|
-
* Autofixes the function body to collapse onto the same line as the arrow.
|
133
|
-
* If comments exist, checks if the function body contains arrow functions, and appends the body with parentheses.
|
134
|
-
* Otherwise, prepends the comments before the arrow function.
|
135
|
-
* @param {Token} arrowToken The arrow token.
|
136
|
-
* @param {ASTNode|Token} arrowBody the function body
|
137
|
-
* @param {ASTNode} node The evaluated node
|
138
|
-
* @returns {Function} autofixer -- validates the node to adhere to besides
|
139
|
-
*/
|
140
|
-
function autoFixBesides(arrowToken, arrowBody, node) {
|
141
|
-
return fixer => {
|
142
|
-
const placeBesides = fixer.replaceTextRange([arrowToken.range[1], arrowBody.range[0]], " ");
|
143
|
-
|
144
|
-
const comments = sourceCode.getCommentsInside(node).filter(comment =>
|
145
|
-
comment.loc.start.line < arrowBody.loc.start.line);
|
146
|
-
|
147
|
-
if (comments.length) {
|
148
|
-
|
149
|
-
// If the grandparent is not a variable declarator
|
150
|
-
if (
|
151
|
-
arrowBody.parent &&
|
152
|
-
arrowBody.parent.parent &&
|
153
|
-
arrowBody.parent.parent.type !== "VariableDeclarator"
|
154
|
-
) {
|
155
|
-
|
156
|
-
// If any arrow functions follow, return the necessary parens fixes.
|
157
|
-
if (node.body.type === "ArrowFunctionExpression" &&
|
158
|
-
arrowBody.parent.parent.type !== "VariableDeclarator"
|
159
|
-
) {
|
160
|
-
return addParentheses(fixer, arrowToken, arrowBody);
|
161
|
-
}
|
162
|
-
}
|
163
|
-
|
164
|
-
const firstToken = findFirstToken(node);
|
165
|
-
|
166
|
-
const commentBeforeExpression = fixer.insertTextBeforeRange(
|
167
|
-
firstToken.range,
|
168
|
-
formatComments(comments)
|
169
|
-
);
|
170
|
-
|
171
|
-
return [placeBesides, commentBeforeExpression];
|
172
|
-
}
|
173
|
-
|
174
|
-
return placeBesides;
|
175
|
-
};
|
176
|
-
}
|
38
|
+
const option = context.options[0] || "beside";
|
177
39
|
|
178
40
|
/**
|
179
41
|
* Validates the location of an arrow function body
|
@@ -181,35 +43,30 @@ module.exports = {
|
|
181
43
|
* @returns {void}
|
182
44
|
*/
|
183
45
|
function validateExpression(node) {
|
184
|
-
|
185
|
-
|
186
|
-
let tokenBefore = sourceCode.getTokenBefore(node.body);
|
187
|
-
const hasParens = tokenBefore.value === "(";
|
188
|
-
|
189
|
-
if (node.type === "BlockStatement") {
|
46
|
+
if (node.body.type === "BlockStatement") {
|
190
47
|
return;
|
191
48
|
}
|
192
49
|
|
193
|
-
|
50
|
+
const arrowToken = sourceCode.getTokenBefore(node.body, isNotOpeningParenToken);
|
51
|
+
const firstTokenOfBody = sourceCode.getTokenAfter(arrowToken);
|
194
52
|
|
195
|
-
if (
|
196
|
-
|
197
|
-
// Gets the first token before the function body that is not an open paren
|
198
|
-
tokenBefore = sourceCode.getTokenBefore(node.body, token => token.value !== "(");
|
199
|
-
fixerTarget = sourceCode.getTokenAfter(tokenBefore);
|
200
|
-
}
|
201
|
-
|
202
|
-
if (tokenBefore.loc.end.line === fixerTarget.loc.start.line && option === "below") {
|
53
|
+
if (arrowToken.loc.end.line === firstTokenOfBody.loc.start.line && option === "below") {
|
203
54
|
context.report({
|
204
|
-
node:
|
55
|
+
node: firstTokenOfBody,
|
205
56
|
messageId: "expected",
|
206
|
-
fix: fixer => fixer.insertTextBefore(
|
57
|
+
fix: fixer => fixer.insertTextBefore(firstTokenOfBody, "\n")
|
207
58
|
});
|
208
|
-
} else if (
|
59
|
+
} else if (arrowToken.loc.end.line !== firstTokenOfBody.loc.start.line && option === "beside") {
|
209
60
|
context.report({
|
210
|
-
node:
|
61
|
+
node: firstTokenOfBody,
|
211
62
|
messageId: "unexpected",
|
212
|
-
fix
|
63
|
+
fix(fixer) {
|
64
|
+
if (sourceCode.getFirstTokenBetween(arrowToken, firstTokenOfBody, { includeComments: true, filter: isCommentToken })) {
|
65
|
+
return null;
|
66
|
+
}
|
67
|
+
|
68
|
+
return fixer.replaceTextRange([arrowToken.range[1], firstTokenOfBody.range[0]], " ");
|
69
|
+
}
|
213
70
|
});
|
214
71
|
}
|
215
72
|
}
|