@ghostvue/eslint-plugin 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +36 -2
- package/dist/.registries/component-index.generated.d.ts +2189 -0
- package/dist/.registries/component-index.generated.js +3070 -0
- package/dist/component-index.d.ts +33 -0
- package/dist/component-index.js +43 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.js +27 -0
- package/dist/native-attributes.d.ts +11 -0
- package/dist/native-attributes.js +229 -0
- package/dist/rules/docs-url.d.ts +2 -0
- package/dist/rules/docs-url.js +5 -0
- package/dist/rules/no-undeclared-v-model.d.ts +7 -0
- package/dist/rules/no-undeclared-v-model.js +52 -0
- package/dist/rules/no-unknown-props.d.ts +10 -0
- package/dist/rules/no-unknown-props.js +75 -0
- package/dist/rules/no-unknown-slots.d.ts +9 -0
- package/dist/rules/no-unknown-slots.js +92 -0
- package/dist/template.d.ts +28 -0
- package/dist/template.js +145 -0
- package/package.json +46 -4
package/dist/template.js
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { resolveGhostComponentPackage, lookupComponentContract, } from './component-index.js';
|
|
2
|
+
import { camelize } from './native-attributes.js';
|
|
3
|
+
function isVueParserServices(services) {
|
|
4
|
+
if (typeof services !== 'object' || services === null)
|
|
5
|
+
return false;
|
|
6
|
+
return ('defineTemplateBodyVisitor' in services &&
|
|
7
|
+
typeof services.defineTemplateBodyVisitor === 'function' &&
|
|
8
|
+
'getDocumentFragment' in services &&
|
|
9
|
+
typeof services.getDocumentFragment === 'function');
|
|
10
|
+
}
|
|
11
|
+
const NO_BINDINGS = {
|
|
12
|
+
named: new Map(),
|
|
13
|
+
namespaces: new Map(),
|
|
14
|
+
};
|
|
15
|
+
function hasScriptSetup(fragment) {
|
|
16
|
+
return (fragment?.children ?? []).some((child) => child.type === 'VElement' &&
|
|
17
|
+
child.name === 'script' &&
|
|
18
|
+
child.startTag.attributes.some((attribute) => !attribute.directive && attribute.key.name === 'setup'));
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Template-visible imports from GhostVue packages. Only `<script setup>`
|
|
22
|
+
* exposes imports to the template; Options API `components` registration and
|
|
23
|
+
* global `app.component` registration are not resolved, so those tags stay
|
|
24
|
+
* unchecked rather than guessed.
|
|
25
|
+
*/
|
|
26
|
+
function collectBindings(program, fragment) {
|
|
27
|
+
if (!hasScriptSetup(fragment))
|
|
28
|
+
return NO_BINDINGS;
|
|
29
|
+
const named = new Map();
|
|
30
|
+
const namespaces = new Map();
|
|
31
|
+
for (const statement of program.body) {
|
|
32
|
+
if (statement.type !== 'ImportDeclaration')
|
|
33
|
+
continue;
|
|
34
|
+
const source = statement.source.value;
|
|
35
|
+
if (typeof source !== 'string')
|
|
36
|
+
continue;
|
|
37
|
+
const packageName = resolveGhostComponentPackage(source);
|
|
38
|
+
if (!packageName)
|
|
39
|
+
continue;
|
|
40
|
+
// `import type` (a typescript-eslint extension of ESTree) cannot bind a
|
|
41
|
+
// runtime component.
|
|
42
|
+
if (isTypeOnly(statement))
|
|
43
|
+
continue;
|
|
44
|
+
for (const specifier of statement.specifiers) {
|
|
45
|
+
if (specifier.type === 'ImportNamespaceSpecifier') {
|
|
46
|
+
namespaces.set(specifier.local.name, packageName);
|
|
47
|
+
}
|
|
48
|
+
else if (specifier.type === 'ImportSpecifier' &&
|
|
49
|
+
!isTypeOnly(specifier)) {
|
|
50
|
+
const exportName = specifier.imported.type === 'Identifier'
|
|
51
|
+
? specifier.imported.name
|
|
52
|
+
: String(specifier.imported.value);
|
|
53
|
+
named.set(specifier.local.name, { packageName, exportName });
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return { named, namespaces };
|
|
58
|
+
}
|
|
59
|
+
function isTypeOnly(node) {
|
|
60
|
+
return 'importKind' in node && node.importKind === 'type';
|
|
61
|
+
}
|
|
62
|
+
function capitalize(name) {
|
|
63
|
+
return name.charAt(0).toUpperCase() + name.slice(1);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Resolve a tag the way Vue's `<script setup>` compiler does: the raw tag,
|
|
67
|
+
* then camelCase, then PascalCase (`ghost-button` -> `GhostButton`), or a
|
|
68
|
+
* namespace member (`<G.GhostButton>`). Lowercase tags without a hyphen are
|
|
69
|
+
* native elements to Vue's compiler before any binding lookup, so they are
|
|
70
|
+
* never treated as components.
|
|
71
|
+
*/
|
|
72
|
+
function resolveTag(rawName, bindings) {
|
|
73
|
+
const dot = rawName.indexOf('.');
|
|
74
|
+
if (dot > 0) {
|
|
75
|
+
const packageName = bindings.namespaces.get(rawName.slice(0, dot));
|
|
76
|
+
return packageName
|
|
77
|
+
? { packageName, exportName: rawName.slice(dot + 1) }
|
|
78
|
+
: undefined;
|
|
79
|
+
}
|
|
80
|
+
if (/^[a-z][a-z0-9]*$/.test(rawName))
|
|
81
|
+
return undefined;
|
|
82
|
+
const camel = camelize(rawName);
|
|
83
|
+
return (bindings.named.get(rawName) ??
|
|
84
|
+
bindings.named.get(camel) ??
|
|
85
|
+
bindings.named.get(capitalize(camel)));
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Shared rule scaffold: visits every template element that resolves to a
|
|
89
|
+
* GhostVue component with a manifest and hands it to `handler`. Everything
|
|
90
|
+
* else (native elements, unresolved tags, exports without a manifest,
|
|
91
|
+
* `<component :is>`) is skipped, because a diagnostic requires positive
|
|
92
|
+
* evidence.
|
|
93
|
+
*/
|
|
94
|
+
export function createGhostTemplateVisitor(context, handler) {
|
|
95
|
+
const services = context.sourceCode.parserServices;
|
|
96
|
+
if (!isVueParserServices(services)) {
|
|
97
|
+
// Mirrors eslint-plugin-vue: a `.vue` file without vue-eslint-parser would
|
|
98
|
+
// otherwise pass silently with every rule disabled in effect.
|
|
99
|
+
if (context.filename.endsWith('.vue')) {
|
|
100
|
+
return {
|
|
101
|
+
Program() {
|
|
102
|
+
context.report({
|
|
103
|
+
loc: { line: 1, column: 0 },
|
|
104
|
+
message: 'GhostVue rules need vue-eslint-parser for .vue files. Use eslint-plugin-vue flat configs or set languageOptions.parser to vue-eslint-parser.',
|
|
105
|
+
});
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
return {};
|
|
110
|
+
}
|
|
111
|
+
let bindings;
|
|
112
|
+
return services.defineTemplateBodyVisitor({
|
|
113
|
+
VElement(element) {
|
|
114
|
+
// Template traversal runs after the script body is parsed, so the
|
|
115
|
+
// import list is complete the first time an element is visited.
|
|
116
|
+
bindings ??= collectBindings(context.sourceCode.ast, services.getDocumentFragment());
|
|
117
|
+
const binding = resolveTag(element.rawName, bindings);
|
|
118
|
+
if (!binding)
|
|
119
|
+
return;
|
|
120
|
+
const contract = lookupComponentContract(binding.packageName, binding.exportName);
|
|
121
|
+
if (!contract)
|
|
122
|
+
return;
|
|
123
|
+
handler(element, {
|
|
124
|
+
name: binding.exportName,
|
|
125
|
+
packageName: binding.packageName,
|
|
126
|
+
contract,
|
|
127
|
+
});
|
|
128
|
+
},
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Directive argument: `null` when absent (`v-model`, `v-slot`), the raw name
|
|
133
|
+
* when static (`v-model:open`), and `undefined` when dynamic (`#[name]`), which
|
|
134
|
+
* callers must skip.
|
|
135
|
+
*/
|
|
136
|
+
export function staticArgument(directive) {
|
|
137
|
+
const argument = directive.key.argument;
|
|
138
|
+
if (argument === null)
|
|
139
|
+
return null;
|
|
140
|
+
return argument.type === 'VIdentifier' ? argument.rawName : undefined;
|
|
141
|
+
}
|
|
142
|
+
/** Comma-separated list for messages, or a stand-in when empty. */
|
|
143
|
+
export function formatNames(names) {
|
|
144
|
+
return names.length > 0 ? names.join(', ') : '(none)';
|
|
145
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,48 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ghostvue/eslint-plugin",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "ESLint rules that check GhostVue component props, slots, and v-model bindings in Vue templates against the component manifests.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/zprue743/ghostvue.git",
|
|
9
|
+
"directory": "packages/eslint-plugin"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"eslint",
|
|
13
|
+
"eslintplugin",
|
|
14
|
+
"eslint-plugin",
|
|
15
|
+
"vue",
|
|
16
|
+
"ghostvue"
|
|
17
|
+
],
|
|
18
|
+
"type": "module",
|
|
19
|
+
"sideEffects": false,
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"import": "./dist/index.js"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"files": [
|
|
28
|
+
"dist"
|
|
29
|
+
],
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public",
|
|
32
|
+
"provenance": true
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"eslint": "^9.0.0 || ^10.0.0",
|
|
36
|
+
"vue-eslint-parser": "^10.0.0"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"eslint": "10.15.0",
|
|
40
|
+
"vue-eslint-parser": "10.4.1"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"prebuild": "pnpm -w registries:write",
|
|
44
|
+
"build": "tsc -p tsconfig.build.json",
|
|
45
|
+
"pretypecheck": "pnpm -w registries:write",
|
|
46
|
+
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
47
|
+
}
|
|
48
|
+
}
|