Skip to content

Commit

Permalink
Update: improve reported location of arrow-parens (fixes #11773) (#11775
Browse files Browse the repository at this point in the history
)
  • Loading branch information
g-plane authored and aladdin-add committed May 30, 2019
1 parent d662b17 commit 9590587
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
21 changes: 21 additions & 0 deletions lib/rules/arrow-parens.js
Expand Up @@ -10,6 +10,23 @@

const astUtils = require("./utils/ast-utils");

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------

/**
* Get location should be reported by AST node.
*
* @param {ASTNode} node AST Node.
* @returns {Location} Location information.
*/
function getLocation(node) {
return {
start: node.params[0].loc.start,
end: node.params[node.params.length - 1].loc.end
};
}

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -102,6 +119,7 @@ module.exports = {
context.report({
node,
messageId: "unexpectedParensInline",
loc: getLocation(node),
fix: fixParamsWithParenthesis
});
}
Expand All @@ -116,6 +134,7 @@ module.exports = {
context.report({
node,
messageId: "expectedParensBlock",
loc: getLocation(node),
fix(fixer) {
return fixer.replaceText(firstTokenOfParam, `(${firstTokenOfParam.value})`);
}
Expand All @@ -135,6 +154,7 @@ module.exports = {
context.report({
node,
messageId: "unexpectedParens",
loc: getLocation(node),
fix: fixParamsWithParenthesis
});
}
Expand All @@ -149,6 +169,7 @@ module.exports = {
context.report({
node,
messageId: "expectedParens",
loc: getLocation(node),
fix(fixer) {
return fixer.replaceText(firstTokenOfParam, `(${firstTokenOfParam.value})`);
}
Expand Down
34 changes: 25 additions & 9 deletions tests/lib/rules/arrow-parens.js
Expand Up @@ -82,6 +82,7 @@ const invalid = [
errors: [{
line: 1,
column: 1,
endColumn: 2,
messageId: "expectedParens",
type
}]
Expand All @@ -92,6 +93,7 @@ const invalid = [
errors: [{
line: 1,
column: 1,
endColumn: 2,
messageId: "expectedParens",
type
}]
Expand All @@ -102,6 +104,7 @@ const invalid = [
errors: [{
line: 1,
column: 1,
endColumn: 2,
messageId: "expectedParens",
type
}]
Expand All @@ -112,6 +115,7 @@ const invalid = [
errors: [{
line: 1,
column: 8,
endColumn: 11,
messageId: "expectedParens",
type
}]
Expand All @@ -122,6 +126,7 @@ const invalid = [
errors: [{
line: 1,
column: 8,
endColumn: 11,
messageId: "expectedParens",
type
}]
Expand All @@ -132,6 +137,7 @@ const invalid = [
errors: [{
line: 1,
column: 3,
endColumn: 6,
messageId: "expectedParens",
type
}]
Expand All @@ -142,7 +148,8 @@ const invalid = [
parserOptions: { ecmaVersion: 8 },
errors: [{
line: 1,
column: 3,
column: 9,
endColumn: 12,
messageId: "expectedParens",
type
}]
Expand All @@ -155,7 +162,8 @@ const invalid = [
options: ["as-needed"],
errors: [{
line: 1,
column: 1,
column: 2,
endColumn: 3,
messageId: "unexpectedParens",
type
}]
Expand All @@ -167,7 +175,8 @@ const invalid = [
parserOptions: { ecmaVersion: 8 },
errors: [{
line: 1,
column: 1,
column: 2,
endColumn: 3,
messageId: "unexpectedParens",
type
}]
Expand All @@ -179,7 +188,8 @@ const invalid = [
parserOptions: { ecmaVersion: 8 },
errors: [{
line: 1,
column: 1,
column: 8,
endColumn: 9,
messageId: "unexpectedParens",
type
}]
Expand All @@ -191,7 +201,8 @@ const invalid = [
parserOptions: { ecmaVersion: 8 },
errors: [{
line: 1,
column: 1,
column: 7,
endColumn: 8,
messageId: "unexpectedParens",
type
}]
Expand All @@ -205,6 +216,7 @@ const invalid = [
errors: [{
line: 1,
column: 1,
endColumn: 2,
messageId: "expectedParensBlock",
type
}]
Expand All @@ -215,7 +227,8 @@ const invalid = [
options: ["as-needed", { requireForBlockBody: true }],
errors: [{
line: 1,
column: 1,
column: 2,
endColumn: 3,
messageId: "unexpectedParensInline",
type
}]
Expand All @@ -227,7 +240,8 @@ const invalid = [
parserOptions: { ecmaVersion: 8 },
errors: [{
line: 1,
column: 1,
column: 7,
endColumn: 8,
messageId: "expectedParensBlock",
type
}]
Expand All @@ -239,7 +253,8 @@ const invalid = [
parserOptions: { ecmaVersion: 8 },
errors: [{
line: 1,
column: 1,
column: 8,
endColumn: 9,
messageId: "unexpectedParensInline",
type
}]
Expand All @@ -251,7 +266,8 @@ const invalid = [
parserOptions: { ecmaVersion: 8 },
errors: [{
line: 1,
column: 1,
column: 7,
endColumn: 8,
messageId: "unexpectedParensInline",
type
}]
Expand Down

0 comments on commit 9590587

Please sign in to comment.