eslint-plugin-vue
Version:
Official ESLint plugin for Vue.js
55 lines (54 loc) • 2.27 kB
JavaScript
const require_index = require("../utils/index.js");
// lib/rules/no-props-shadow.ts
/**
* @author Valentin Yushkevich
* See LICENSE file in root directory for full license.
*/
/**
* Checks whether the given variable only exists in the type space.
* e.g. `type Foo = {}`, `interface Foo {}`, `import type { Foo } from './foo'`
* Such a declaration cannot be referenced from the template,
* so it never shadows a prop.
*/
function isTypeOnlyVariable(variable) {
return variable.defs.length === 0 || variable.isTypeVariable && !variable.isValueVariable || variable.defs.every((def) => def.type === "ImportBinding" && (def.parent.importKind === "type" || def.node.type === "ImportSpecifier" && def.node.importKind === "type"));
}
var no_props_shadow_default = {
meta: {
type: "problem",
docs: {
description: "disallow declarations that shadow props in `<script setup>`",
categories: void 0,
url: "https://eslint.vuejs.org/rules/no-props-shadow.html"
},
fixable: null,
schema: [],
messages: { shadow: "Declaration of '{{name}}' shadows the prop with the same name." }
},
create(context) {
const propNames = /* @__PURE__ */ new Set();
const propsDeclarators = /* @__PURE__ */ new Set();
return require_index.default.defineScriptSetupVisitor(context, {
onDefinePropsEnter(node, props) {
for (const prop of props) if (prop.propName != null) propNames.add(prop.propName);
const target = require_index.default.hasWithDefaults(node) ? node.parent : node;
if (target.parent?.type === "VariableDeclarator") propsDeclarators.add(target.parent);
},
"Program:exit"() {
if (propNames.size === 0) return;
const globalScope = context.sourceCode.scopeManager.globalScope;
if (!globalScope) return;
const moduleScope = globalScope.childScopes.find((scope) => scope.type === "module") || globalScope;
for (const variable of moduleScope.variables) {
if (!propNames.has(variable.name) || isTypeOnlyVariable(variable) || variable.defs.some((def) => def.type === "Variable" && propsDeclarators.has(def.node))) continue;
context.report({
node: variable.defs[0].name,
messageId: "shadow",
data: { name: variable.name }
});
}
}
});
}
};
exports.default = no_props_shadow_default;