@neon-pixels/eslint-config 0.2.0 → 0.4.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/README.md CHANGED
@@ -32,12 +32,12 @@ pnpm add -D @neon-pixels/eslint-config eslint @eslint/js typescript-eslint eslin
32
32
 
33
33
  Each entry point default-exports a flat config array. Spread it into your own config, then add your rules and ignores after it.
34
34
 
35
- | Entry point | Adds to the base |
36
- | -------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------- |
37
- | `@neon-pixels/eslint-config` | Nothing: the base with no stack assumptions and no globals |
38
- | `@neon-pixels/eslint-config/node` | Node globals |
39
- | `@neon-pixels/eslint-config/vitepress` | Browser and Node globals, `eslint-plugin-vue` recommended, `eslint-plugin-vuejs-accessibility` recommended, `vue/no-v-html` as an error |
40
- | `@neon-pixels/eslint-config/nuxt` | Everything in `vitepress`, single-word names allowed for pages and layouts, plus the Clerk composable restriction |
35
+ | Entry point | Adds to the base |
36
+ | -------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
37
+ | `@neon-pixels/eslint-config` | Nothing: the base with no stack assumptions and no globals |
38
+ | `@neon-pixels/eslint-config/node` | Node globals |
39
+ | `@neon-pixels/eslint-config/vitepress` | Browser and Node globals, `eslint-plugin-vue` recommended, `eslint-plugin-vuejs-accessibility` recommended with `label-has-for` accepting either nesting or `for`/`id`, `vue/no-v-html` as an error |
40
+ | `@neon-pixels/eslint-config/nuxt` | Everything in `vitepress`, single-word names allowed for pages and layouts, plus the Clerk composable restriction |
41
41
 
42
42
  ### Node
43
43
 
