|
| 1 | +import { createRule } from '../utils/index.js'; |
| 2 | +import { defineWrapperListener, getProxyContent, getCoreRule } from '../utils/eslint-core.js'; |
| 3 | +import type { TSESTree } from '@typescript-eslint/types'; |
| 4 | +import type { Scope } from '@typescript-eslint/scope-manager'; |
| 5 | +import type { Range } from 'svelte-eslint-parser/lib/ast/common.js'; |
| 6 | +import { getScope as getScopeUtil } from '../utils/ast-utils.js'; |
| 7 | +import { getSourceCode as getSourceCodeCompat } from '../utils/compat.js'; |
| 8 | + |
| 9 | +const coreRule = getCoreRule('no-shadow'); |
| 10 | + |
| 11 | +function removeSnippetIdentifiers(snippetIdentifierNodeLocations: Range[], scope: Scope): Scope { |
| 12 | + return { |
| 13 | + ...scope, |
| 14 | + variables: scope.variables.filter((variable) => { |
| 15 | + return !snippetIdentifierNodeLocations.some(([start, end]) => { |
| 16 | + return variable.identifiers.every((identifier) => { |
| 17 | + const { range } = identifier; |
| 18 | + return range[0] === start && range[1] === end; |
| 19 | + }); |
| 20 | + }); |
| 21 | + }), |
| 22 | + childScopes: scope.childScopes.map((scope) => { |
| 23 | + return removeSnippetIdentifiers(snippetIdentifierNodeLocations, scope); |
| 24 | + }) |
| 25 | + } as Scope; |
| 26 | +} |
| 27 | + |
| 28 | +export default createRule('no-shadow', { |
| 29 | + meta: { |
| 30 | + ...coreRule.meta, |
| 31 | + docs: { |
| 32 | + description: coreRule.meta.docs.description, |
| 33 | + category: 'Best Practices', |
| 34 | + recommended: false, |
| 35 | + extensionRule: 'no-shadow' |
| 36 | + } |
| 37 | + }, |
| 38 | + create(context) { |
| 39 | + const snippetIdentifierNodeLocations: Range[] = []; |
| 40 | + |
| 41 | + function getScope(node: TSESTree.Node) { |
| 42 | + const scope = getScopeUtil(context, node); |
| 43 | + return removeSnippetIdentifiers(snippetIdentifierNodeLocations, scope); |
| 44 | + } |
| 45 | + |
| 46 | + function getSourceCode() { |
| 47 | + const sourceCode = getSourceCodeCompat(context); |
| 48 | + return new Proxy(sourceCode, { |
| 49 | + get(target, key) { |
| 50 | + if (key === 'getScope') { |
| 51 | + return getScope; |
| 52 | + } |
| 53 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any -- ignore |
| 54 | + return (target as any)[key]; |
| 55 | + } |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + return defineWrapperListener( |
| 60 | + coreRule, |
| 61 | + getProxyContent(context, { |
| 62 | + sourceCode: getSourceCode() |
| 63 | + }), |
| 64 | + { |
| 65 | + createListenerProxy(coreListener) { |
| 66 | + return { |
| 67 | + ...coreListener, |
| 68 | + SvelteSnippetBlock(node) { |
| 69 | + const parent = node.parent; |
| 70 | + if (parent.type === 'SvelteElement' && parent.kind === 'component') { |
| 71 | + snippetIdentifierNodeLocations.push(node.id.range); |
| 72 | + } |
| 73 | + coreListener.SvelteSnippetBlock?.(node); |
| 74 | + }, |
| 75 | + 'Program:exit'(node) { |
| 76 | + coreListener['Program:exit']?.(node); |
| 77 | + } |
| 78 | + }; |
| 79 | + } |
| 80 | + } |
| 81 | + ); |
| 82 | + } |
| 83 | +}); |
0 commit comments