Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: improve reported location of arrow-parens (fixes #11773) #11775

Merged
merged 1 commit into from May 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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