@murky-web/oxlint-plugin-solid 1.3.0 → 1.4.1

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.
@@ -2,38 +2,115 @@ import { existsSync, readFileSync, statSync } from "node:fs";
2
2
  import { dirname, join, resolve, sep } from "node:path";
3
3
 
4
4
  const SERVER_DIRECTORY = join("src", "server");
5
+ const GIT_ENTRY = ".git";
6
+ const PACKAGE_MANIFEST = "package.json";
5
7
  const EXTENSIONS = [".ts", ".tsx", ".mts", ".js", ".jsx", ".mjs"];
6
8
  const CLIENT_FILE = /\.(?:[cm]?[jt]s|[jt]sx)$/;
7
9
  const TEST_FILE = /\.(?:test|spec)\.[^/]+$/;
8
10
  const ALIAS_PREFIX = "~/";
9
- const USE_SERVER = /^(?:["']use server["'])/;
10
- // Whitespace and comments may precede the directive; anything else means it is
11
- // not the first statement.
12
- const LEADING_TRIVIA = /^(?:\s+|\/\/[^\n]*|\/\*[\s\S]*?\*\/)+/;
11
+ const USE_SERVER_DIRECTIVE = "use server";
12
+ // The byte order mark, the line terminators (LF, CR, U+2028, U+2029) and the
13
+ // white space of ECMAScript (tab, VT, FF, BOM and every Unicode Zs, which
14
+ // includes the plain space). The code points that cannot be seen are written
15
+ // as escapes, so no invisible character sits in the source.
16
+ const BYTE_ORDER_MARK = /^\u{FEFF}/u;
17
+ const LINE_BREAK = /[\n\r\u{2028}\u{2029}]/u;
18
+ const WHITESPACE = /[\t\v\f\u{FEFF}\p{Zs}]/u;
19
+ // A whole identifier, so that `in_progress()` is not the operator `in`.
20
+ const WORD = /^[\p{ID_Continue}$\u{200C}\u{200D}]+/u;
21
+ // One more than the longest operator word, so a longer identifier that starts
22
+ // with one (`instanceofx`) is not cut down to it.
23
+ const LONGEST_OPERATOR_WORD = 10;
24
+ const WORD_SLICE_LENGTH = LONGEST_OPERATOR_WORD + 1;
25
+ // Words that go on with an expression after a string: `"use server" in x`.
26
+ const OPERATOR_WORDS = new Set(["as", "in", "instanceof", "satisfies"]);
27
+ // The first character of what follows a string on the next line, when it goes
28
+ // on with the string as an expression (`.length`, `(...)`, `* 2`, a tagged
29
+ // template, ...) instead of starting a new statement.
30
+ const CONTINUING_CHARACTERS = new Set([
31
+ ".",
32
+ "(",
33
+ "[",
34
+ "*",
35
+ "%",
36
+ "<",
37
+ ">",
38
+ "=",
39
+ "?",
40
+ ",",
41
+ "&",
42
+ "|",
43
+ "^",
44
+ "/",
45
+ "`",
46
+ ]);
13
47
 
14
- /** Nearest directory at or above `directory` that holds a package.json. */
15
- function findProjectRoot(directory) {
16
- let current = directory;
48
+ function isDirectory(path) {
49
+ return existsSync(path) && statSync(path).isDirectory();
50
+ }
17
51
 
18
- while (!existsSync(join(current, "package.json"))) {
19
- const parent = dirname(current);
20
- if (parent === current) {
21
- return null;
22
- }
52
+ function isFile(path) {
53
+ return existsSync(path) && statSync(path).isFile();
54
+ }
55
+
56
+ /**
57
+ * Whether `directory` is where a repository or a workspace ends: it holds a
58
+ * `.git` or a package.json with `workspaces`. A `src/server` further up belongs
59
+ * to something else (a home directory, an outer checkout).
60
+ */
61
+ function isProjectBoundary(directory) {
62
+ if (existsSync(join(directory, GIT_ENTRY))) {
63
+ return true;
64
+ }
23
65
 
24
- current = parent;
66
+ const manifest = join(directory, PACKAGE_MANIFEST);
67
+ if (!isFile(manifest)) {
68
+ return false;
25
69
  }
26
70
 
27
- return current;
71
+ try {
72
+ const { workspaces } = JSON.parse(readFileSync(manifest, "utf8"));
73
+ return typeof workspaces === "object" && workspaces !== null;
74
+ } catch {
75
+ return false;
76
+ }
28
77
  }
29
78
 
30
- function isFile(path) {
31
- return existsSync(path) && statSync(path).isFile();
79
+ // Directory -> the project root found from it (or null). One oxlint run lints
80
+ // many files of the same folders, and every lookup stats each folder above.
81
+ const projectRootCache = new Map();
82
+
83
+ /**
84
+ * Nearest directory at or above `directory` that holds `src/server`: the
85
+ * project the client file belongs to. The nearest package.json is not it: one
86
+ * can sit in any folder below the project, and it does not say where the server
87
+ * code is. The walk stops at the first folder with a `.git` or a package.json
88
+ * with `workspaces`. The answer is cached for the life of the process: a
89
+ * `src/server` made while a language server runs is found after a restart.
90
+ */
91
+ function findProjectRoot(directory) {
92
+ if (projectRootCache.has(directory)) {
93
+ return projectRootCache.get(directory);
94
+ }
95
+
96
+ let root = null;
97
+ if (isDirectory(join(directory, SERVER_DIRECTORY))) {
98
+ root = directory;
99
+ } else if (!isProjectBoundary(directory)) {
100
+ const parent = dirname(directory);
101
+ if (parent !== directory) {
102
+ root = findProjectRoot(parent);
103
+ }
104
+ }
105
+
106
+ projectRootCache.set(directory, root);
107
+
108
+ return root;
32
109
  }
33
110
 
34
111
  /**
35
- * The file an import path points at, trying the extensions the bundler would.
36
- * A `.js` specifier names the `.ts` source next to it.
112
+ * The file an import path points at, trying the extensions the bundler would. A
113
+ * `.js` specifier names the `.ts` source next to it.
37
114
  */
38
115
  function findModuleFile(basePath) {
39
116
  const withoutJsExtension = basePath.replace(/\.[cm]?jsx?$/, "");
@@ -47,19 +124,131 @@ function findModuleFile(basePath) {
47
124
  return candidates.find(isFile) ?? null;
48
125
  }
49
126
 
50
- function startsWithUseServer(filePath) {
51
- let text = readFileSync(filePath, "utf8").replace(/^/, "");
127
+ /**
128
+ * The index after the whitespace and comments that start at `start`, and
129
+ * whether a line break was among them.
130
+ */
131
+ function skipTrivia(text, start) {
132
+ let index = start;
133
+ let sawLineBreak = false;
52
134
 
53
- for (;;) {
54
- const trivia = LEADING_TRIVIA.exec(text);
55
- if (!trivia) {
135
+ while (index < text.length) {
136
+ const character = text[index];
137
+ if (LINE_BREAK.test(character)) {
138
+ sawLineBreak = true;
139
+ index += 1;
140
+ } else if (WHITESPACE.test(character)) {
141
+ index += 1;
142
+ } else if (text.startsWith("//", index)) {
143
+ while (index < text.length && !LINE_BREAK.test(text[index])) {
144
+ index += 1;
145
+ }
146
+ } else if (text.startsWith("/*", index)) {
147
+ const end = text.indexOf("*/", index + 2);
148
+ const stop = end === -1 ? text.length : end + 2;
149
+ sawLineBreak ||= LINE_BREAK.test(text.slice(index, stop));
150
+ index = stop;
151
+ } else {
56
152
  break;
57
153
  }
154
+ }
155
+
156
+ return { index, sawLineBreak };
157
+ }
158
+
159
+ /**
160
+ * Whether the code at `index`, on a new line, goes on with the expression
161
+ * before it, so that no semicolon is inserted there.
162
+ */
163
+ function continuesExpression(text, index) {
164
+ const character = text[index];
165
+ if (character === "+" || character === "-") {
166
+ // `++x` and `--x` start a statement of their own.
167
+ return text[index + 1] !== character;
168
+ }
169
+
170
+ if (character === "!") {
171
+ return text[index + 1] === "=";
172
+ }
173
+
174
+ if (CONTINUING_CHARACTERS.has(character)) {
175
+ return true;
176
+ }
177
+
178
+ // The whole word: `in_progress()` starts a statement, `in x` does not.
179
+ const word = WORD.exec(text.slice(index, index + WORD_SLICE_LENGTH));
180
+
181
+ return word !== null && OPERATOR_WORDS.has(word[0]);
182
+ }
183
+
184
+ /**
185
+ * The index of the string literal's closing quote, or -1 when the line ends
186
+ * first: a string literal cannot span lines.
187
+ */
188
+ function findStringEnd(text, start) {
189
+ const quote = text[start];
190
+ let end = start + 1;
191
+
192
+ while (end < text.length && text[end] !== quote) {
193
+ if (LINE_BREAK.test(text[end])) {
194
+ return -1;
195
+ }
58
196
 
59
- text = text.slice(trivia[0].length);
197
+ end += text[end] === "\\" ? 2 : 1;
60
198
  }
61
199
 
62
- return USE_SERVER.test(text);
200
+ return end < text.length ? end : -1;
201
+ }
202
+
203
+ /**
204
+ * Whether `"use server"` is one of the directives that open the module: the
205
+ * directive prologue, which is every string-literal statement at the top (`"use
206
+ * strict"; "use server";`) and ends with the first statement that is anything
207
+ * else. `"use server".length;`, `("use server");` and an escaped
208
+ * `"use\x20server"` are an expression and another directive, not this one.
209
+ */
210
+ function hasUseServerDirective(source) {
211
+ const text = source.replace(BYTE_ORDER_MARK, "");
212
+ let index = 0;
213
+ if (text.startsWith("#!")) {
214
+ const lineEnd = text.search(LINE_BREAK);
215
+ index = lineEnd === -1 ? text.length : lineEnd;
216
+ }
217
+
218
+ while (index < text.length) {
219
+ index = skipTrivia(text, index).index;
220
+ const quote = text[index];
221
+ if (quote !== '"' && quote !== "'") {
222
+ return false;
223
+ }
224
+
225
+ const end = findStringEnd(text, index);
226
+ if (end === -1) {
227
+ return false;
228
+ }
229
+
230
+ const after = skipTrivia(text, end + 1);
231
+ const next = text[after.index];
232
+ const endsStatement =
233
+ next === ";" ||
234
+ after.index >= text.length ||
235
+ (after.sawLineBreak && !continuesExpression(text, after.index));
236
+ if (!endsStatement) {
237
+ return false;
238
+ }
239
+
240
+ if (text.slice(index + 1, end) === USE_SERVER_DIRECTIVE) {
241
+ return true;
242
+ }
243
+
244
+ index = next === ";" ? after.index + 1 : after.index;
245
+ }
246
+
247
+ return false;
248
+ }
249
+
250
+ function startsWithUseServer(filePath) {
251
+ return hasUseServerDirective(readFileSync(filePath, "utf8"));
63
252
  }
64
253
 
65
254
  function isInside(path, directory) {
@@ -82,6 +271,27 @@ function resolveImport(specifier, filename, projectRoot) {
82
271
  return null;
83
272
  }
84
273
 
274
+ /**
275
+ * The module specifier a node spells out: a string literal, or a template
276
+ * literal without substitutions (`` `~/server/db` ``). Null for anything that
277
+ * is only known at run time.
278
+ */
279
+ function getStaticSpecifier(node) {
280
+ if (node?.type === "Literal" && typeof node.value === "string") {
281
+ return node.value;
282
+ }
283
+
284
+ if (
285
+ node?.type === "TemplateLiteral" &&
286
+ node.expressions.length === 0 &&
287
+ node.quasis.length === 1
288
+ ) {
289
+ return node.quasis[0].value.cooked ?? null;
290
+ }
291
+
292
+ return null;
293
+ }
294
+
85
295
  export const noServerImportInClientRule = {
86
296
  meta: {
87
297
  type: "problem",
@@ -93,7 +303,7 @@ export const noServerImportInClientRule = {
93
303
  schema: [],
94
304
  messages: {
95
305
  serverImportInClient:
96
- "'{{specifier}}' lives under src/server/ but does not start with the \"use server\" directive, so importing it into client code ships server-only code to the browser. Import a server function module instead (first statement `\"use server\";`, async exports only), and keep database clients and secrets inside it.",
306
+ '\'{{specifier}}\' lives under src/server/ but does not have the "use server" directive, so importing it into client code ships server-only code to the browser. Import a server function module instead (a `"use server";` directive at the top, async exports only), and keep database clients and secrets inside it.',
97
307
  },
98
308
  },
99
309
  defaultOptions: [],
@@ -114,18 +324,12 @@ export const noServerImportInClientRule = {
114
324
  const serverDirectory = join(projectRoot, SERVER_DIRECTORY);
115
325
 
116
326
  function check(sourceNode) {
117
- if (
118
- sourceNode?.type !== "Literal" ||
119
- typeof sourceNode.value !== "string"
120
- ) {
327
+ const specifier = getStaticSpecifier(sourceNode);
328
+ if (specifier === null) {
121
329
  return;
122
330
  }
123
331
 
124
- const basePath = resolveImport(
125
- sourceNode.value,
126
- filename,
127
- projectRoot,
128
- );
332
+ const basePath = resolveImport(specifier, filename, projectRoot);
129
333
  if (basePath === null) {
130
334
  return;
131
335
  }
@@ -143,7 +347,7 @@ export const noServerImportInClientRule = {
143
347
  context.report({
144
348
  node: sourceNode,
145
349
  messageId: "serverImportInClient",
146
- data: { specifier: sourceNode.value },
350
+ data: { specifier },
147
351
  });
148
352
  }
149
353
 
@@ -167,6 +371,15 @@ export const noServerImportInClientRule = {
167
371
  ImportExpression(node) {
168
372
  check(node.source);
169
373
  },
374
+ CallExpression(node) {
375
+ if (
376
+ node.callee.type === "Identifier" &&
377
+ node.callee.name === "require" &&
378
+ node.arguments.length === 1
379
+ ) {
380
+ check(node.arguments[0]);
381
+ }
382
+ },
170
383
  };
171
384
  },
172
385
  };
@@ -22,7 +22,6 @@ export default {
22
22
  "Disallow Solid's removed JSX attribute namespaces. Solid 2.0 dropped `on:`, `oncapture:`, `use:`, `prop:`, `attr:`, `bool:`, `class:` and `style:`.",
23
23
  url: "https://docs.solidjs.com/",
24
24
  },
25
- hasSuggestions: true,
26
25
  schema: [
27
26
  {
28
27
  type: "object",
@@ -43,12 +42,11 @@ export default {
43
42
  },
44
43
  ],
45
44
  messages: {
46
- removed: "'{{namespace}}:' was removed in Solid 2.0. Use {{replacement}} instead.",
45
+ removed:
46
+ "'{{namespace}}:' was removed in Solid 2.0. Use {{replacement}} instead.",
47
47
  unknown:
48
48
  "'{{namespace}}:' is not a namespace Solid understands. Solid 2.0 has no special JSX prefixes.",
49
49
  component: "Namespaced props have no effect on components.",
50
- "component-suggest":
51
- "Replace {{namespace}}:{{name}} with {{name}}.",
52
50
  },
53
51
  },
54
52
  defaultOptions: [],
@@ -57,34 +55,24 @@ export default {
57
55
  context.options?.[0]?.allowedNamespaces;
58
56
  return {
59
57
  "JSXAttribute > JSXNamespacedName": (node) => {
58
+ const namespace = node.namespace?.name;
59
+ // The allow list covers every element: a framework can give a
60
+ // namespace meaning on components (`<Island client:visible>`).
61
+ if (explicitlyAllowedNamespaces?.includes(namespace)) {
62
+ return;
63
+ }
60
64
  const openingElement = node.parent.parent;
61
65
  if (
62
66
  openingElement.name.type === "JSXIdentifier" &&
63
67
  !isDOMElementName(openingElement.name.name)
64
68
  ) {
65
- // no namespaces on Solid component elements
66
- context.report({
67
- node,
68
- messageId: "component",
69
- suggest: [
70
- {
71
- messageId: "component-suggest",
72
- data: {
73
- namespace: node.namespace.name,
74
- name: node.name.name,
75
- },
76
- fix: (fixer) =>
77
- fixer.replaceText(node, node.name.name),
78
- },
79
- ],
80
- });
69
+ // Solid itself gives a namespaced prop on a component no
70
+ // meaning. No suggestion: deleting the prefix changes the
71
+ // prop's name, and the rule cannot know which one is meant.
72
+ context.report({ node, messageId: "component" });
81
73
  return;
82
74
  }
83
- const namespace = node.namespace?.name;
84
- if (
85
- xmlNamespaces.includes(namespace) ||
86
- explicitlyAllowedNamespaces?.includes(namespace)
87
- ) {
75
+ if (xmlNamespaces.includes(namespace)) {
88
76
  return;
89
77
  }
90
78
  const replacement = replacements[namespace];