Skip to content

Commit 305ec9b

Browse files
danilsomsikovDevtools-frontend LUCI CQ
authored andcommitted
[eslint] Fix strict tsc issues in the no-imperative-dom-api
Bug: 407085691 Change-Id: If07beab48274b5491e78576556434db3b3670495 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6414778 Commit-Queue: Danil Somsikov <[email protected]> Reviewed-by: Nikolay Vitkov <[email protected]>
1 parent 9491d58 commit 305ec9b

File tree

4 files changed

+35
-24
lines changed

4 files changed

+35
-24
lines changed

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

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,21 @@ module.exports = {
7777
function processReference(reference, domFragment) {
7878
const parent = reference.parent;
7979
const isAccessed = parent.type === 'MemberExpression' && parent.object === reference;
80-
const property = isAccessed ? parent.property : null;
80+
if (!isAccessed) {
81+
return false;
82+
}
83+
const property = parent.property;
8184
const grandParent = parent.parent;
82-
const isPropertyAssignment =
83-
isAccessed && grandParent.type === 'AssignmentExpression' && grandParent.left === parent;
85+
const isPropertyAssignment = grandParent.type === 'AssignmentExpression' && grandParent.left === parent;
8486
const propertyValue = isPropertyAssignment ? grandParent.right : null;
85-
const isMethodCall = isAccessed && grandParent.type === 'CallExpression' && grandParent.callee === parent;
86-
const firstArg = isMethodCall ? grandParent.arguments[0] : null;
87-
const secondArg = isMethodCall ? grandParent.arguments[1] : null;
87+
const isMethodCall = grandParent.type === 'CallExpression' && grandParent.callee === parent;
8888
const grandGrandParent = grandParent.parent;
89-
const isPropertyMethodCall = isAccessed && grandParent.type === 'MemberExpression' &&
90-
grandParent.object === parent && grandGrandParent.type === 'CallExpression' &&
91-
grandGrandParent.callee === grandParent;
89+
const isPropertyMethodCall = grandParent.type === 'MemberExpression' && grandParent.object === parent &&
90+
grandGrandParent.type === 'CallExpression' && grandGrandParent.callee === grandParent;
9291
const propertyMethodArgument = isPropertyMethodCall ? grandGrandParent.arguments[0] : null;
93-
const isSubpropertyAssignment = isAccessed && grandParent.type === 'MemberExpression' &&
94-
grandParent.object === parent && grandParent.property.type === 'Identifier' &&
95-
grandGrandParent.type === 'AssignmentExpression' && grandGrandParent.left === grandParent;
92+
const isSubpropertyAssignment = grandParent.type === 'MemberExpression' && grandParent.object === parent &&
93+
grandParent.property.type === 'Identifier' && grandGrandParent.type === 'AssignmentExpression' &&
94+
grandGrandParent.left === grandParent;
9695
const subproperty =
9796
isSubpropertyAssignment && grandParent.property.type === 'Identifier' ? grandParent.property : null;
9897
const subpropertyValue = isSubpropertyAssignment ? grandGrandParent.right : null;
@@ -102,6 +101,8 @@ module.exports = {
102101
return true;
103102
}
104103
} else if (isMethodCall) {
104+
const firstArg = grandParent.arguments[0];
105+
const secondArg = grandParent.arguments[1];
105106
if (isIdentifier(property, 'addEventListener')) {
106107
const event = getEvent(firstArg);
107108
const value = secondArg;
@@ -136,19 +137,21 @@ module.exports = {
136137
if (!reference.processed) {
137138
continue;
138139
}
139-
const expression = getEnclosingExpression(reference.node);
140-
if (!expression) {
140+
const range = getEnclosingExpression(reference.node)?.range;
141+
if (!range) {
141142
continue;
142143
}
143-
const range = expression.range;
144144
ranges.push(range);
145145
for (const child of domFragment.children) {
146146
ranges.push(...getRangesToRemove(child));
147147
}
148148
}
149149

150150
if (domFragment.initializer && domFragment.references.every(r => r.processed)) {
151-
ranges.push(getEnclosingExpression(domFragment.initializer).range);
151+
const range = getEnclosingExpression(domFragment.initializer)?.range;
152+
if (range) {
153+
ranges.push(range);
154+
}
152155
}
153156
for (const range of ranges) {
154157
while ([' ', '\n'].includes(sourceCode.text[range[0] - 1])) {
@@ -180,15 +183,15 @@ module.exports = {
180183
const template = 'html`' + domFragment.toTemplateLiteral(sourceCode).join('') + '`';
181184
let replacementLocation = domFragment.replacementLocation;
182185

183-
if (replacementLocation.type === 'VariableDeclarator') {
184-
domFragment.initializer = null;
186+
if (replacementLocation?.type === 'VariableDeclarator') {
187+
domFragment.initializer = undefined;
185188
return [
186189
fixer.replaceText(replacementLocation.init, template),
187190
...getRangesToRemove(domFragment).map(range => fixer.removeRange(range)),
188191
];
189192
}
190193

191-
if (replacementLocation.parent.type === 'ExportNamedDeclaration') {
194+
if (replacementLocation?.parent?.type === 'ExportNamedDeclaration') {
192195
replacementLocation = replacementLocation.parent;
193196
}
194197
const text = `

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ module.exports = {
4343
}
4444
return true;
4545
}
46+
return false;
4647
},
4748
NewExpression(node) {
4849
if (isMemberExpression(

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,18 @@ class ClassMember {
2121
/** @type {Node} */
2222
classDeclaration;
2323

24+
/** @param {Node} classDeclaration */
25+
constructor(classDeclaration) {
26+
this.classDeclaration = classDeclaration;
27+
}
28+
2429
/** @type {Node|undefined} */
2530
initializer;
2631

2732
/**
2833
* @param {Node} node
2934
* @param {SourceCode} sourceCode
30-
* @return {ClassMember}
35+
* @return {ClassMember|null}
3136
*/
3237
static getOrCreate(node, sourceCode) {
3338
const classDeclaration = getEnclosingClassDeclaration(node);
@@ -42,8 +47,7 @@ class ClassMember {
4247
const memberName = sourceCode.getText(node);
4348
let classMember = classMembers.get(memberName);
4449
if (!classMember) {
45-
classMember = new ClassMember();
46-
classMember.classDeclaration = classDeclaration;
50+
classMember = new ClassMember(classDeclaration);
4751
classMembers.set(memberName, classMember);
4852
}
4953
classMember.references.add(node);

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class DomFragment {
2424
/** @type {{key: string, value: Node}[]} */ style = [];
2525
/** @type {{key: string, value: Node}[]} */ eventListeners = [];
2626
/** @type {{key: string, value: Node|string}[]} */ bindings = [];
27-
/** @type {Node} */ textContent;
27+
/** @type {Node|undefined} */ textContent;
2828
/** @type {DomFragment[]} */ children = [];
2929
/** @type {DomFragment|undefined} */ parent;
3030
/** @type {string|undefined} */ expression;
@@ -255,7 +255,10 @@ function getKey(node, sourceCode) {
255255
}
256256
const property = getEnclosingProperty(node);
257257
if (property) {
258-
return ClassMember.getOrCreate(property, sourceCode);
258+
const classMember = ClassMember.getOrCreate(property, sourceCode);
259+
if (classMember) {
260+
return classMember;
261+
}
259262
}
260263
return node;
261264
}

0 commit comments

Comments
 (0)