Skip to content

Commit 2801cc6

Browse files
Lightning00BladeDevtools-frontend LUCI CQ
authored andcommitted
[eslint] Add new test case for html tagged template
With the new change of allowing destruction in the import, create a test case for this rule as well. Bug: 397260638 Change-Id: I276d185beb377599cec2cd7fdb526fc33fae98f1 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6278525 Auto-Submit: Nikolay Vitkov <[email protected]> Reviewed-by: Benedikt Meurer <[email protected]> Commit-Queue: Benedikt Meurer <[email protected]>
1 parent cdeebfe commit 2801cc6

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

scripts/eslint_rules/lib/html-tagged-template.js

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,58 @@ module.exports = {
1414
category: 'Possible Errors',
1515
},
1616
fixable: 'code',
17-
schema: [] // no options
17+
schema: [], // no options
1818
},
19-
create: function(context) {
19+
create: function (context) {
2020
const sourceCode = context.sourceCode ?? context.getSourceCode();
2121
let lastImport = null;
22-
let shortandDefined = false;
22+
let shorthandDefined = false;
2323
return {
2424
ImportDeclaration(node) {
2525
lastImport = node;
2626
},
2727
VariableDeclarator(node) {
28-
if ((sourceCode.getScope ? sourceCode.getScope(node) : context.getScope()).type !== 'module') {
28+
if (
29+
(sourceCode.getScope ? sourceCode.getScope(node) : context.getScope())
30+
.type !== 'module'
31+
) {
2932
return;
3033
}
3134
if (node.id.name === 'html') {
32-
shortandDefined = true;
35+
shorthandDefined = true;
3336
}
3437
for (const property of node.id.properties || []) {
3538
if (property.key.name === 'html') {
36-
shortandDefined = true;
39+
shorthandDefined = true;
3740
}
3841
}
3942
},
4043
TaggedTemplateExpression(node) {
4144
const tag = node.tag;
42-
if (tag.type === 'MemberExpression' && tag.object.name === 'Lit' && tag.property.name === 'html') {
45+
if (
46+
tag.type === 'MemberExpression' &&
47+
tag.object.name === 'Lit' &&
48+
tag.property.name === 'html'
49+
) {
4350
context.report({
4451
node,
45-
message: 'Use unqualified html tagged template for compatibility with lit-analyzer',
52+
message:
53+
'Use unqualified html tagged template for compatibility with lit-analyzer',
4654
fix(fixer) {
47-
const result = [fixer.removeRange([tag.object.range[0], tag.property.range[0]])];
48-
if (lastImport && !shortandDefined) {
49-
result.push(fixer.insertTextAfter(lastImport, '\n\nconst {html} = Lit;'));
50-
shortandDefined = true;
55+
const result = [
56+
fixer.removeRange([tag.object.range[0], tag.property.range[0]]),
57+
];
58+
if (lastImport && !shorthandDefined) {
59+
result.push(
60+
fixer.insertTextAfter(lastImport, '\n\nconst {html} = Lit;'),
61+
);
62+
shorthandDefined = true;
5163
}
5264
return result;
53-
}
65+
},
5466
});
5567
}
56-
}
68+
},
5769
};
58-
}
70+
},
5971
};

scripts/eslint_rules/tests/html-tagged-template.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ const error = {
1313

1414
new RuleTester().run('html-tagged-template', rule, {
1515
valid: [
16+
{
17+
code: `import {html, render} from '../../ui/lit/lit.js';
18+
19+
function render() {
20+
render(
21+
html\`<div></div>\`,
22+
this.shadow, {host: this});
23+
}
24+
`,
25+
},
1626
{
1727
code: `import * as Lit from '../../../../ui/lit/lit.js';
1828

0 commit comments

Comments
 (0)