MCPcopy
hub / github.com/ampproject/amphtml / callQuerySelector

Function callQuerySelector

build-system/eslint-rules/query-selector.js:9–68  ·  view source on GitHub ↗

* @param {CompilerNode} node

(node)

Source from the content-addressed store, hash-verified

7 * @param {CompilerNode} node
8 */
9 function callQuerySelector(node) {
10 const {callee} = node;
11
12 // If it's not a querySelector(All) call, I don't care about it.
13 const {property} = callee;
14 if (
15 property.type !== 'Identifier' ||
16 !property.name.startsWith('querySelector')
17 ) {
18 return;
19 }
20
21 const leadingComments = context.getCommentsBefore(property);
22 const ok = leadingComments.some((comment) => {
23 return comment.value === 'OK';
24 });
25 if (ok) {
26 return;
27 }
28
29 const selector = getSelector(node, 0);
30
31 if (!isValidSelector(selector)) {
32 context.report({
33 node,
34 message: 'Failed to parse CSS Selector `' + selector + '`',
35 });
36 return;
37 }
38
39 // What are we calling querySelector on?
40 let obj = callee.object;
41 if (obj.type === 'CallExpression') {
42 obj = obj.callee;
43 }
44 if (obj.type === 'MemberExpression') {
45 obj = obj.property;
46 }
47
48 // Any query selector is allowed on document
49 // This check must be done after getting the selector, to ensure the
50 // selector adheres to escaping requirements.
51 if (obj.type === 'Identifier' && /[dD]oc|[rR]oot/.test(obj.name)) {
52 return;
53 }
54
55 if (!selectorNeedsScope(selector)) {
56 return;
57 }
58
59 context.report({
60 node,
61 message:
62 'querySelector is not scoped to the element, but ' +
63 'globally and filtered to just the elements inside the element. ' +
64 'This leads to obscure bugs if you attempt to match a descendant ' +
65 'of a descendant (ie querySelector("div div")). Instead, use the ' +
66 'scopedQuerySelector in src/core/dom/query.js',

Callers 1

CallExpressionFunction · 0.85

Calls 5

isValidSelectorFunction · 0.85
selectorNeedsScopeFunction · 0.85
getSelectorFunction · 0.70
reportMethod · 0.45
testMethod · 0.45

Tested by

no test coverage detected