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

Fix: wrong indent at tagged template in indent (fixes #12122) #12596

Merged
merged 7 commits into from Jan 17, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 9 additions & 1 deletion lib/rules/indent.js
Expand Up @@ -922,7 +922,15 @@ module.exports = {

parameterParens.add(openingParen);
parameterParens.add(closingParen);
offsets.setDesiredOffset(openingParen, sourceCode.getTokenBefore(openingParen), 0);

let offsetFrom = sourceCode.getTokenBefore(openingParen);
yeonjuan marked this conversation as resolved.
Show resolved Hide resolved

// If the node's callee is a TaggedTemplateExpression, use callee's tag as a offset from.
yeonjuan marked this conversation as resolved.
Show resolved Hide resolved
if (node.callee.type === "TaggedTemplateExpression") {
offsetFrom = node.callee.tag;
}

offsets.setDesiredOffset(openingParen, offsetFrom, 0);

addElementListIndent(node.arguments, openingParen, closingParen, options.CallExpression.arguments);
}
Expand Down
334 changes: 334 additions & 0 deletions tests/lib/rules/indent.js
Expand Up @@ -5383,6 +5383,115 @@ ruleTester.run("indent", rule, {
)
`,
parserOptions: { ecmaVersion: 2020 }
},

// https://github.com/eslint/eslint/issues/12122
{
code: unIndent`
foo(() => {
tag\`
multiline
template
literal
\`(() => {
bar();
});
});
`,
parserOptions: { ecmaVersion: 2015 }
},
{
code: unIndent`
{
tag\`
multiline
template
literal
\`(() => {
bar();
});
}
`,
parserOptions: { ecmaVersion: 2015 }
},
{
code: unIndent`
foo(() => {
tagOne\`
multiline
template
literal
\`(() => {
tagTwo\`
multiline
template
literal
\`(() => {
bar();
});

baz();
});
});
`,
parserOptions: { ecmaVersion: 2015 }
},
{
code: unIndent`
{
tagOne\`
multiline
template
literal
\`(() => {
tagTwo\`
multiline
template
literal
\`(() => {
bar();
});

baz();
});
};
`,
parserOptions: { ecmaVersion: 2015 }
},
{
code: unIndent`
tagOne\`multiline
template
linteral
\`(() => {
foo();

tagTwo\`multiline
template
literal
\`({
bar: 1,
baz: 2
});
});
`,
parserOptions: { ecmaVersion: 2015 }
},
{
code: unIndent`
tagOne\`multiline
template
linteral\`({
foo: 1,
bar: tagTwo\`multiline
template
linteral\`(() => {

baz();
})
});
`,
parserOptions: { ecmaVersion: 2015 }
}
],

Expand Down Expand Up @@ -10554,6 +10663,231 @@ ruleTester.run("indent", rule, {
[2, 4, 0, "Identifier"],
[3, 0, 4, "Punctuator"]
])
},

// https://github.com/eslint/eslint/issues/12122
{
code: unIndent`
foo(() => {
tag\`
multiline
template
literal
\`(() => {
bar();
});
});
`,
output: unIndent`
foo(() => {
tag\`
multiline
template
literal
\`(() => {
bar();
});
});
`,
parserOptions: { ecmaVersion: 2015 },
errors: expectedErrors([
[7, 8, 4, "Identifier"]
])
},
{
code: unIndent`
{
tag\`
multiline
template
literal
\`(() => {
bar();
});
}
`,
output: unIndent`
{
tag\`
multiline
template
literal
\`(() => {
bar();
});
}
`,
parserOptions: { ecmaVersion: 2015 },
errors: expectedErrors([
[2, 4, 8, "Identifier"],
[7, 8, 12, "Identifier"],
[8, 4, 8, "Punctuator"]
])
},
{
code: unIndent`
foo(() => {
tagOne\`
multiline
template
literal
\`(() => {
tagTwo\`
multiline
template
literal
\`(() => {
bar();
});

baz();
});
});
`,
output: unIndent`
foo(() => {
tagOne\`
multiline
template
literal
\`(() => {
tagTwo\`
multiline
template
literal
\`(() => {
bar();
});

baz();
});
});
`,
parserOptions: { ecmaVersion: 2015 },
errors: expectedErrors([
[7, 8, 12, "Identifier"],
[15, 8, 12, "Identifier"],
[16, 4, 0, "Punctuator"]
])
},
{
code: unIndent`
{
tagOne\`
multiline
template
literal
\`(() => {
tagTwo\`
multiline
template
literal
\`(() => {
bar();
});

baz();
});
}
`,
output: unIndent`
{
tagOne\`
multiline
template
literal
\`(() => {
tagTwo\`
multiline
template
literal
\`(() => {
bar();
});

baz();
});
}
`,
parserOptions: { ecmaVersion: 2015 },
errors: expectedErrors([
[7, 8, 12, "Identifier"],
[15, 8, 12, "Identifier"],
[16, 4, 0, "Punctuator"]
])
},
{
code: unIndent`
tagOne\`multiline
template
linteral
\`(() => {
foo();

tagTwo\`multiline
template
literal
\`({
bar: 1,
baz: 2
});
});
`,
output: unIndent`
tagOne\`multiline
template
linteral
\`(() => {
foo();

tagTwo\`multiline
template
literal
\`({
bar: 1,
baz: 2
});
});
`,
parserOptions: { ecmaVersion: 2015 },
errors: expectedErrors([
[5, 4, 0, "Identifier"],
[11, 8, 4, "Identifier"]
])
},
{
code: unIndent`
tagOne\`multiline
template
linteral\`({
foo: 1,
bar: tagTwo\`multiline
template
linteral\`(() => {

baz();
})
});
`,
output: unIndent`
tagOne\`multiline
template
linteral\`({
foo: 1,
bar: tagTwo\`multiline
template
linteral\`(() => {

baz();
})
});
`,
parserOptions: { ecmaVersion: 2015 },
errors: expectedErrors([
[4, 4, 8, "Identifier"],
[5, 4, 0, "Identifier"],
[9, 8, 0, "Identifier"]
])
}
]
});