@@ -100,11 +100,44 @@ The wrapper itself has to call them. Pass its path pattern (or an array of patte
100
100
 
101
101
  Name your wrappers something other than `useAuth` and `useUser`, or the call check flags the wrappers' own callers.
102
102
 
103
- #### Overriding
103
+ #### Adding your own restrictions
104
104
 
105
- `allowClerkComposables()` switches off `no-restricted-imports`, `no-restricted-syntax`, and `vue/no-restricted-syntax` entirely for the files it matches, so put any restrictions of your own for those files in a block after it.
105
+ Flat config replaces a rule's options instead of merging them, so a block of yours that sets `no-restricted-imports`, `no-restricted-syntax`, or `vue/no-restricted-syntax` drops the Clerk restriction for the files it matches. Use `extendRestrictions()` instead. It returns a block whose three rules hold the preset's Clerk entries followed by yours.
106
106
 
107
- A later `no-restricted-imports` or `no-restricted-syntax` block in your config replaces the Clerk rule for the files it matches (flat config does not merge rule options). Re-include the restriction in your own block if you need extra restrictions on the same files.
107
+ ```js
108
+ // eslint.config.mjs
109
+ import nuxt, {
110
+ allowClerkComposables,
111
+ extendRestrictions,
112
+ } from "@neon-pixels/eslint-config/nuxt";
113
+ import withNuxt from "./.nuxt/eslint.config.mjs";
114
+
115
+ export default withNuxt(
116
+ ...nuxt,
117
+ extendRestrictions({
118
+ files: ["app/**/*.vue"], // optional, defaults to every file
119
+ imports: [{ name: "lodash", message: "Import from lodash-es." }],
120
+ syntax: [
121
+ { selector: "ThrowStatement", message: "Return a result instead." },
122
+ ],
123
+ vueSyntax: [
124
+ {
125
+ selector: "VElement[name='marquee']",
126
+ message: "Do not use <marquee>.",
127
+ },
128
+ ],
129
+ }),
130
+ allowClerkComposables("app/composables/useClerkAuth.ts"),
131
+ );
132
+ ```
133
+
134
+ `imports` entries use the `no-restricted-imports` path shape (`name`, `importNames`, `message`, `allowTypeImports`). `syntax` and `vueSyntax` entries are `{ selector, message }`. A rule you pass nothing for still carries the Clerk entries. The helper throws a `TypeError` on an empty call, an unknown key, or an entry without a `name` or `selector`.
135
+
136
+ Two `extendRestrictions()` blocks that match the same files replace each other, so put all your entries for one set of files in one call.
137
+
138
+ Order matters: the preset, then your `extendRestrictions()` blocks, then `allowClerkComposables()`. A restriction block placed after `allowClerkComposables()` switches the restriction back on for the wrapper files.
139
+
140
+ `allowClerkComposables()` switches off all three rules entirely for the files it matches, so put any restrictions you want on those files in a block after it.
108
141
 
109
142
  ## License
110
143
 
package/lib/clerk.js CHANGED
@@ -7,6 +7,12 @@ const MESSAGE =
7
7
  const restrictedSyntax = [
8
8
  { selector: COMPOSABLE_CALL_SELECTOR, message: MESSAGE },
9
9
  ];
10
+ const restrictedImports = CLERK_MODULES.map((name) => ({
11
+ name,
12
+ importNames: BANNED_COMPOSABLES,
13
+ allowTypeImports: true,
14
+ message: MESSAGE,
15
+ }));
10
16
 
11
17
  /**
12
18
  * Forbids Clerk's client composables. Nuxt auto-imports them, so an import ban
@@ -18,15 +24,7 @@ const restrictedSyntax = [
18
24
  export const clerkWrapperRestriction = {
19
25
  name: "neon-pixels/clerk-wrapper-restriction",
20
26
  rules: {
21
- "no-restricted-imports": [
22
- "error",
23
- ...CLERK_MODULES.map((name) => ({
24
- name,
25
- importNames: BANNED_COMPOSABLES,
26
- allowTypeImports: true,
27
- message: MESSAGE,
28
- })),
29
- ],
27
+ "no-restricted-imports": ["error", ...restrictedImports],
30
28
  "no-restricted-syntax": ["error", ...restrictedSyntax],
31
29
  "vue/no-restricted-syntax": ["error", ...restrictedSyntax],
32
30
  },
@@ -60,3 +58,105 @@ export function allowClerkComposables(files) {
60
58
  },
61
59
  };
62
60
  }
61
+
62
+ const EXTEND_KEYS = ["files", "imports", "syntax", "vueSyntax"];
63
+
64
+ function assertEntries(
65
+ /** @type {string} */ key,
66
+ /** @type {unknown} */ entries,
67
+ /** @type {string} */ requiredField,
68
+ ) {
69
+ if (entries === undefined) {
70
+ return;
71
+ }
72
+ const valid =
73
+ Array.isArray(entries) &&
74
+ entries.every(
75
+ (entry) =>
76
+ entry !== null &&
77
+ typeof entry === "object" &&
78
+ typeof entry[requiredField] === "string" &&
79
+ entry[requiredField].trim() !== "",
80
+ );
81
+ if (!valid) {
82
+ throw new TypeError(
83
+ `extendRestrictions() "${key}" must be an array of objects with a non-empty "${requiredField}".`,
84
+ );
85
+ }
86
+ }
87
+
88
+ function assertKnownKeys(/** @type {object} */ options) {
89
+ const unknownKeys = Object.keys(options).filter(
90
+ (key) => !EXTEND_KEYS.includes(key),
91
+ );
92
+ if (unknownKeys.length) {
93
+ throw new TypeError(
94
+ `extendRestrictions() got unknown keys: ${unknownKeys.join(", ")}.`,
95
+ );
96
+ }
97
+ }
98
+
99
+ function assertFiles(/** @type {unknown} */ files) {
100
+ if (files === undefined) {
101
+ return;
102
+ }
103
+ const patterns = [files].flat();
104
+ if (patterns.length && !patterns.some(isBlankPattern)) {
105
+ return;
106
+ }
107
+ throw new TypeError(
108
+ "extendRestrictions() files must be non-empty glob patterns.",
109
+ );
110
+ }
111
+
112
+ function assertHasRestriction(/** @type {Record<string, unknown>} */ options) {
113
+ const hasRestriction = ["imports", "syntax", "vueSyntax"].some(
114
+ (key) => Array.isArray(options[key]) && options[key].length,
115
+ );
116
+ if (!hasRestriction) {
117
+ throw new TypeError(
118
+ "extendRestrictions() needs at least one entry in imports, syntax, or vueSyntax.",
119
+ );
120
+ }
121
+ }
122
+
123
+ function assertExtendOptions(/** @type {unknown} */ options) {
124
+ if (options === null || typeof options !== "object") {
125
+ throw new TypeError("extendRestrictions() needs an options object.");
126
+ }
127
+ const checked = /** @type {Record<string, unknown>} */ (options);
128
+ assertKnownKeys(checked);
129
+ assertFiles(checked.files);
130
+ assertEntries("imports", checked.imports, "name");
131
+ assertEntries("syntax", checked.syntax, "selector");
132
+ assertEntries("vueSyntax", checked.vueSyntax, "selector");
133
+ assertHasRestriction(checked);
134
+ }
135
+
136
+ /**
137
+ * Adds restrictions of your own on top of the Clerk restriction. Flat config
138
+ * replaces a rule's options instead of merging them, so this block repeats the
139
+ * preset's entries and appends yours. Spread it after the entry point's config
140
+ * and before `allowClerkComposables()`.
141
+ *
142
+ * @param {{
143
+ * files?: string | string[],
144
+ * imports?: import("../nuxt.js").RestrictedImport[],
145
+ * syntax?: import("../nuxt.js").RestrictedSyntax[],
146
+ * vueSyntax?: import("../nuxt.js").RestrictedSyntax[],
147
+ * }} options
148
+ * @returns {import("eslint").Linter.Config}
149
+ */
150
+ export function extendRestrictions(options) {
151
+ assertExtendOptions(options);
152
+ const { files, imports = [], syntax = [], vueSyntax = [] } = options;
153
+ return {
154
+ name: "neon-pixels/extend-restrictions",
155
+ ...(files === undefined ? {} : { files: [files].flat() }),
156
+ rules: {
157
+ "no-restricted-imports": ["error", ...restrictedImports, ...imports],
158
+ "no-restricted-syntax": ["error", ...restrictedSyntax, ...syntax],
159
+ "vue/no-restricted-syntax": ["error", ...restrictedSyntax, ...vueSyntax],
160
+ },
161
+ };
162
+ }
package/lib/vue.js CHANGED
@@ -21,6 +21,11 @@ export const vueConfigs = [
21
21
  rules: {
22
22
  // The recommended preset only warns, and a warning does not fail CI.
23
23
  "vue/no-v-html": "error",
24
+ // The recommended preset demands both; either one associates the label. The rule only checks that `for` is present, not that an id matches.
25
+ "vuejs-accessibility/label-has-for": [
26
+ "error",
27
+ { required: { some: ["nesting", "id"] } },
28
+ ],
24
29
  },
25
30
  },
26
31
  ];
package/nuxt.d.ts CHANGED
@@ -1,5 +1,27 @@
1
1
  import type { Linter } from "eslint";
2
2
 
3
+ /** An entry for `no-restricted-imports`, in its path shape. */
4
+ export interface RestrictedImport {
5
+ name: string;
6
+ importNames?: string[];
7
+ message?: string;
8
+ allowTypeImports?: boolean;
9
+ }
10
+
11
+ /** An entry for `no-restricted-syntax` or `vue/no-restricted-syntax`. */
12
+ export interface RestrictedSyntax {
13
+ selector: string;
14
+ message?: string;
15
+ }
16
+
17
+ export interface ExtendRestrictionsOptions {
18
+ /** Glob patterns the block applies to. Defaults to every file. */
19
+ files?: string | string[];
20
+ imports?: RestrictedImport[];
21
+ syntax?: RestrictedSyntax[];
22
+ vueSyntax?: RestrictedSyntax[];
23
+ }
24
+
3
25
  declare const config: Linter.Config[];
4
26
 
5
27
  /**
@@ -8,4 +30,15 @@ declare const config: Linter.Config[];
8
30
  */
9
31
  export function allowClerkComposables(files: string | string[]): Linter.Config;
10
32
 
33
+ /**
34
+ * A config block whose `no-restricted-imports`, `no-restricted-syntax`, and
35
+ * `vue/no-restricted-syntax` rules hold the preset's Clerk entries followed by
36
+ * yours. Spread it after the default export and before `allowClerkComposables()`.
37
+ * Throws a `TypeError` on an empty call, an unknown key, or an entry without a
38
+ * `selector` or `name`.
39
+ */
40
+ export function extendRestrictions(
41
+ options: ExtendRestrictionsOptions,
42
+ ): Linter.Config;
43
+
11
44
  export default config;
package/nuxt.js CHANGED
@@ -3,7 +3,7 @@ import { withBase } from "./lib/base.js";
3
3
  import { clerkWrapperRestriction } from "./lib/clerk.js";
4
4
  import { vueConfigs } from "./lib/vue.js";
5
5
 
6
- export { allowClerkComposables } from "./lib/clerk.js";
6
+ export { allowClerkComposables, extendRestrictions } from "./lib/clerk.js";
7
7
 
8
8
  export default withBase(
9
9
  ...vueConfigs,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@neon-pixels/eslint-config",
3
- "version": "0.2.0",
3
+ "version": "0.4.0",
4
4
  "description": "Shared ESLint flat configuration for Neon Pixels projects (Nuxt, VitePress, Node).",
5
5
  "license": "MIT",
6
6
  "type": "module",