@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
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** Package specifiers whose component exports the rules can resolve. */
|
|
2
|
+
export type GhostComponentPackage = '@ghostvue/vue' | '@ghostvue/themes';
|
|
3
|
+
/**
|
|
4
|
+
* Statically checkable usage contract for one component export, derived from
|
|
5
|
+
* its manifest. Names are the manifest's camelCase prop names, v-model prop
|
|
6
|
+
* names, and slot names.
|
|
7
|
+
*/
|
|
8
|
+
export interface GhostComponentContract {
|
|
9
|
+
readonly props: readonly string[];
|
|
10
|
+
readonly models: readonly string[];
|
|
11
|
+
/**
|
|
12
|
+
* `null` when slot names come from consumer data (for example one slot per
|
|
13
|
+
* DataTable column id), so they cannot be checked statically.
|
|
14
|
+
*/
|
|
15
|
+
readonly slots: readonly string[] | null;
|
|
16
|
+
/** Page under `docs/components/` (without `.md`) that documents the component. */
|
|
17
|
+
readonly docs: string;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Package whose component contracts apply to imports from `source`, or
|
|
21
|
+
* `undefined` when the rules do not resolve components from it.
|
|
22
|
+
*/
|
|
23
|
+
export declare function resolveGhostComponentPackage(source: string): GhostComponentPackage | undefined;
|
|
24
|
+
/**
|
|
25
|
+
* Contract for `exportName` from `packageName`, or `undefined` when that export
|
|
26
|
+
* has no manifest (helpers, names the package does not export). A themed
|
|
27
|
+
* compound part without a themed manifest carries the headless contract it
|
|
28
|
+
* forwards to (see `buildEslintComponentIndex`). Callers must treat `undefined`
|
|
29
|
+
* as "no evidence" and stay silent.
|
|
30
|
+
*/
|
|
31
|
+
export declare function lookupComponentContract(packageName: GhostComponentPackage, exportName: string): GhostComponentContract | undefined;
|
|
32
|
+
/** Public docs URL for a contract, used in diagnostics. */
|
|
33
|
+
export declare function componentDocsUrl(contract: GhostComponentContract): string;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { generatedComponentIndex } from './.registries/component-index.generated.js';
|
|
2
|
+
// Assignment (not an assertion) makes the compiler check the generated literal
|
|
3
|
+
// against the contract shape, so a generator change cannot silently drift.
|
|
4
|
+
const componentIndex = generatedComponentIndex;
|
|
5
|
+
const DOCS_BASE_URL = 'https://github.com/zprue743/ghostvue/blob/master/docs/components/';
|
|
6
|
+
/**
|
|
7
|
+
* Import specifiers that re-export a package's components unchanged.
|
|
8
|
+
* `@ghostvue/themes/components` (the opt-in theme CSS entry) exports exactly
|
|
9
|
+
* the `@ghostvue/themes` wrappers without loading `default.css`, so its tags
|
|
10
|
+
* are checked against the themed contracts and reported as `@ghostvue/themes`.
|
|
11
|
+
*/
|
|
12
|
+
const componentPackageAliases = { '@ghostvue/themes/components': '@ghostvue/themes' };
|
|
13
|
+
function isIndexedPackage(source) {
|
|
14
|
+
return Object.hasOwn(componentIndex, source);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Package whose component contracts apply to imports from `source`, or
|
|
18
|
+
* `undefined` when the rules do not resolve components from it.
|
|
19
|
+
*/
|
|
20
|
+
export function resolveGhostComponentPackage(source) {
|
|
21
|
+
if (isIndexedPackage(source))
|
|
22
|
+
return source;
|
|
23
|
+
return Object.hasOwn(componentPackageAliases, source)
|
|
24
|
+
? componentPackageAliases[source]
|
|
25
|
+
: undefined;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Contract for `exportName` from `packageName`, or `undefined` when that export
|
|
29
|
+
* has no manifest (helpers, names the package does not export). A themed
|
|
30
|
+
* compound part without a themed manifest carries the headless contract it
|
|
31
|
+
* forwards to (see `buildEslintComponentIndex`). Callers must treat `undefined`
|
|
32
|
+
* as "no evidence" and stay silent.
|
|
33
|
+
*/
|
|
34
|
+
export function lookupComponentContract(packageName, exportName) {
|
|
35
|
+
const components = componentIndex[packageName];
|
|
36
|
+
return Object.hasOwn(components, exportName)
|
|
37
|
+
? components[exportName]
|
|
38
|
+
: undefined;
|
|
39
|
+
}
|
|
40
|
+
/** Public docs URL for a contract, used in diagnostics. */
|
|
41
|
+
export function componentDocsUrl(contract) {
|
|
42
|
+
return `${DOCS_BASE_URL}${contract.docs}.md`;
|
|
43
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public entry point for `@ghostvue/eslint-plugin`: manifest-based usage rules
|
|
3
|
+
* for GhostVue components in Vue templates.
|
|
4
|
+
*/
|
|
5
|
+
import type { ESLint, Linter, Rule } from 'eslint';
|
|
6
|
+
/** Rules keyed by name, without the plugin namespace. */
|
|
7
|
+
export declare const rules: {
|
|
8
|
+
'no-unknown-props': Rule.RuleModule;
|
|
9
|
+
'no-unknown-slots': Rule.RuleModule;
|
|
10
|
+
'no-undeclared-v-model': Rule.RuleModule;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* The plugin object for flat config. Register it under the `ghostvue`
|
|
14
|
+
* namespace (`plugins: { ghostvue }`) so rule ids match the docs and the
|
|
15
|
+
* recommended config.
|
|
16
|
+
*/
|
|
17
|
+
export interface GhostVueEslintPlugin extends ESLint.Plugin {
|
|
18
|
+
meta: {
|
|
19
|
+
name: string;
|
|
20
|
+
};
|
|
21
|
+
rules: typeof rules;
|
|
22
|
+
configs: {
|
|
23
|
+
/**
|
|
24
|
+
* Enables every rule as an error for `**\/*.vue`. Does not set a parser:
|
|
25
|
+
* combine it with eslint-plugin-vue's flat configs, which install
|
|
26
|
+
* vue-eslint-parser for `.vue` files.
|
|
27
|
+
*/
|
|
28
|
+
recommended: Linter.Config;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
declare const plugin: GhostVueEslintPlugin;
|
|
32
|
+
export default plugin;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { noUndeclaredVModel } from './rules/no-undeclared-v-model.js';
|
|
2
|
+
import { noUnknownProps } from './rules/no-unknown-props.js';
|
|
3
|
+
import { noUnknownSlots } from './rules/no-unknown-slots.js';
|
|
4
|
+
/** Rules keyed by name, without the plugin namespace. */
|
|
5
|
+
export const rules = {
|
|
6
|
+
'no-unknown-props': noUnknownProps,
|
|
7
|
+
'no-unknown-slots': noUnknownSlots,
|
|
8
|
+
'no-undeclared-v-model': noUndeclaredVModel,
|
|
9
|
+
};
|
|
10
|
+
const recommended = {
|
|
11
|
+
name: 'ghostvue/recommended',
|
|
12
|
+
files: ['**/*.vue'],
|
|
13
|
+
rules: {
|
|
14
|
+
'ghostvue/no-unknown-props': 'error',
|
|
15
|
+
'ghostvue/no-unknown-slots': 'error',
|
|
16
|
+
'ghostvue/no-undeclared-v-model': 'error',
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
const plugin = {
|
|
20
|
+
meta: { name: '@ghostvue/eslint-plugin' },
|
|
21
|
+
rules,
|
|
22
|
+
configs: { recommended },
|
|
23
|
+
};
|
|
24
|
+
// The recommended config references the plugin object itself, so it is
|
|
25
|
+
// attached after construction instead of in the literal above.
|
|
26
|
+
recommended.plugins = { ghostvue: plugin };
|
|
27
|
+
export default plugin;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** `fooBar` -> `foo-bar`, matching Vue's `hyphenate`. */
|
|
2
|
+
export declare function hyphenate(name: string): string;
|
|
3
|
+
/** `foo-bar` -> `fooBar`, matching Vue's `camelize`. */
|
|
4
|
+
export declare function camelize(name: string): string;
|
|
5
|
+
/**
|
|
6
|
+
* True when `name` is an attribute the rules must never report: ARIA and
|
|
7
|
+
* `data-*` attributes, native HTML/SVG attributes in either kebab or camel
|
|
8
|
+
* case, Vue reserved attributes, and listener bindings (`:onClick`,
|
|
9
|
+
* `onclick`), which are events rather than props.
|
|
10
|
+
*/
|
|
11
|
+
export declare function isAllowedNonPropAttribute(name: string): boolean;
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Attribute names that are valid on some HTML or SVG element. Components fall
|
|
3
|
+
* through attributes they do not declare as props to their root element, so a
|
|
4
|
+
* native name is never evidence of a mistake even when the component does not
|
|
5
|
+
* declare it. The list is the union across elements on purpose: manifests do
|
|
6
|
+
* not record which element a component renders, and a missed typo is cheaper
|
|
7
|
+
* than a false error.
|
|
8
|
+
*
|
|
9
|
+
* Kept local instead of adding a dependency: the set changes rarely, and only
|
|
10
|
+
* membership (not per-element validity) matters here.
|
|
11
|
+
*/
|
|
12
|
+
const NATIVE_ATTRIBUTES = new Set([
|
|
13
|
+
// HTML (WHATWG attribute index, including global attributes).
|
|
14
|
+
'accept',
|
|
15
|
+
'accept-charset',
|
|
16
|
+
'accesskey',
|
|
17
|
+
'action',
|
|
18
|
+
'allow',
|
|
19
|
+
'allowfullscreen',
|
|
20
|
+
'alpha',
|
|
21
|
+
'alt',
|
|
22
|
+
'as',
|
|
23
|
+
'async',
|
|
24
|
+
'autocapitalize',
|
|
25
|
+
'autocomplete',
|
|
26
|
+
'autocorrect',
|
|
27
|
+
'autofocus',
|
|
28
|
+
'autoplay',
|
|
29
|
+
'blocking',
|
|
30
|
+
'charset',
|
|
31
|
+
'checked',
|
|
32
|
+
'cite',
|
|
33
|
+
'class',
|
|
34
|
+
'closedby',
|
|
35
|
+
'color',
|
|
36
|
+
'colorspace',
|
|
37
|
+
'cols',
|
|
38
|
+
'colspan',
|
|
39
|
+
'command',
|
|
40
|
+
'commandfor',
|
|
41
|
+
'content',
|
|
42
|
+
'contenteditable',
|
|
43
|
+
'controls',
|
|
44
|
+
'coords',
|
|
45
|
+
'crossorigin',
|
|
46
|
+
'data',
|
|
47
|
+
'datetime',
|
|
48
|
+
'decoding',
|
|
49
|
+
'default',
|
|
50
|
+
'defer',
|
|
51
|
+
'dir',
|
|
52
|
+
'dirname',
|
|
53
|
+
'disabled',
|
|
54
|
+
'download',
|
|
55
|
+
'draggable',
|
|
56
|
+
'enctype',
|
|
57
|
+
'enterkeyhint',
|
|
58
|
+
'exportparts',
|
|
59
|
+
'fetchpriority',
|
|
60
|
+
'for',
|
|
61
|
+
'form',
|
|
62
|
+
'formaction',
|
|
63
|
+
'formenctype',
|
|
64
|
+
'formmethod',
|
|
65
|
+
'formnovalidate',
|
|
66
|
+
'formtarget',
|
|
67
|
+
'headers',
|
|
68
|
+
'height',
|
|
69
|
+
'hidden',
|
|
70
|
+
'high',
|
|
71
|
+
'href',
|
|
72
|
+
'hreflang',
|
|
73
|
+
'http-equiv',
|
|
74
|
+
'id',
|
|
75
|
+
'imagesizes',
|
|
76
|
+
'imagesrcset',
|
|
77
|
+
'inert',
|
|
78
|
+
'inputmode',
|
|
79
|
+
'integrity',
|
|
80
|
+
'is',
|
|
81
|
+
'ismap',
|
|
82
|
+
'itemid',
|
|
83
|
+
'itemprop',
|
|
84
|
+
'itemref',
|
|
85
|
+
'itemscope',
|
|
86
|
+
'itemtype',
|
|
87
|
+
'kind',
|
|
88
|
+
'label',
|
|
89
|
+
'lang',
|
|
90
|
+
'list',
|
|
91
|
+
'loading',
|
|
92
|
+
'loop',
|
|
93
|
+
'low',
|
|
94
|
+
'max',
|
|
95
|
+
'maxlength',
|
|
96
|
+
'media',
|
|
97
|
+
'method',
|
|
98
|
+
'min',
|
|
99
|
+
'minlength',
|
|
100
|
+
'multiple',
|
|
101
|
+
'muted',
|
|
102
|
+
'name',
|
|
103
|
+
'nomodule',
|
|
104
|
+
'nonce',
|
|
105
|
+
'novalidate',
|
|
106
|
+
'open',
|
|
107
|
+
'optimum',
|
|
108
|
+
'part',
|
|
109
|
+
'pattern',
|
|
110
|
+
'ping',
|
|
111
|
+
'placeholder',
|
|
112
|
+
'playsinline',
|
|
113
|
+
'popover',
|
|
114
|
+
'popovertarget',
|
|
115
|
+
'popovertargetaction',
|
|
116
|
+
'poster',
|
|
117
|
+
'preload',
|
|
118
|
+
'readonly',
|
|
119
|
+
'referrerpolicy',
|
|
120
|
+
'rel',
|
|
121
|
+
'required',
|
|
122
|
+
'reversed',
|
|
123
|
+
'role',
|
|
124
|
+
'rows',
|
|
125
|
+
'rowspan',
|
|
126
|
+
'sandbox',
|
|
127
|
+
'scope',
|
|
128
|
+
'selected',
|
|
129
|
+
'shadowrootclonable',
|
|
130
|
+
'shadowrootdelegatesfocus',
|
|
131
|
+
'shadowrootmode',
|
|
132
|
+
'shadowrootserializable',
|
|
133
|
+
'shape',
|
|
134
|
+
'size',
|
|
135
|
+
'sizes',
|
|
136
|
+
'slot',
|
|
137
|
+
'span',
|
|
138
|
+
'spellcheck',
|
|
139
|
+
'src',
|
|
140
|
+
'srcdoc',
|
|
141
|
+
'srclang',
|
|
142
|
+
'srcset',
|
|
143
|
+
'start',
|
|
144
|
+
'step',
|
|
145
|
+
'style',
|
|
146
|
+
'switch',
|
|
147
|
+
'tabindex',
|
|
148
|
+
'target',
|
|
149
|
+
'title',
|
|
150
|
+
'translate',
|
|
151
|
+
'type',
|
|
152
|
+
'usemap',
|
|
153
|
+
'value',
|
|
154
|
+
'virtualkeyboardpolicy',
|
|
155
|
+
'width',
|
|
156
|
+
'wrap',
|
|
157
|
+
'writingsuggestions',
|
|
158
|
+
// DOM properties Vue sets directly when bound on an element root.
|
|
159
|
+
'innerhtml',
|
|
160
|
+
'innertext',
|
|
161
|
+
'textcontent',
|
|
162
|
+
// Common SVG attributes for components that render an SVG root or icon.
|
|
163
|
+
'clip-path',
|
|
164
|
+
'clip-rule',
|
|
165
|
+
'cx',
|
|
166
|
+
'cy',
|
|
167
|
+
'd',
|
|
168
|
+
'fill',
|
|
169
|
+
'fill-opacity',
|
|
170
|
+
'fill-rule',
|
|
171
|
+
'focusable',
|
|
172
|
+
'opacity',
|
|
173
|
+
'points',
|
|
174
|
+
'preserveaspectratio',
|
|
175
|
+
'r',
|
|
176
|
+
'rx',
|
|
177
|
+
'ry',
|
|
178
|
+
'stroke',
|
|
179
|
+
'stroke-dasharray',
|
|
180
|
+
'stroke-dashoffset',
|
|
181
|
+
'stroke-linecap',
|
|
182
|
+
'stroke-linejoin',
|
|
183
|
+
'stroke-opacity',
|
|
184
|
+
'stroke-width',
|
|
185
|
+
'transform',
|
|
186
|
+
'viewbox',
|
|
187
|
+
'x',
|
|
188
|
+
'x1',
|
|
189
|
+
'x2',
|
|
190
|
+
'xmlns',
|
|
191
|
+
'y',
|
|
192
|
+
'y1',
|
|
193
|
+
'y2',
|
|
194
|
+
]);
|
|
195
|
+
/**
|
|
196
|
+
* Names Vue consumes itself or never forwards as a component prop: special
|
|
197
|
+
* attributes and the Vue 2 slot syntax that Vue 3 still parses.
|
|
198
|
+
*/
|
|
199
|
+
const VUE_RESERVED_ATTRIBUTES = new Set([
|
|
200
|
+
'key',
|
|
201
|
+
'ref',
|
|
202
|
+
'ref_for',
|
|
203
|
+
'ref_key',
|
|
204
|
+
'slot-scope',
|
|
205
|
+
]);
|
|
206
|
+
/** `fooBar` -> `foo-bar`, matching Vue's `hyphenate`. */
|
|
207
|
+
export function hyphenate(name) {
|
|
208
|
+
return name.replace(/\B([A-Z])/g, '-$1').toLowerCase();
|
|
209
|
+
}
|
|
210
|
+
/** `foo-bar` -> `fooBar`, matching Vue's `camelize`. */
|
|
211
|
+
export function camelize(name) {
|
|
212
|
+
return name.replace(/-(\w)/g, (_, letter) => letter.toUpperCase());
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* True when `name` is an attribute the rules must never report: ARIA and
|
|
216
|
+
* `data-*` attributes, native HTML/SVG attributes in either kebab or camel
|
|
217
|
+
* case, Vue reserved attributes, and listener bindings (`:onClick`,
|
|
218
|
+
* `onclick`), which are events rather than props.
|
|
219
|
+
*/
|
|
220
|
+
export function isAllowedNonPropAttribute(name) {
|
|
221
|
+
if (/^(aria|data)-/i.test(name) || /^aria[A-Z]/.test(name))
|
|
222
|
+
return true;
|
|
223
|
+
if (/^on[A-Z:]/.test(name) || /^on[a-z]+$/.test(name))
|
|
224
|
+
return true;
|
|
225
|
+
if (VUE_RESERVED_ATTRIBUTES.has(name))
|
|
226
|
+
return true;
|
|
227
|
+
return (NATIVE_ATTRIBUTES.has(name.toLowerCase()) ||
|
|
228
|
+
NATIVE_ATTRIBUTES.has(hyphenate(name)));
|
|
229
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Rule } from 'eslint';
|
|
2
|
+
/**
|
|
3
|
+
* Reports `v-model` / `v-model:name` on a resolved GhostVue component when the
|
|
4
|
+
* manifest declares no model for that prop. Dynamic arguments
|
|
5
|
+
* (`v-model:[name]`) are skipped; modifiers and value types are not checked.
|
|
6
|
+
*/
|
|
7
|
+
export declare const noUndeclaredVModel: Rule.RuleModule;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { componentDocsUrl } from '../component-index.js';
|
|
2
|
+
import { camelize } from '../native-attributes.js';
|
|
3
|
+
import { createGhostTemplateVisitor, formatNames, staticArgument, } from '../template.js';
|
|
4
|
+
import { ruleDocsUrl } from './docs-url.js';
|
|
5
|
+
/**
|
|
6
|
+
* Reports `v-model` / `v-model:name` on a resolved GhostVue component when the
|
|
7
|
+
* manifest declares no model for that prop. Dynamic arguments
|
|
8
|
+
* (`v-model:[name]`) are skipped; modifiers and value types are not checked.
|
|
9
|
+
*/
|
|
10
|
+
export const noUndeclaredVModel = {
|
|
11
|
+
meta: {
|
|
12
|
+
type: 'problem',
|
|
13
|
+
docs: {
|
|
14
|
+
description: 'Disallow v-model bindings that a GhostVue component does not declare',
|
|
15
|
+
recommended: true,
|
|
16
|
+
url: ruleDocsUrl('no-undeclared-v-model'),
|
|
17
|
+
},
|
|
18
|
+
schema: [],
|
|
19
|
+
messages: {
|
|
20
|
+
undeclaredModel: "{{component}} from {{packageName}} does not declare {{directive}} (model prop '{{prop}}'). Declared models: {{models}}. See {{docs}}",
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
create(context) {
|
|
24
|
+
return createGhostTemplateVisitor(context, (element, component) => {
|
|
25
|
+
const models = new Set(component.contract.models);
|
|
26
|
+
for (const attribute of element.startTag.attributes) {
|
|
27
|
+
if (!attribute.directive || attribute.key.name.name !== 'model') {
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
const argument = staticArgument(attribute);
|
|
31
|
+
if (argument === undefined)
|
|
32
|
+
continue;
|
|
33
|
+
// Vue camelizes the v-model argument when matching it to a prop.
|
|
34
|
+
const prop = argument === null ? 'modelValue' : camelize(argument);
|
|
35
|
+
if (models.has(prop))
|
|
36
|
+
continue;
|
|
37
|
+
context.report({
|
|
38
|
+
loc: attribute.key.loc,
|
|
39
|
+
messageId: 'undeclaredModel',
|
|
40
|
+
data: {
|
|
41
|
+
component: component.name,
|
|
42
|
+
packageName: component.packageName,
|
|
43
|
+
directive: argument === null ? 'v-model' : `v-model:${argument}`,
|
|
44
|
+
prop,
|
|
45
|
+
models: formatNames(component.contract.models),
|
|
46
|
+
docs: componentDocsUrl(component.contract),
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
},
|
|
52
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Rule } from 'eslint';
|
|
2
|
+
/**
|
|
3
|
+
* Reports a static attribute or `v-bind:name` on a resolved GhostVue component
|
|
4
|
+
* when the name is neither a manifest prop nor an attribute that falls through
|
|
5
|
+
* natively. Object spreads (`v-bind="attrs"`), dynamic arguments
|
|
6
|
+
* (`:[name]`), and `.prop`/`.attr` bindings are skipped because they carry no
|
|
7
|
+
* static prop name, and so is an element with a bound polymorphic `:as`.
|
|
8
|
+
* Value types are left to vue-tsc.
|
|
9
|
+
*/
|
|
10
|
+
export declare const noUnknownProps: Rule.RuleModule;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { componentDocsUrl } from '../component-index.js';
|
|
2
|
+
import { camelize, isAllowedNonPropAttribute } from '../native-attributes.js';
|
|
3
|
+
import { createGhostTemplateVisitor, formatNames } from '../template.js';
|
|
4
|
+
import { ruleDocsUrl } from './docs-url.js';
|
|
5
|
+
function hasBoundArgument(element, name) {
|
|
6
|
+
return element.startTag.attributes.some((attribute) => attribute.directive &&
|
|
7
|
+
attribute.key.name.name === 'bind' &&
|
|
8
|
+
attribute.key.argument?.type === 'VIdentifier' &&
|
|
9
|
+
attribute.key.argument.rawName === name);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Reports a static attribute or `v-bind:name` on a resolved GhostVue component
|
|
13
|
+
* when the name is neither a manifest prop nor an attribute that falls through
|
|
14
|
+
* natively. Object spreads (`v-bind="attrs"`), dynamic arguments
|
|
15
|
+
* (`:[name]`), and `.prop`/`.attr` bindings are skipped because they carry no
|
|
16
|
+
* static prop name, and so is an element with a bound polymorphic `:as`.
|
|
17
|
+
* Value types are left to vue-tsc.
|
|
18
|
+
*/
|
|
19
|
+
export const noUnknownProps = {
|
|
20
|
+
meta: {
|
|
21
|
+
type: 'problem',
|
|
22
|
+
docs: {
|
|
23
|
+
description: 'Disallow attributes on GhostVue components that are neither declared props nor native, ARIA, or data attributes',
|
|
24
|
+
recommended: true,
|
|
25
|
+
url: ruleDocsUrl('no-unknown-props'),
|
|
26
|
+
},
|
|
27
|
+
schema: [],
|
|
28
|
+
messages: {
|
|
29
|
+
unknownProp: "'{{name}}' is not a prop of {{component}} from {{packageName}} and is not a native, ARIA, or data attribute. Declared props: {{props}}. See {{docs}}",
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
create(context) {
|
|
33
|
+
return createGhostTemplateVisitor(context, (element, component) => {
|
|
34
|
+
const props = new Set(component.contract.props);
|
|
35
|
+
// A bound polymorphic `:as` may render another component (for example
|
|
36
|
+
// RouterLink) that receives the fallthrough attributes as its own props,
|
|
37
|
+
// so no attribute on this element is evidence of a mistake.
|
|
38
|
+
if (props.has('as') && hasBoundArgument(element, 'as'))
|
|
39
|
+
return;
|
|
40
|
+
for (const attribute of element.startTag.attributes) {
|
|
41
|
+
let name;
|
|
42
|
+
if (attribute.directive) {
|
|
43
|
+
const key = attribute.key;
|
|
44
|
+
if (key.name.name !== 'bind')
|
|
45
|
+
continue;
|
|
46
|
+
if (key.argument?.type !== 'VIdentifier')
|
|
47
|
+
continue;
|
|
48
|
+
if (key.modifiers.some((modifier) => modifier.name === 'prop' || modifier.name === 'attr')) {
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
name = key.argument.rawName;
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
name = attribute.key.rawName;
|
|
55
|
+
}
|
|
56
|
+
// Vue normalizes `max-value` and `maxValue` to the same prop key.
|
|
57
|
+
if (props.has(name) || props.has(camelize(name)))
|
|
58
|
+
continue;
|
|
59
|
+
if (isAllowedNonPropAttribute(name))
|
|
60
|
+
continue;
|
|
61
|
+
context.report({
|
|
62
|
+
loc: attribute.key.loc,
|
|
63
|
+
messageId: 'unknownProp',
|
|
64
|
+
data: {
|
|
65
|
+
name,
|
|
66
|
+
component: component.name,
|
|
67
|
+
packageName: component.packageName,
|
|
68
|
+
props: formatNames(component.contract.props),
|
|
69
|
+
docs: componentDocsUrl(component.contract),
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
},
|
|
75
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Rule } from 'eslint';
|
|
2
|
+
/**
|
|
3
|
+
* Reports slot content passed to a resolved GhostVue component under a name
|
|
4
|
+
* its manifest does not declare: `#name` / `v-slot:name` on a child
|
|
5
|
+
* `<template>` or on the component itself, and implicit default content when
|
|
6
|
+
* the component has no default slot. Dynamic names (`#[name]`) are skipped,
|
|
7
|
+
* and slot props are left to vue-tsc.
|
|
8
|
+
*/
|
|
9
|
+
export declare const noUnknownSlots: Rule.RuleModule;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { componentDocsUrl } from '../component-index.js';
|
|
2
|
+
import { createGhostTemplateVisitor, formatNames, staticArgument, } from '../template.js';
|
|
3
|
+
import { ruleDocsUrl } from './docs-url.js';
|
|
4
|
+
function slotDirective(element) {
|
|
5
|
+
for (const attribute of element.startTag.attributes) {
|
|
6
|
+
if (attribute.directive && attribute.key.name.name === 'slot') {
|
|
7
|
+
return attribute;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
return undefined;
|
|
11
|
+
}
|
|
12
|
+
/** Content that Vue passes to the implicit default slot. */
|
|
13
|
+
function isImplicitDefaultContent(child) {
|
|
14
|
+
if (child.type === 'VText')
|
|
15
|
+
return child.value.trim() !== '';
|
|
16
|
+
if (child.type === 'VExpressionContainer')
|
|
17
|
+
return true;
|
|
18
|
+
return !(child.rawName === 'template' && slotDirective(child));
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Reports slot content passed to a resolved GhostVue component under a name
|
|
22
|
+
* its manifest does not declare: `#name` / `v-slot:name` on a child
|
|
23
|
+
* `<template>` or on the component itself, and implicit default content when
|
|
24
|
+
* the component has no default slot. Dynamic names (`#[name]`) are skipped,
|
|
25
|
+
* and slot props are left to vue-tsc.
|
|
26
|
+
*/
|
|
27
|
+
export const noUnknownSlots = {
|
|
28
|
+
meta: {
|
|
29
|
+
type: 'problem',
|
|
30
|
+
docs: {
|
|
31
|
+
description: 'Disallow slot content for slots a GhostVue component does not declare',
|
|
32
|
+
recommended: true,
|
|
33
|
+
url: ruleDocsUrl('no-unknown-slots'),
|
|
34
|
+
},
|
|
35
|
+
schema: [],
|
|
36
|
+
messages: {
|
|
37
|
+
unknownSlot: "{{component}} from {{packageName}} has no '{{slot}}' slot. Declared slots: {{slots}}. See {{docs}}",
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
create(context) {
|
|
41
|
+
return createGhostTemplateVisitor(context, (element, component) => {
|
|
42
|
+
const declared = component.contract.slots;
|
|
43
|
+
// Data-driven slot names (one per column id) are left to vue-tsc.
|
|
44
|
+
if (declared === null)
|
|
45
|
+
return;
|
|
46
|
+
const slots = new Set(declared);
|
|
47
|
+
const report = (loc, slot) => {
|
|
48
|
+
context.report({
|
|
49
|
+
loc,
|
|
50
|
+
messageId: 'unknownSlot',
|
|
51
|
+
data: {
|
|
52
|
+
component: component.name,
|
|
53
|
+
packageName: component.packageName,
|
|
54
|
+
slot,
|
|
55
|
+
slots: formatNames(declared),
|
|
56
|
+
docs: componentDocsUrl(component.contract),
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
};
|
|
60
|
+
const check = (directive) => {
|
|
61
|
+
const argument = staticArgument(directive);
|
|
62
|
+
if (argument === undefined)
|
|
63
|
+
return;
|
|
64
|
+
const slot = argument ?? 'default';
|
|
65
|
+
if (!slots.has(slot))
|
|
66
|
+
report(directive.key.loc, slot);
|
|
67
|
+
};
|
|
68
|
+
const ownDirective = slotDirective(element);
|
|
69
|
+
if (ownDirective) {
|
|
70
|
+
// `<Comp #name>`: the children are that slot's content.
|
|
71
|
+
check(ownDirective);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
let reportedDefault = false;
|
|
75
|
+
for (const child of element.children) {
|
|
76
|
+
if (child.type === 'VElement' && child.rawName === 'template') {
|
|
77
|
+
const directive = slotDirective(child);
|
|
78
|
+
if (directive) {
|
|
79
|
+
check(directive);
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (!reportedDefault &&
|
|
84
|
+
!slots.has('default') &&
|
|
85
|
+
isImplicitDefaultContent(child)) {
|
|
86
|
+
reportedDefault = true;
|
|
87
|
+
report(child.loc, 'default');
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
},
|
|
92
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Rule } from 'eslint';
|
|
2
|
+
import type { AST } from 'vue-eslint-parser';
|
|
3
|
+
import { type GhostComponentContract, type GhostComponentPackage } from './component-index.js';
|
|
4
|
+
/** A template element whose tag statically resolves to a GhostVue export. */
|
|
5
|
+
export interface ResolvedGhostComponent {
|
|
6
|
+
/** Exported component name, independent of the local alias. */
|
|
7
|
+
readonly name: string;
|
|
8
|
+
readonly packageName: GhostComponentPackage;
|
|
9
|
+
readonly contract: GhostComponentContract;
|
|
10
|
+
}
|
|
11
|
+
type ElementHandler = (element: AST.VElement, component: ResolvedGhostComponent) => void;
|
|
12
|
+
/**
|
|
13
|
+
* Shared rule scaffold: visits every template element that resolves to a
|
|
14
|
+
* GhostVue component with a manifest and hands it to `handler`. Everything
|
|
15
|
+
* else (native elements, unresolved tags, exports without a manifest,
|
|
16
|
+
* `<component :is>`) is skipped, because a diagnostic requires positive
|
|
17
|
+
* evidence.
|
|
18
|
+
*/
|
|
19
|
+
export declare function createGhostTemplateVisitor(context: Rule.RuleContext, handler: ElementHandler): Rule.RuleListener;
|
|
20
|
+
/**
|
|
21
|
+
* Directive argument: `null` when absent (`v-model`, `v-slot`), the raw name
|
|
22
|
+
* when static (`v-model:open`), and `undefined` when dynamic (`#[name]`), which
|
|
23
|
+
* callers must skip.
|
|
24
|
+
*/
|
|
25
|
+
export declare function staticArgument(directive: AST.VDirective): string | null | undefined;
|
|
26
|
+
/** Comma-separated list for messages, or a stand-in when empty. */
|
|
27
|
+
export declare function formatNames(names: readonly string[]): string;
|
|
28
|
+
export {};
|