|
| 1 | +/** |
| 2 | + * @fileoverview prevent variables defined in `<script setup>` to be marked as undefined |
| 3 | + * @author Yosuke Ota |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +const Module = require('module') |
| 8 | +const path = require('path') |
| 9 | +const utils = require('../utils') |
| 10 | +const AST = require('vue-eslint-parser').AST |
| 11 | + |
| 12 | +const ecmaVersion = 2020 |
| 13 | + |
| 14 | +// ------------------------------------------------------------------------------ |
| 15 | +// Rule Definition |
| 16 | +// ------------------------------------------------------------------------------ |
| 17 | + |
| 18 | +module.exports = { |
| 19 | + meta: { |
| 20 | + type: 'problem', |
| 21 | + docs: { |
| 22 | + description: |
| 23 | + 'prevent variables defined in `<script setup>` to be marked as undefined', // eslint-disable-line consistent-docs-description |
| 24 | + categories: ['base'], |
| 25 | + url: 'https://eslint.vuejs.org/rules/experimental-script-setup-vars.html' |
| 26 | + }, |
| 27 | + schema: [] |
| 28 | + }, |
| 29 | + /** |
| 30 | + * @param {RuleContext} context - The rule context. |
| 31 | + * @returns {RuleListener} AST event handlers. |
| 32 | + */ |
| 33 | + create(context) { |
| 34 | + const documentFragment = |
| 35 | + context.parserServices.getDocumentFragment && |
| 36 | + context.parserServices.getDocumentFragment() |
| 37 | + if (!documentFragment) { |
| 38 | + return {} |
| 39 | + } |
| 40 | + const sourceCode = context.getSourceCode() |
| 41 | + const scriptElement = documentFragment.children |
| 42 | + .filter(utils.isVElement) |
| 43 | + .find( |
| 44 | + (element) => |
| 45 | + element.name === 'script' && |
| 46 | + element.range[0] <= sourceCode.ast.range[0] && |
| 47 | + sourceCode.ast.range[1] <= element.range[1] |
| 48 | + ) |
| 49 | + if (!scriptElement) { |
| 50 | + return {} |
| 51 | + } |
| 52 | + const setupAttr = utils.getAttribute(scriptElement, 'setup') |
| 53 | + if (!setupAttr || !setupAttr.value) { |
| 54 | + return {} |
| 55 | + } |
| 56 | + const value = setupAttr.value.value |
| 57 | + |
| 58 | + let eslintScope |
| 59 | + try { |
| 60 | + eslintScope = getESLintModule('eslint-scope', () => |
| 61 | + // @ts-ignore |
| 62 | + require('eslint-scope') |
| 63 | + ) |
| 64 | + } catch (_e) { |
| 65 | + context.report({ |
| 66 | + node: setupAttr, |
| 67 | + message: 'Can not be resolved eslint-scope.' |
| 68 | + }) |
| 69 | + return {} |
| 70 | + } |
| 71 | + let espree |
| 72 | + try { |
| 73 | + espree = getESLintModule('espree', () => |
| 74 | + // @ts-ignore |
| 75 | + require('espree') |
| 76 | + ) |
| 77 | + } catch (_e) { |
| 78 | + context.report({ |
| 79 | + node: setupAttr, |
| 80 | + message: 'Can not be resolved espree.' |
| 81 | + }) |
| 82 | + return {} |
| 83 | + } |
| 84 | + |
| 85 | + const globalScope = sourceCode.scopeManager.scopes[0] |
| 86 | + |
| 87 | + /** @type {string[]} */ |
| 88 | + let vars |
| 89 | + try { |
| 90 | + vars = parseSetup(value, espree, eslintScope) |
| 91 | + } catch (_e) { |
| 92 | + context.report({ |
| 93 | + node: setupAttr.value, |
| 94 | + message: 'Parsing error.' |
| 95 | + }) |
| 96 | + return {} |
| 97 | + } |
| 98 | + |
| 99 | + // Define configured global variables. |
| 100 | + for (const id of vars) { |
| 101 | + const tempVariable = globalScope.set.get(id) |
| 102 | + |
| 103 | + /** @type {Variable} */ |
| 104 | + let variable |
| 105 | + if (!tempVariable) { |
| 106 | + variable = new eslintScope.Variable(id, globalScope) |
| 107 | + |
| 108 | + globalScope.variables.push(variable) |
| 109 | + globalScope.set.set(id, variable) |
| 110 | + } else { |
| 111 | + variable = tempVariable |
| 112 | + } |
| 113 | + |
| 114 | + variable.eslintImplicitGlobalSetting = 'readonly' |
| 115 | + variable.eslintExplicitGlobal = undefined |
| 116 | + variable.eslintExplicitGlobalComments = undefined |
| 117 | + variable.writeable = false |
| 118 | + } |
| 119 | + |
| 120 | + /* |
| 121 | + * "through" contains all references which definitions cannot be found. |
| 122 | + * Since we augment the global scope using configuration, we need to update |
| 123 | + * references and remove the ones that were added by configuration. |
| 124 | + */ |
| 125 | + globalScope.through = globalScope.through.filter((reference) => { |
| 126 | + const name = reference.identifier.name |
| 127 | + const variable = globalScope.set.get(name) |
| 128 | + |
| 129 | + if (variable) { |
| 130 | + /* |
| 131 | + * Links the variable and the reference. |
| 132 | + * And this reference is removed from `Scope#through`. |
| 133 | + */ |
| 134 | + reference.resolved = variable |
| 135 | + variable.references.push(reference) |
| 136 | + |
| 137 | + return false |
| 138 | + } |
| 139 | + |
| 140 | + return true |
| 141 | + }) |
| 142 | + |
| 143 | + return {} |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +/** |
| 148 | + * @param {string} code |
| 149 | + * @param {any} espree |
| 150 | + * @param {any} eslintScope |
| 151 | + * @returns {string[]} |
| 152 | + */ |
| 153 | +function parseSetup(code, espree, eslintScope) { |
| 154 | + /** @type {Program} */ |
| 155 | + const ast = espree.parse(`(${code})=>{}`, { ecmaVersion }) |
| 156 | + const result = eslintScope.analyze(ast, { |
| 157 | + ignoreEval: true, |
| 158 | + nodejsScope: false, |
| 159 | + ecmaVersion, |
| 160 | + sourceType: 'script', |
| 161 | + fallback: AST.getFallbackKeys |
| 162 | + }) |
| 163 | + |
| 164 | + const variables = /** @type {Variable[]} */ (result.globalScope.childScopes[0] |
| 165 | + .variables) |
| 166 | + |
| 167 | + return variables.map((v) => v.name) |
| 168 | +} |
| 169 | + |
| 170 | +const createRequire = |
| 171 | + // Added in v12.2.0 |
| 172 | + Module.createRequire || |
| 173 | + // Added in v10.12.0, but deprecated in v12.2.0. |
| 174 | + Module.createRequireFromPath || |
| 175 | + // Polyfill - This is not executed on the tests on node@>=10. |
| 176 | + /** |
| 177 | + * @param {string} filename |
| 178 | + */ |
| 179 | + function (filename) { |
| 180 | + const mod = new Module(filename) |
| 181 | + |
| 182 | + mod.filename = filename |
| 183 | + // @ts-ignore |
| 184 | + mod.paths = Module._nodeModulePaths(path.dirname(filename)) |
| 185 | + // @ts-ignore |
| 186 | + mod._compile('module.exports = require;', filename) |
| 187 | + return mod.exports |
| 188 | + } |
| 189 | + |
| 190 | +/** @type { { 'espree'?: any, 'eslint-scope'?: any } } */ |
| 191 | +const modulesCache = {} |
| 192 | + |
| 193 | +/** |
| 194 | + * @param {string} p |
| 195 | + */ |
| 196 | +function isLinterPath(p) { |
| 197 | + return ( |
| 198 | + // ESLint 6 and above |
| 199 | + p.includes(`eslint${path.sep}lib${path.sep}linter${path.sep}linter.js`) || |
| 200 | + // ESLint 5 |
| 201 | + p.includes(`eslint${path.sep}lib${path.sep}linter.js`) |
| 202 | + ) |
| 203 | +} |
| 204 | + |
| 205 | +/** |
| 206 | + * Load module from the loaded ESLint. |
| 207 | + * If the loaded ESLint was not found, just returns `fallback()`. |
| 208 | + * @param {'espree' | 'eslint-scope'} name |
| 209 | + * @param { () => any } fallback |
| 210 | + */ |
| 211 | +function getESLintModule(name, fallback) { |
| 212 | + if (!modulesCache[name]) { |
| 213 | + // Lookup the loaded eslint |
| 214 | + const linterPath = Object.keys(require.cache).find(isLinterPath) |
| 215 | + if (linterPath) { |
| 216 | + try { |
| 217 | + modulesCache[name] = createRequire(linterPath)(name) |
| 218 | + } catch (_e) { |
| 219 | + // ignore |
| 220 | + } |
| 221 | + } |
| 222 | + if (!modulesCache[name]) { |
| 223 | + modulesCache[name] = fallback() |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + return modulesCache[name] |
| 228 | +} |
0 commit comments