Skip to content

Commit dd60dc9

Browse files
danilsomsikovDevtools-frontend LUCI CQ
authored andcommitted
Support class properties as DOM fragments in the preferTemplateLiterals
Bug: 400353541 Change-Id: I0d0724f752d75a262da31deaf0662221cc8442d4 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6387921 Reviewed-by: Philip Pfaffe <[email protected]> Commit-Queue: Danil Somsikov <[email protected]>
1 parent e419264 commit dd60dc9

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

scripts/eslint_rules/lib/no-imperative-dom-api.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ module.exports = {
180180
*/
181181
function getOrCreateDomFragment(node) {
182182
const variable = getEnclosingVariable(node);
183-
const key = variable ?? sourceCode.getText(node);
183+
const key = variable ?? sourceCode.getText(getEnclosingProperty(node) ?? node);
184184

185185
let result = domFragments.get(key);
186186
if (!result) {
@@ -230,6 +230,14 @@ module.exports = {
230230
return null;
231231
}
232232

233+
function getEnclosingProperty(node) {
234+
if (node.parent.type === 'AssignmentExpression' && node.parent.right === node &&
235+
isMemberExpression(node.parent.left, n => n.type === 'ThisExpression', n => n.type === 'Identifier')) {
236+
return /** @type {Node} */ (node.parent.left);
237+
}
238+
return null;
239+
}
240+
233241
/**
234242
* @param {Node} event
235243
* @return {string|null}
@@ -381,7 +389,7 @@ module.exports = {
381389
* @param {DomFragment} domFragment
382390
*/
383391
function maybeReportDomFragment(domFragment) {
384-
if (!domFragment.replacementLocation || domFragment.parent) {
392+
if (!domFragment.replacementLocation || domFragment.parent || !domFragment.tagName) {
385393
return;
386394
}
387395
context.report({
@@ -410,9 +418,11 @@ export const DEFAULT_VIEW = (input, _output, target) => {
410418

411419
return {
412420
MemberExpression(node) {
413-
if (node.object.type === 'ThisExpression' && isIdentifier(node.property, 'contentElement')) {
421+
if (node.object.type === 'ThisExpression') {
414422
const domFragment = getOrCreateDomFragment(node);
415-
domFragment.tagName = 'div';
423+
if (isIdentifier(node.property, 'contentElement')) {
424+
domFragment.tagName = 'div';
425+
}
416426
}
417427
if (isIdentifier(node.object, 'document') && isIdentifier(node.property, 'createElement')
418428
&& node.parent.type === 'CallExpression' && node.parent.callee === node) {

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,18 @@ class SomeWidget extends UI.Widget.Widget {
7878
class SomeWidget extends UI.Widget.Widget {
7979
constructor() {
8080
super();
81-
this.contentElement.classList.add('some-class');
82-
this.contentElement.addEventListener('click', this.onClick.bind(this));
81+
this.container = this.contentElement.createChild('div', 'some-class');
82+
this.container.classList.add('container');
83+
this.container.addEventListener('click', this.onClick.bind(this));
8384
}
8485
}`,
8586
output: `
8687
8788
export const DEFAULT_VIEW = (input, _output, target) => {
8889
render(html\`
89-
<div class="some-class" @click=\${this.onClick.bind(this)}></div>\`,
90+
<div>
91+
<div class="some-class container" @click=\${this.onClick.bind(this)}></div>
92+
</div>\`,
9093
target, {host: input});
9194
};
9295

0 commit comments

Comments
 (0)