Skip to content

Commit 11e8f26

Browse files
danilsomsikovDevtools-frontend LUCI CQ
authored and
Devtools-frontend LUCI CQ
committed
no-imperative-dom-api: Inline local variables definition in template
By default we refer to the variable by name, but the variable definition would be removed if all it's references are processed. Bug: 407751147 Change-Id: I0088e34c630c420868ff4e206a10dd68efaf4fdf Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6438846 Auto-Submit: Danil Somsikov <dsv@chromium.org> Reviewed-by: Philip Pfaffe <pfaffe@chromium.org> Commit-Queue: Philip Pfaffe <pfaffe@chromium.org>
1 parent 45f5c04 commit 11e8f26

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

scripts/eslint_rules/lib/no-imperative-dom-api/dom-fragment.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ export class DomFragment {
7676

7777
toTemplateLiteral(sourceCode: Readonly<SourceCode>, indent = 4): string[] {
7878
if (this.expression && !this.tagName) {
79-
return [`\n${' '.repeat(indent)}`, '${', this.expression, '}'];
79+
const value = this.initializer?.parent?.type === 'VariableDeclarator' ? this.initializer?.parent?.init : null;
80+
const expression =
81+
(this.references.every(r => r.processed) && value) ? sourceCode.getText(value) : this.expression;
82+
return [`\n${' '.repeat(indent)}`, '${', expression, '}'];
8083
}
8184

8285
function toOutputString(node: Node|string, quoteLiterals = false): string {

scripts/eslint_rules/tests/no-imperative-dom-api.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,8 @@ class SomeWidget extends UI.Widget.Widget {
433433
label.addEventListener('click', () => this.doSomething.bind(this));
434434
this.contentElement.appendChild(details);
435435
details.createChild('span');
436+
const banner = createBanner();
437+
this.contentElement.appendChild(banner);
436438
}
437439
}`,
438440
output: `
@@ -442,6 +444,7 @@ export const DEFAULT_VIEW = (input, _output, target) => {
442444
<div>
443445
\${label}
444446
\${details}
447+
\${createBanner()}
445448
</div>\`,
446449
target, {host: input});
447450
};

0 commit comments

Comments
 (0)