@zgeoff/oxlint-config 0.10.1 → 0.10.3

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
@@ -70,8 +70,8 @@ The plugin is addressable on its own at `@zgeoff/oxlint-config/plugin` for confi
70
70
  - Comments: `zgeoff/no-jsdoc` and `zgeoff/max-consecutive-line-comments` keep comments to the reason
71
71
  for a line. See
72
72
  [the comment rules](../../docs/architecture/oxlint-config.md#zgeoffno-jsdoc-and-zgeoffmax-consecutive-line-comments).
73
- - Code style: function verbs, verb contracts, module order, abbreviated names and classes. See
74
- [code-style rules](../../docs/architecture/oxlint-config-code-style.md).
73
+ - Code style: function verbs, verb contracts, module order, file names, abbreviated names and
74
+ classes. See [code-style rules](../../docs/architecture/oxlint-config-code-style.md).
75
75
  - Test files: a test sits beside its module, holds only `setupTest()` and its tests, uses inline
76
76
  snapshots, keeps each timeout within its lane's limit, and waits on a condition, never a sleep.
77
77
  See [test-file rules](../../docs/architecture/oxlint-config-tests.md).
@@ -1,5 +1,7 @@
1
1
  import { basename } from 'node:path';
2
2
  import buildLaneLimits from './build-lane-limits.js';
3
+ import findConstInitializer from './find-const-initializer.js';
4
+ import findVariable from './find-variable.js';
3
5
  import getImportedName from './get-imported-name.js';
4
6
  import resolveSuiteDirectory from './resolve-suite-directory.js';
5
7
 
@@ -103,6 +105,7 @@ function findRunnerName(context, callee) {
103
105
 
104
106
  const variable = findVariable(context, current);
105
107
 
108
+ // a name with no declaration reads the global that `bun test` defines
106
109
  if (variable === null) {
107
110
  return current.name;
108
111
  }
@@ -124,19 +127,6 @@ function findRunnerName(context, callee) {
124
127
  return definition.node.type === 'ImportSpecifier' ? getImportedName(definition.node) : null;
125
128
  }
126
129
 
127
- // a name with no declaration reads the global that `bun test` defines
128
- function findVariable(context, identifier) {
129
- for (let scope = context.sourceCode.getScope(identifier); scope !== null; scope = scope.upper) {
130
- const variable = scope.set.get(identifier.name);
131
-
132
- if (variable !== undefined && variable.defs.length > 0) {
133
- return variable;
134
- }
135
- }
136
-
137
- return null;
138
- }
139
-
140
130
  function emitTimeouts(context, args, limit) {
141
131
  for (const argument of args) {
142
132
  const value = argument.type === 'ObjectExpression' ? findTimeoutValue(argument) : argument;
@@ -182,16 +172,9 @@ function findNumber(context, node, seen) {
182
172
  return null;
183
173
  }
184
174
 
185
- const definition = findVariable(context, node)?.defs[0];
186
-
187
- if (
188
- definition?.type !== 'Variable' ||
189
- definition.parent.kind !== 'const' ||
190
- definition.node.id.type !== 'Identifier' ||
191
- definition.node.init === null
192
- ) {
193
- return null;
194
- }
175
+ const initializer = findConstInitializer(context, node);
195
176
 
196
- return findNumber(context, definition.node.init, new Set([...seen, node.name]));
177
+ return initializer === null
178
+ ? null
179
+ : findNumber(context, initializer, new Set([...seen, node.name]));
197
180
  }
@@ -0,0 +1,263 @@
1
+ import { basename, dirname } from 'node:path';
2
+ import collectCreatedTables from './collect-created-tables.js';
3
+ import collectSQLWrites from './collect-sql-writes.js';
4
+ import collectWrittenTables from './collect-written-tables.js';
5
+ import findConstInitializer from './find-const-initializer.js';
6
+ import findFunctionName from './find-function-name.js';
7
+ import isInTestUtils from './is-in-test-utils.js';
8
+ import isVerbMatch from './is-verb-match.js';
9
+ import resolveRepoPath from './resolve-repo-path.js';
10
+ import toTableName from './to-table-name.js';
11
+
12
+ export default function buildSeedOnly(readFolderSources) {
13
+ const folderTables = new Map();
14
+
15
+ const readFolderTables = (directory) => {
16
+ const cached = folderTables.get(directory);
17
+
18
+ if (cached !== undefined) {
19
+ return cached;
20
+ }
21
+
22
+ const tables = new Set(
23
+ readFolderSources(directory)
24
+ .filter((source) => !source.name.startsWith('seed-'))
25
+ .flatMap((source) => collectWrittenTables(source.text)),
26
+ );
27
+
28
+ folderTables.set(directory, tables);
29
+
30
+ return tables;
31
+ };
32
+
33
+ return {
34
+ meta: {
35
+ type: 'problem',
36
+ messages: {
37
+ write:
38
+ "Seed '{{table}}' through the module that writes it, with a `seed…` function from its `test-utils`. A direct write passes only inside a `seed…` function in `test-utils`, in a test beside a module that writes '{{table}}', in a migration test and in a seed's own test.",
39
+ nonLiteral:
40
+ "Name the table in a string literal: the rule finds a table's owner by the table's name.",
41
+ },
42
+ schema: [],
43
+ },
44
+ create(context) {
45
+ const filename = context.physicalFilename;
46
+ const kind = pickFileKind(resolveRepoPath(filename, context.cwd), basename(filename));
47
+
48
+ if (kind === null) {
49
+ return {};
50
+ }
51
+
52
+ // a table the file creates is the test's own, so a write to it counts only once the whole
53
+ // file is read
54
+ const writes = [];
55
+ const createdTables = new Set();
56
+
57
+ const collectText = (node) => {
58
+ const text = toSQLText(context, node, new Set());
59
+
60
+ if (text === null) {
61
+ return;
62
+ }
63
+
64
+ for (const table of collectSQLWrites(text)) {
65
+ // the statement, not its call, so a disable comment above a statement inside a
66
+ // multi-line call covers it
67
+ writes.push({ node, table });
68
+ }
69
+
70
+ for (const table of collectCreatedTables(text)) {
71
+ createdTables.add(table);
72
+ }
73
+ };
74
+
75
+ const checkWrite = (write) => {
76
+ if ((kind.inTestUtils && isInSeedFunction(write.node)) || createdTables.has(write.table)) {
77
+ return;
78
+ }
79
+
80
+ if (write.table === null) {
81
+ context.report({ node: write.node, messageId: 'nonLiteral' });
82
+ } else if (!kind.isTest || !readFolderTables(dirname(filename)).has(write.table)) {
83
+ context.report({ node: write.node, messageId: 'write', data: { table: write.table } });
84
+ }
85
+ };
86
+
87
+ return {
88
+ CallExpression(node) {
89
+ if (isKyselyWrite(node)) {
90
+ writes.push({ node, table: findLiteralTable(node.arguments[0]) });
91
+ } else if (isKyselyCreateTable(node)) {
92
+ const table = findLiteralTable(node.arguments[0]);
93
+
94
+ if (table !== null) {
95
+ createdTables.add(table);
96
+ }
97
+ } else if (isSQLCall(context, node.callee)) {
98
+ collectText(node.arguments[0]);
99
+ }
100
+ },
101
+ TaggedTemplateExpression(node) {
102
+ if (isSQLTag(node.tag)) {
103
+ collectText(node.quasi);
104
+ }
105
+ },
106
+ 'Program:exit'() {
107
+ for (const write of writes) {
108
+ checkWrite(write);
109
+ }
110
+ },
111
+ };
112
+ },
113
+ };
114
+ }
115
+
116
+ const testFilePattern = /\.test\.tsx?$/u;
117
+ const seedTestPattern = /^seed-.+\.test\.tsx?$/u;
118
+ const migrationWords = new Set(['migrate', 'migration', 'migrations']);
119
+
120
+ function pickFileKind(repoPath, name) {
121
+ const inTestUtils = isInTestUtils(repoPath);
122
+ const isTest = testFilePattern.test(name);
123
+ const isMigrationTest = repoPath.split(/[-./\\_]/u).some((word) => migrationWords.has(word));
124
+
125
+ if (isTest && (isMigrationTest || (inTestUtils && seedTestPattern.test(name)))) {
126
+ return null;
127
+ }
128
+
129
+ return isTest || inTestUtils ? { inTestUtils, isTest } : null;
130
+ }
131
+
132
+ // oxlint-disable-next-line no-template-curly-in-string -- marks an interpolation as a module's text shows it
133
+ const interpolation = '${}';
134
+
135
+ function toSQLText(context, node, seen) {
136
+ if (node?.type === 'Literal' && typeof node.value === 'string') {
137
+ return node.value;
138
+ }
139
+
140
+ if (node?.type === 'TemplateLiteral') {
141
+ return node.quasis.map((quasi) => quasi.value.cooked ?? quasi.value.raw).join(interpolation);
142
+ }
143
+
144
+ // a part the rule cannot read stands for an interpolation, so its table name counts as not a
145
+ // string literal
146
+ if (node?.type === 'BinaryExpression' && node.operator === '+') {
147
+ const left = toSQLText(context, node.left, seen);
148
+ const right = toSQLText(context, node.right, seen);
149
+
150
+ return left === null && right === null
151
+ ? null
152
+ : `${left ?? interpolation}${right ?? interpolation}`;
153
+ }
154
+
155
+ if (node?.type !== 'Identifier' || seen.has(node.name)) {
156
+ return null;
157
+ }
158
+
159
+ const initializer = findConstInitializer(context, node);
160
+
161
+ return initializer === null
162
+ ? null
163
+ : toSQLText(context, initializer, new Set([...seen, node.name]));
164
+ }
165
+
166
+ const functionTypes = new Set([
167
+ 'FunctionDeclaration',
168
+ 'FunctionExpression',
169
+ 'ArrowFunctionExpression',
170
+ ]);
171
+
172
+ // a seed's callbacks write for the seed, while a seed-named helper inside another function does not
173
+ function isInSeedFunction(node) {
174
+ let outermost = null;
175
+
176
+ for (
177
+ let current = node.parent;
178
+ current !== null && current !== undefined;
179
+ current = current.parent
180
+ ) {
181
+ if (functionTypes.has(current.type)) {
182
+ outermost = current;
183
+ }
184
+ }
185
+
186
+ const name = outermost === null ? null : findFunctionName(outermost);
187
+
188
+ return name !== null && isVerbMatch(name.name, 'seed');
189
+ }
190
+
191
+ const kyselyWriteMethods = new Set(['insertInto', 'updateTable', 'deleteFrom']);
192
+
193
+ function isKyselyWrite(node) {
194
+ const callee = node.callee;
195
+
196
+ return (
197
+ callee.type === 'MemberExpression' &&
198
+ !callee.computed &&
199
+ callee.property.type === 'Identifier' &&
200
+ kyselyWriteMethods.has(callee.property.name)
201
+ );
202
+ }
203
+
204
+ function findLiteralTable(argument) {
205
+ return argument?.type === 'Literal' && typeof argument.value === 'string'
206
+ ? toTableName(argument.value)
207
+ : null;
208
+ }
209
+
210
+ function isKyselyCreateTable(node) {
211
+ const callee = node.callee;
212
+
213
+ return (
214
+ callee.type === 'MemberExpression' &&
215
+ !callee.computed &&
216
+ callee.property.type === 'Identifier' &&
217
+ callee.property.name === 'createTable'
218
+ );
219
+ }
220
+
221
+ // bun:sqlite and better-sqlite3 run SQL through `run`, `exec`, `query` and `prepare`, Kysely
222
+ // through `sql.raw` and `CompiledQuery.raw`, and postgres.js through `sql(…)` and `sql.unsafe`
223
+ const sqlMethods = new Set(['run', 'exec', 'query', 'prepare', 'raw', 'unsafe']);
224
+
225
+ function isSQLCall(context, callee) {
226
+ if (callee.type === 'Identifier') {
227
+ return callee.name === 'sql';
228
+ }
229
+
230
+ // a regular expression's `exec` takes prose
231
+ return (
232
+ callee.type === 'MemberExpression' &&
233
+ !callee.computed &&
234
+ callee.property.type === 'Identifier' &&
235
+ sqlMethods.has(callee.property.name) &&
236
+ !isRegularExpression(context, callee.object)
237
+ );
238
+ }
239
+
240
+ function isRegularExpression(context, node) {
241
+ if (node.type === 'Identifier') {
242
+ const initializer = findConstInitializer(context, node);
243
+
244
+ return (
245
+ initializer !== null &&
246
+ initializer.type !== 'Identifier' &&
247
+ isRegularExpression(context, initializer)
248
+ );
249
+ }
250
+
251
+ return (
252
+ node.regex !== undefined ||
253
+ ((node.type === 'NewExpression' || node.type === 'CallExpression') &&
254
+ node.callee.type === 'Identifier' &&
255
+ node.callee.name === 'RegExp')
256
+ );
257
+ }
258
+
259
+ function isSQLTag(tag) {
260
+ const name = tag.type === 'MemberExpression' && !tag.computed ? tag.property : tag;
261
+
262
+ return name.type === 'Identifier' && name.name === 'sql';
263
+ }
@@ -0,0 +1,13 @@
1
+ import { tablePattern } from './collect-sql-writes.js';
2
+ import toTableName from './to-table-name.js';
3
+
4
+ const createTablePattern = new RegExp(
5
+ String.raw`\bcreate\s+(?:(?:temp|temporary|virtual)\s+)?table\s+(?:if\s+not\s+exists\s+)?${tablePattern}`,
6
+ 'giu',
7
+ );
8
+
9
+ export default function collectCreatedTables(text) {
10
+ return [...text.matchAll(createTablePattern)]
11
+ .map((match) => toTableName(match.groups.table))
12
+ .filter((table) => table !== null);
13
+ }
@@ -1,6 +1,6 @@
1
1
  import toTableName from './to-table-name.js';
2
2
 
3
- const tablePattern = String.raw`(?<table>[^\s(),;'\x60]+)`;
3
+ export const tablePattern = String.raw`(?<table>[^\s(),;'\x60]+)`;
4
4
 
5
5
  const sqlWritePatterns = [
6
6
  new RegExp(String.raw`\binsert\s+(?:or\s+[a-z]+\s+)?into\s+${tablePattern}`, 'giu'),
@@ -0,0 +1,177 @@
1
+ import { basename } from 'node:path';
2
+
3
+ const exemptBases = new Set(['index', 'types', 'constants']);
4
+
5
+ const fileNamedForExport = {
6
+ meta: {
7
+ type: 'suggestion',
8
+ messages: {
9
+ mismatch:
10
+ "No export of '{{file}}' is named for the file: name the main export '{{base}}' in camelCase, PascalCase or SCREAMING_SNAKE_CASE, or rename the file for its main export.",
11
+ },
12
+ schema: [],
13
+ },
14
+ create(context) {
15
+ return {
16
+ Program(program) {
17
+ const file = basename(context.physicalFilename);
18
+ const [base, ...suffixes] = file.split('.');
19
+
20
+ // a role suffix, such as `.config` or `.bench`, marks a file that a tool loads, and the
21
+ // tool sets its exports
22
+ if (suffixes.length !== 1 || exemptBases.has(base)) {
23
+ return;
24
+ }
25
+
26
+ const exports = collectExports(program.body);
27
+
28
+ if (exports.statements.length === 0 || exports.names === null) {
29
+ return;
30
+ }
31
+
32
+ const wanted = toComparableName(base);
33
+
34
+ if (exports.names.some((name) => toComparableName(name) === wanted)) {
35
+ return;
36
+ }
37
+
38
+ context.report({
39
+ node: exports.statements[0],
40
+ messageId: 'mismatch',
41
+ data: { file, base },
42
+ });
43
+ },
44
+ };
45
+ },
46
+ };
47
+
48
+ export default fileNamedForExport;
49
+
50
+ const exportTypes = new Set([
51
+ 'ExportNamedDeclaration',
52
+ 'ExportDefaultDeclaration',
53
+ 'ExportAllDeclaration',
54
+ 'TSExportAssignment',
55
+ ]);
56
+
57
+ // an `export * from` or an `export { default } from` hides the name it exports, so names is null
58
+ // and the file passes
59
+ function collectExports(body) {
60
+ const statements = body.filter(
61
+ (statement) => exportTypes.has(statement.type) && !isEmptyExport(statement),
62
+ );
63
+ const names = [];
64
+
65
+ for (const statement of statements) {
66
+ const exported = collectExportNames(statement);
67
+
68
+ if (exported === null) {
69
+ return { statements, names: null };
70
+ }
71
+
72
+ names.push(...exported);
73
+ }
74
+
75
+ return { statements, names };
76
+ }
77
+
78
+ // `export {}` only marks a script as a module
79
+ function isEmptyExport(statement) {
80
+ return (
81
+ statement.type === 'ExportNamedDeclaration' &&
82
+ statement.declaration === null &&
83
+ statement.specifiers.length === 0
84
+ );
85
+ }
86
+
87
+ function collectExportNames(statement) {
88
+ if (statement.type === 'ExportAllDeclaration') {
89
+ return statement.exported === null ? null : [toExportName(statement.exported)];
90
+ }
91
+
92
+ if (statement.type === 'TSExportAssignment') {
93
+ return collectValueNames(statement.expression);
94
+ }
95
+
96
+ if (statement.type === 'ExportDefaultDeclaration') {
97
+ return collectValueNames(statement.declaration);
98
+ }
99
+
100
+ if (statement.declaration !== null) {
101
+ return collectDeclarationNames(statement.declaration);
102
+ }
103
+
104
+ const names = statement.specifiers.map((specifier) => {
105
+ const exported = toExportName(specifier.exported);
106
+
107
+ return exported === 'default' ? toExportName(specifier.local) : exported;
108
+ });
109
+
110
+ return statement.source !== null && names.includes('default') ? null : names;
111
+ }
112
+
113
+ function toExportName(node) {
114
+ return node.type === 'Identifier' ? node.name : node.value;
115
+ }
116
+
117
+ const wrapperTypes = new Set(['TSAsExpression', 'TSSatisfiesExpression', 'TSNonNullExpression']);
118
+
119
+ function collectValueNames(value) {
120
+ if (wrapperTypes.has(value.type)) {
121
+ return collectValueNames(value.expression);
122
+ }
123
+
124
+ if (value.type === 'Identifier') {
125
+ return [value.name];
126
+ }
127
+
128
+ return value.id?.type === 'Identifier' ? [value.id.name] : [];
129
+ }
130
+
131
+ function collectDeclarationNames(declaration) {
132
+ if (declaration.type === 'VariableDeclaration') {
133
+ return declaration.declarations.flatMap((declarator) => collectPatternNames(declarator.id));
134
+ }
135
+
136
+ return declaration.id === null || declaration.id === undefined
137
+ ? []
138
+ : [toRootName(declaration.id)];
139
+ }
140
+
141
+ function collectPatternNames(pattern) {
142
+ if (pattern.type === 'Identifier') {
143
+ return [pattern.name];
144
+ }
145
+
146
+ if (pattern.type === 'ObjectPattern') {
147
+ return pattern.properties.flatMap((property) => {
148
+ const target = property.type === 'RestElement' ? property.argument : property.value;
149
+
150
+ return collectPatternNames(target);
151
+ });
152
+ }
153
+
154
+ if (pattern.type === 'ArrayPattern') {
155
+ return pattern.elements.flatMap((element) =>
156
+ element === null ? [] : collectPatternNames(element),
157
+ );
158
+ }
159
+
160
+ if (pattern.type === 'AssignmentPattern') {
161
+ return collectPatternNames(pattern.left);
162
+ }
163
+
164
+ return pattern.type === 'RestElement' ? collectPatternNames(pattern.argument) : [];
165
+ }
166
+
167
+ function toRootName(name) {
168
+ if (name.type === 'TSQualifiedName') {
169
+ return toRootName(name.left);
170
+ }
171
+
172
+ return toExportName(name);
173
+ }
174
+
175
+ function toComparableName(name) {
176
+ return name.replaceAll(/[-_]/gu, '').toLowerCase();
177
+ }
@@ -0,0 +1,15 @@
1
+ import findVariable from './find-variable.js';
2
+
3
+ export default function findConstInitializer(context, identifier) {
4
+ const definition = findVariable(context, identifier)?.defs[0];
5
+
6
+ if (
7
+ definition?.type !== 'Variable' ||
8
+ definition.parent.kind !== 'const' ||
9
+ definition.node.id.type !== 'Identifier'
10
+ ) {
11
+ return null;
12
+ }
13
+
14
+ return definition.node.init;
15
+ }
@@ -0,0 +1,11 @@
1
+ export default function findVariable(context, identifier) {
2
+ for (let scope = context.sourceCode.getScope(identifier); scope !== null; scope = scope.upper) {
3
+ const variable = scope.set.get(identifier.name);
4
+
5
+ if (variable !== undefined && variable.defs.length > 0) {
6
+ return variable;
7
+ }
8
+ }
9
+
10
+ return null;
11
+ }
@@ -0,0 +1,2 @@
1
+ export { default as collectRefusingEdges } from '../collect-refusing-edges.js';
2
+ export { default as importZonesSchema } from '../import-zones-schema.js';
package/oxlintrc.json CHANGED
@@ -45,6 +45,7 @@
45
45
  "zgeoff/feature-folders": "error",
46
46
  "zgeoff/seed-only": "error",
47
47
  "zgeoff/test-beside-seed": "error",
48
+ "zgeoff/file-named-for-export": "error",
48
49
 
49
50
  // name a value for what it holds, in full words: one letter only for a
50
51
  // coordinate, zod's `z`, Bun's shell `$` or an unread `_` placeholder, and
@@ -156,7 +157,7 @@
156
157
  "unicorn/no-useless-undefined": ["error", { "checkArguments": false }],
157
158
  "unicorn/no-null": "off",
158
159
  "unicorn/number-literal-case": "off",
159
- "unicorn/filename-case": "off",
160
+ "unicorn/filename-case": ["error", { "case": "kebabCase" }],
160
161
 
161
162
  // one declaration per variable; a declaration list has no order to sort
162
163
  "one-var": ["error", "never"],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zgeoff/oxlint-config",
3
- "version": "0.10.1",
3
+ "version": "0.10.3",
4
4
  "description": "Shareable oxlint config: strict categories, house-style rules, and a custom JS plugin",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -28,10 +28,10 @@
28
28
  "no-jsdoc.js",
29
29
  "no-comment-rule-override.js",
30
30
  "no-hidden-cast.js",
31
- "test-beside-module.js",
31
+ "build-test-beside-module.js",
32
32
  "no-file-snapshot.js",
33
33
  "setup-test-only.js",
34
- "max-test-timeout.js",
34
+ "build-max-test-timeout.js",
35
35
  "test-timeout-exception-ticket.js",
36
36
  "build-lane-limits.js",
37
37
  "read-rig-lanes.js",
@@ -47,8 +47,8 @@
47
47
  "find-static-key.js",
48
48
  "import-zones.js",
49
49
  "import-zones-schema.js",
50
- "import-zones-entry.js",
51
- "import-zones-entry.d.ts",
50
+ "import-zones/index.js",
51
+ "import-zones/index.d.ts",
52
52
  "collect-refusing-edges.js",
53
53
  "is-edge-refusing.js",
54
54
  "is-entry-applying.js",
@@ -70,11 +70,15 @@
70
70
  "read-stale-pending.js",
71
71
  "read-package-manifest.js",
72
72
  "read-module-paths.js",
73
- "seed-only.js",
73
+ "build-seed-only.js",
74
74
  "test-beside-seed.js",
75
+ "file-named-for-export.js",
75
76
  "read-folder-sources.js",
76
77
  "collect-written-tables.js",
77
78
  "collect-sql-writes.js",
79
+ "collect-created-tables.js",
80
+ "find-const-initializer.js",
81
+ "find-variable.js",
78
82
  "to-table-name.js",
79
83
  "resolve-repo-path.js",
80
84
  "is-in-test-utils.js",
@@ -87,8 +91,8 @@
87
91
  "./react.json": "./react.json",
88
92
  "./plugin": "./plugin.js",
89
93
  "./import-zones": {
90
- "types": "./import-zones-entry.d.ts",
91
- "default": "./import-zones-entry.js"
94
+ "types": "./import-zones/index.d.ts",
95
+ "default": "./import-zones/index.js"
92
96
  }
93
97
  },
94
98
  "publishConfig": {
@@ -102,7 +106,7 @@
102
106
  },
103
107
  "devDependencies": {
104
108
  "@types/bun": "1.4.4",
105
- "@zgeoff/rig": "0.2.1",
109
+ "@zgeoff/rig": "0.2.2",
106
110
  "@zgeoff/std": "0.4.3",
107
111
  "@zgeoff/testing": "0.3.0",
108
112
  "tsrs": "0.18.0-ts7.1.0-dev.20260929",
package/plugin.js CHANGED
@@ -2,11 +2,14 @@ import { existsSync } from 'node:fs';
2
2
  // oxlint-disable-next-line zgeoff/no-require -- only resolves rig's path from the repo root; an import loads it
3
3
  import { createRequire } from 'node:module';
4
4
  import { join } from 'node:path';
5
+ import buildMaxTestTimeout from './build-max-test-timeout.js';
6
+ import buildSeedOnly from './build-seed-only.js';
7
+ import buildTestBesideModule from './build-test-beside-module.js';
5
8
  import featureFolders from './feature-folders.js';
9
+ import fileNamedForExport from './file-named-for-export.js';
6
10
  import functionVerb from './function-verb.js';
7
11
  import importZones from './import-zones.js';
8
12
  import maxConsecutiveLineComments from './max-consecutive-line-comments.js';
9
- import buildMaxTestTimeout from './max-test-timeout.js';
10
13
  import moduleOrder from './module-order.js';
11
14
  import noAbbreviatedNames from './no-abbreviated-names.js';
12
15
  import noClass from './no-class.js';
@@ -20,9 +23,7 @@ import noTestSleep from './no-test-sleep.js';
20
23
  import readFolderSources from './read-folder-sources.js';
21
24
  import readRigLanes from './read-rig-lanes.js';
22
25
  import resolveRepoRoot from './resolve-repo-root.js';
23
- import buildSeedOnly from './seed-only.js';
24
26
  import setupTestOnly from './setup-test-only.js';
25
- import buildTestBesideModule from './test-beside-module.js';
26
27
  import testBesideSeed from './test-beside-seed.js';
27
28
  import testShape from './test-shape.js';
28
29
  import testTimeoutExceptionTicket from './test-timeout-exception-ticket.js';
@@ -138,6 +139,7 @@ const plugin = {
138
139
  'feature-folders': featureFolders,
139
140
  'seed-only': buildSeedOnly(readFolderSources),
140
141
  'test-beside-seed': testBesideSeed,
142
+ 'file-named-for-export': fileNamedForExport,
141
143
  },
142
144
  };
143
145
 
@@ -1,2 +0,0 @@
1
- export { default as collectRefusingEdges } from './collect-refusing-edges.js';
2
- export { default as importZonesSchema } from './import-zones-schema.js';
package/seed-only.js DELETED
@@ -1,168 +0,0 @@
1
- import { basename, dirname } from 'node:path';
2
- import collectSQLWrites from './collect-sql-writes.js';
3
- import collectWrittenTables from './collect-written-tables.js';
4
- import findBunTestName from './find-bun-test-name.js';
5
- import findChainRoot from './find-chain-root.js';
6
- import findFunctionName from './find-function-name.js';
7
- import isInTestUtils from './is-in-test-utils.js';
8
- import isVerbMatch from './is-verb-match.js';
9
- import resolveRepoPath from './resolve-repo-path.js';
10
- import toTableName from './to-table-name.js';
11
-
12
- export default function buildSeedOnly(readFolderSources) {
13
- const folderTables = new Map();
14
-
15
- const readFolderTables = (directory) => {
16
- const cached = folderTables.get(directory);
17
-
18
- if (cached !== undefined) {
19
- return cached;
20
- }
21
-
22
- const tables = new Set(
23
- readFolderSources(directory)
24
- .filter((source) => !source.name.startsWith('seed-'))
25
- .flatMap((source) => collectWrittenTables(source.text)),
26
- );
27
-
28
- folderTables.set(directory, tables);
29
-
30
- return tables;
31
- };
32
-
33
- return {
34
- meta: {
35
- type: 'problem',
36
- messages: {
37
- write:
38
- "Seed '{{table}}' through the module that writes it, with a `seed…` function from its `test-utils`. A direct write passes only inside a `seed…` function in `test-utils`, in a test beside a module that writes '{{table}}', in a migration test and in a seed's own test.",
39
- nonLiteral:
40
- "Name the table in a string literal: the rule finds a table's owner by the table's name.",
41
- },
42
- schema: [],
43
- },
44
- create(context) {
45
- const filename = context.physicalFilename;
46
- const kind = pickFileKind(resolveRepoPath(filename, context.cwd), basename(filename));
47
-
48
- if (kind === null) {
49
- return {};
50
- }
51
-
52
- const checkWrite = (node, table) => {
53
- if (kind.inTestUtils && isInSeedFunction(node)) {
54
- return;
55
- }
56
-
57
- if (table === null) {
58
- context.report({ node, messageId: 'nonLiteral' });
59
- } else if (!kind.isTest || !readFolderTables(dirname(filename)).has(table)) {
60
- context.report({ node, messageId: 'write', data: { table } });
61
- }
62
- };
63
-
64
- const checkText = (node, text) => {
65
- if (!isTestTitle(context, node)) {
66
- for (const table of collectSQLWrites(text)) {
67
- checkWrite(node, table);
68
- }
69
- }
70
- };
71
-
72
- return {
73
- CallExpression(node) {
74
- if (isKyselyWrite(node)) {
75
- checkWrite(node, findLiteralTable(node.arguments[0]));
76
- }
77
- },
78
- Literal(node) {
79
- if (typeof node.value === 'string') {
80
- checkText(node, node.value);
81
- }
82
- },
83
- TemplateLiteral(node) {
84
- const text = node.quasis
85
- .map((quasi) => quasi.value.cooked ?? quasi.value.raw)
86
- // oxlint-disable-next-line no-template-curly-in-string -- marks each interpolation as a module's text shows it
87
- .join('${}');
88
-
89
- checkText(node, text);
90
- },
91
- };
92
- },
93
- };
94
- }
95
-
96
- const testFilePattern = /\.test\.tsx?$/u;
97
- const seedTestPattern = /^seed-.+\.test\.tsx?$/u;
98
- const migrationWords = new Set(['migrate', 'migration', 'migrations']);
99
-
100
- function pickFileKind(repoPath, name) {
101
- const inTestUtils = isInTestUtils(repoPath);
102
- const isTest = testFilePattern.test(name);
103
- const isMigrationTest = repoPath.split(/[-./\\_]/u).some((word) => migrationWords.has(word));
104
-
105
- if (isTest && (isMigrationTest || (inTestUtils && seedTestPattern.test(name)))) {
106
- return null;
107
- }
108
-
109
- return isTest || inTestUtils ? { inTestUtils, isTest } : null;
110
- }
111
-
112
- const functionTypes = new Set([
113
- 'FunctionDeclaration',
114
- 'FunctionExpression',
115
- 'ArrowFunctionExpression',
116
- ]);
117
-
118
- // a seed's callbacks write for the seed, while a seed-named helper inside another function does not
119
- function isInSeedFunction(node) {
120
- let outermost = null;
121
-
122
- for (
123
- let current = node.parent;
124
- current !== null && current !== undefined;
125
- current = current.parent
126
- ) {
127
- if (functionTypes.has(current.type)) {
128
- outermost = current;
129
- }
130
- }
131
-
132
- const name = outermost === null ? null : findFunctionName(outermost);
133
-
134
- return name !== null && isVerbMatch(name.name, 'seed');
135
- }
136
-
137
- const testRunnerNames = new Set(['test', 'it', 'describe']);
138
-
139
- function isTestTitle(context, node) {
140
- const call = node.parent;
141
-
142
- if (call?.type !== 'CallExpression' || call.arguments[0] !== node) {
143
- return false;
144
- }
145
-
146
- const root = findChainRoot(call.callee, true);
147
-
148
- return root !== null && testRunnerNames.has(findBunTestName(context, root));
149
- }
150
-
151
- const kyselyWriteMethods = new Set(['insertInto', 'updateTable', 'deleteFrom']);
152
-
153
- function isKyselyWrite(node) {
154
- const callee = node.callee;
155
-
156
- return (
157
- callee.type === 'MemberExpression' &&
158
- !callee.computed &&
159
- callee.property.type === 'Identifier' &&
160
- kyselyWriteMethods.has(callee.property.name)
161
- );
162
- }
163
-
164
- function findLiteralTable(argument) {
165
- return argument?.type === 'Literal' && typeof argument.value === 'string'
166
- ? toTableName(argument.value)
167
- : null;
168
- }