Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix(eslint-plugin): [no-unnecessary-template-expression] don't report when an expression includes comment#10444

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,6 +10,8 @@ import {
getParserServices,
isTypeFlagSet,
isUndefinedIdentifier,
nullThrows,
NullThrowsReasons,
} from '../util';
import { rangeToLoc } from '../util/rangeToLoc';

Expand DownExpand Up@@ -92,6 +94,22 @@ export default createRule<[], MessageId>({
);
}

function hasCommentsBetweenQuasi(
startQuasi: TSESTree.TemplateElement,
endQuasi: TSESTree.TemplateElement,
): boolean {
const startToken = nullThrows(
context.sourceCode.getTokenByRangeStart(startQuasi.range[0]),
NullThrowsReasons.MissingToken('`${', 'opening template literal'),
);
const endToken = nullThrows(
context.sourceCode.getTokenByRangeStart(endQuasi.range[0]),
NullThrowsReasons.MissingToken('}', 'closing template literal'),
);

return context.sourceCode.commentsExistBetween(startToken, endToken);
}

return {
TemplateLiteral(node: TSESTree.TemplateLiteral): void {
if (node.parent.type === AST_NODE_TYPES.TaggedTemplateExpression) {
Expand All@@ -106,6 +124,10 @@ export default createRule<[], MessageId>({
isUnderlyingTypeString(node.expressions[0]);

if (hasSingleStringVariable) {
if (hasCommentsBetweenQuasi(node.quasis[0], node.quasis[1])) {
return;
}

context.report({
loc: rangeToLoc(context.sourceCode, [
node.expressions[0].range[0] - 2,
Expand All@@ -132,7 +154,7 @@ export default createRule<[], MessageId>({
nextQuasi: node.quasis[index + 1],
prevQuasi: node.quasis[index],
}))
.filter(({ expression, nextQuasi }) => {
.filter(({ expression, nextQuasi, prevQuasi }) => {
if (
isUndefinedIdentifier(expression) ||
isInfinityIdentifier(expression) ||
Expand All@@ -141,6 +163,11 @@ export default createRule<[], MessageId>({
return true;
}

// allow expressions that include comments
if (hasCommentsBetweenQuasi(prevQuasi, nextQuasi)) {
return false;
}

if (isLiteral(expression)) {
// allow trailing whitespace literal
if (startsWithNewLine(nextQuasi.value.raw)) {
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -310,10 +310,6 @@ const invalidCases: readonly InvalidTestCase<
}\`;
`,
errors: [
{
line: 2,
messageId: 'noUnnecessaryTemplateExpression',
},
{
column: 2,
endColumn: 2,
Expand All@@ -331,12 +327,18 @@ const invalidCases: readonly InvalidTestCase<
],
output: [
`
\`use\${
\`less\`
}\`;
\`u\${
// hopefully this comment is not needed.
'se'

}le\${ \`ss\` }\`;
`,
`
\`useless\`;
\`u\${
// hopefully this comment is not needed.
'se'

}less\`;
`,
],
},
Expand DownExpand Up@@ -1104,6 +1106,42 @@ this code has trailing whitespace: \${' '}
\`trailing position interpolated empty string also makes whitespace clear \${''}
\`;
`,
`
\`
\${/* intentional comment before */ 'bar'}
...\`;
`,
`
\`
\${'bar' /* intentional comment after */}
...\`;
`,
`
\`
\${/* intentional comment before */ 'bar' /* intentional comment after */}
...\`;
`,
`
\`\${/* intentional before */ 'bar'}\`;
`,
`
\`\${'bar' /* intentional comment after */}\`;
`,
`
\`\${/* intentional comment before */ 'bar' /* intentional comment after */}\`;
`,
`
\`\${
// intentional comment before
'bar'
}\`;
`,
`
\`\${
'bar'
// intentional comment after
}\`;
`,
],

invalid: [
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp