Skip to content

Commit

Permalink
Update: report backtick loc in no-unexpected-multiline (refs #12334) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjermanovic committed Apr 24, 2020
1 parent 8e7a2d9 commit d3aac53
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 27 deletions.
34 changes: 22 additions & 12 deletions lib/rules/no-unexpected-multiline.js
Expand Up @@ -53,7 +53,11 @@ module.exports = {
const nodeExpressionEnd = sourceCode.getTokenBefore(openParen);

if (openParen.loc.start.line !== nodeExpressionEnd.loc.end.line) {
context.report({ node, loc: openParen.loc.start, messageId, data: { char: openParen.value } });
context.report({
node,
loc: openParen.loc,
messageId
});
}
}

Expand All @@ -71,18 +75,24 @@ module.exports = {
},

TaggedTemplateExpression(node) {
if (node.tag.loc.end.line === node.quasi.loc.start.line) {
return;
}

// handle generics type parameters on template tags
const tokenBefore = sourceCode.getTokenBefore(node.quasi);

if (tokenBefore.loc.end.line === node.quasi.loc.start.line) {
return;
const { quasi } = node;

// handles common tags, parenthesized tags, and typescript's generic type arguments
const tokenBefore = sourceCode.getTokenBefore(quasi);

if (tokenBefore.loc.end.line !== quasi.loc.start.line) {
context.report({
node,
loc: {
start: quasi.loc.start,
end: {
line: quasi.loc.start.line,
column: quasi.loc.start.column + 1
}
},
messageId: "taggedTemplate"
});
}

context.report({ node, loc: node.loc.start, messageId: "taggedTemplate" });
},

CallExpression(node) {
Expand Down
71 changes: 56 additions & 15 deletions tests/lib/rules/no-unexpected-multiline.js
Expand Up @@ -43,6 +43,18 @@ ruleTester.run("no-unexpected-multiline", rule, {
code: "x\n.y\nz `Valid Test Case`",
parserOptions: { ecmaVersion: 6 }
},
{
code: "f(x\n)`Valid Test Case`",
parserOptions: { ecmaVersion: 6 }
},
{
code: "x.\ny `Valid Test Case`",
parserOptions: { ecmaVersion: 6 }
},
{
code: "(x\n)`Valid Test Case`",
parserOptions: { ecmaVersion: 6 }
},
`
foo
/ bar /2
Expand Down Expand Up @@ -118,15 +130,18 @@ ruleTester.run("no-unexpected-multiline", rule, {
errors: [{
messageId: "function",
line: 2,
column: 1

column: 1,
endLine: 2,
endColumn: 2
}]
},
{
code: "var a = (a || b)\n(x || y).doSomething()",
errors: [{
line: 2,
column: 1,
endLine: 2,
endColumn: 2,
messageId: "function"
}]
},
Expand All @@ -135,6 +150,8 @@ ruleTester.run("no-unexpected-multiline", rule, {
errors: [{
line: 2,
column: 1,
endLine: 2,
endColumn: 2,
messageId: "function"
}]
},
Expand All @@ -143,6 +160,8 @@ ruleTester.run("no-unexpected-multiline", rule, {
errors: [{
line: 2,
column: 1,
endLine: 2,
endColumn: 2,
messageId: "property"
}]
},
Expand All @@ -151,6 +170,8 @@ ruleTester.run("no-unexpected-multiline", rule, {
errors: [{
line: 2,
column: 5,
endLine: 2,
endColumn: 6,
messageId: "function"
}]
},
Expand All @@ -159,33 +180,41 @@ ruleTester.run("no-unexpected-multiline", rule, {
errors: [{
line: 2,
column: 3,
endLine: 2,
endColumn: 4,
messageId: "property"
}]
},
{
code: "let x = function() {}\n `hello`",
parserOptions: { ecmaVersion: 6 },
errors: [{
line: 1,
column: 9,
line: 2,
column: 2,
endLine: 2,
endColumn: 3,
messageId: "taggedTemplate"
}]
},
{
code: "let x = function() {}\nx\n`hello`",
parserOptions: { ecmaVersion: 6 },
errors: [{
line: 2,
line: 3,
column: 1,
endLine: 3,
endColumn: 2,
messageId: "taggedTemplate"
}]
},
{
code: "x\n.y\nz\n`Invalid Test Case`",
parserOptions: { ecmaVersion: 6 },
errors: [{
line: 3,
line: 4,
column: 1,
endLine: 4,
endColumn: 2,
messageId: "taggedTemplate"
}]
},
Expand All @@ -197,6 +226,8 @@ ruleTester.run("no-unexpected-multiline", rule, {
errors: [{
line: 3,
column: 17,
endLine: 3,
endColumn: 18,
messageId: "division"
}]
},
Expand All @@ -208,6 +239,8 @@ ruleTester.run("no-unexpected-multiline", rule, {
errors: [{
line: 3,
column: 17,
endLine: 3,
endColumn: 18,
messageId: "division"
}]
},
Expand All @@ -219,6 +252,8 @@ ruleTester.run("no-unexpected-multiline", rule, {
errors: [{
line: 3,
column: 17,
endLine: 3,
endColumn: 18,
messageId: "division"
}]
},
Expand All @@ -230,6 +265,8 @@ ruleTester.run("no-unexpected-multiline", rule, {
errors: [{
line: 3,
column: 17,
endLine: 3,
endColumn: 18,
messageId: "division"
}]
},
Expand All @@ -241,24 +278,28 @@ ruleTester.run("no-unexpected-multiline", rule, {
errors: [{
line: 3,
column: 17,
endLine: 3,
endColumn: 18,
messageId: "division"
}]
},

// https://github.com/eslint/eslint/issues/11650
{
code: `
const x = aaaa<
test
>/*
test
*/\`foo\`
`,
code: [
"const x = aaaa<",
" test",
">/*",
"test",
"*/`foo`"
].join("\n"),
parser: require.resolve("../../fixtures/parsers/typescript-parsers/tagged-template-with-generic/tagged-template-with-generic-and-comment"),
errors: [
{
line: 1,
column: 11,
line: 5,
column: 3,
endLine: 5,
endColumn: 4,
messageId: "taggedTemplate"
}
]
Expand Down

0 comments on commit d3aac53

Please sign in to comment.