Skip to content

Commit 5cab0e9

Browse files
JerryWu1234wuls
andauthored
Fix: Enhance lexical scope and skip variables declared by Qwik's internal hooks. (#7366)
* fix: a bug is about qwik ts error * When it is a Qwik hook, there is no need for recursive lookup. * Create wicked-dolphins-boil.md * modify comment --------- Co-authored-by: wuls <[email protected]>
1 parent 0cf1eaf commit 5cab0e9

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

.changeset/wicked-dolphins-boil.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'eslint-plugin-qwik': patch
3+
---
4+
5+
FIX: Enhance lexical scope and skip variables declared by Qwik's internal hooks.

packages/eslint-plugin-qwik/src/validLexicalScope.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ export const validLexicalScope = createRule({
7575
if (variableType === 'ImportBinding') {
7676
return;
7777
}
78+
79+
if (isQwikHook(declaredVariable, context)) {
80+
return;
81+
}
82+
7883
let dollarScope: Scope.Scope | null = ref.from;
7984
let dollarIdentifier: string | undefined;
8085
while (dollarScope) {
@@ -478,6 +483,41 @@ function getContent(symbol: ts.Symbol, sourceCode: string) {
478483
return '';
479484
}
480485

486+
function isQwikHook(variable, context) {
487+
const def = variable.defs[0];
488+
if (!def || def.type !== 'Variable') {
489+
return false;
490+
}
491+
492+
const init = def.node.init;
493+
if (
494+
init?.type === 'CallExpression' &&
495+
init.callee.type === 'Identifier' &&
496+
/^use[A-Z]/.test(init.callee.name)
497+
) {
498+
const hookName = init.callee.name;
499+
const scope = context.sourceCode.getScope(def.node);
500+
const ref = scope.references.find((r) => r.identifier.name === hookName);
501+
502+
return ref?.resolved && isFromQwikModule(ref.resolved, context);
503+
}
504+
return false;
505+
}
506+
507+
function isFromQwikModule(resolvedVar) {
508+
return resolvedVar.defs.some((def) => {
509+
if (def.type !== 'ImportBinding') {
510+
return false;
511+
}
512+
const importSource = def.parent.source.value;
513+
514+
return (
515+
importSource.startsWith('@builder.io/qwik') ||
516+
importSource.startsWith('@builder.io/qwik-city')
517+
);
518+
});
519+
}
520+
481521
const ALLOWED_CLASSES = {
482522
Promise: true,
483523
URL: true,
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { component$, useComputed$ } from '@builder.io/qwik';
2+
import { useDocumentHead } from '@builder.io/qwik-city';
3+
import { useLocation as exampleTest } from '@builder.io/qwik-city';
4+
const loc = exampleTest();
5+
6+
export default component$(() => {
7+
const head = useDocumentHead();
8+
const authorId = useComputed$(() => {
9+
return head.meta; // <--- EESLint was not happy here, but now it is
10+
});
11+
return (
12+
<>
13+
{authorId.value}
14+
<p>pathname: {loc.url.pathname}</p>
15+
<p>skuId: {loc.params.skuId}</p>
16+
</>
17+
);
18+
});

0 commit comments

Comments
 (0)