@valbuild/eslint-plugin 0.67.0 → 0.67.2

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.
@@ -4,11 +4,13 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var path = require('path');
6
6
  var fs = require('fs');
7
+ var ts = require('typescript');
7
8
 
8
9
  function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
9
10
 
10
11
  var path__default = /*#__PURE__*/_interopDefault(path);
11
12
  var fs__default = /*#__PURE__*/_interopDefault(fs);
13
+ var ts__default = /*#__PURE__*/_interopDefault(ts);
12
14
 
13
15
  // @ts-check
14
16
 
@@ -275,6 +277,110 @@ var defaultExportValModule = {
275
277
 
276
278
  // @ts-check
277
279
 
280
+ /** @type {Record<string, ts.ModuleResolutionCache>} */
281
+ var cache = {};
282
+ /**
283
+ * @type {import('eslint').Rule.RuleModule}
284
+ */
285
+ var moduleInValModules = {
286
+ meta: {
287
+ type: "problem",
288
+ docs: {
289
+ description: "Ensure all .val files with export default c.define are referenced in val.modules",
290
+ recommended: true
291
+ },
292
+ schema: [],
293
+ // No options needed
294
+ messages: {
295
+ missingFile: "'{{file}}' contains export default c.define but is not referenced in val.modules.ts or val.module.js."
296
+ }
297
+ },
298
+ create: function create(context) {
299
+ var projectDir = path__default["default"].resolve(context.cwd || context.getCwd());
300
+ var modulesFilePath = path__default["default"].join(projectDir, "val.modules.ts");
301
+ var baseUrl = projectDir;
302
+ /** @type { {[key: string]: string[];} } */
303
+ var paths = {};
304
+ var cacheKey = baseUrl;
305
+ var tsConfigPath = ts__default["default"].findConfigFile(projectDir, ts__default["default"].sys.fileExists, "tsconfig.json");
306
+ var jsConfigPath = ts__default["default"].findConfigFile(projectDir, ts__default["default"].sys.fileExists, "jsconfig.json");
307
+ if (tsConfigPath) {
308
+ var _tsConfig$compilerOpt, _tsConfig$compilerOpt2;
309
+ var tsConfig = tsConfigPath ? JSON.parse(fs__default["default"].readFileSync(tsConfigPath, "utf-8")) : {};
310
+ baseUrl = (_tsConfig$compilerOpt = tsConfig.compilerOptions) !== null && _tsConfig$compilerOpt !== void 0 && _tsConfig$compilerOpt.baseUrl ? path__default["default"].resolve(projectDir, tsConfig.compilerOptions.baseUrl) : projectDir;
311
+ paths = ((_tsConfig$compilerOpt2 = tsConfig.compilerOptions) === null || _tsConfig$compilerOpt2 === void 0 ? void 0 : _tsConfig$compilerOpt2.paths) || {};
312
+ cacheKey = baseUrl + "/tsconfig.json";
313
+ if (!cache[cacheKey]) {
314
+ cache[cacheKey] = ts__default["default"].createModuleResolutionCache(baseUrl, function (x) {
315
+ return x;
316
+ }, tsConfig.compilerOptions);
317
+ }
318
+ } else if (jsConfigPath) {
319
+ var _jsConfig$compilerOpt, _jsConfig$compilerOpt2;
320
+ var jsConfig = jsConfigPath ? JSON.parse(fs__default["default"].readFileSync(jsConfigPath, "utf-8")) : {};
321
+ baseUrl = (_jsConfig$compilerOpt = jsConfig.compilerOptions) !== null && _jsConfig$compilerOpt !== void 0 && _jsConfig$compilerOpt.baseUrl ? path__default["default"].resolve(projectDir, jsConfig.compilerOptions.baseUrl) : projectDir;
322
+ paths = ((_jsConfig$compilerOpt2 = jsConfig.compilerOptions) === null || _jsConfig$compilerOpt2 === void 0 ? void 0 : _jsConfig$compilerOpt2.paths) || {};
323
+ cacheKey = baseUrl + "/jsconfig.json";
324
+ if (!cache[cacheKey]) {
325
+ cache[cacheKey] = ts__default["default"].createModuleResolutionCache(baseUrl, function (x) {
326
+ return x;
327
+ }, jsConfig.compilerOptions);
328
+ }
329
+ }
330
+
331
+ // Function to resolve import paths
332
+ var resolvePath = function resolvePath(/** @type {string} */importPath) {
333
+ var _resolvedModule$resol;
334
+ // Attempt to resolve using TypeScript path mapping
335
+ var resolvedModule = ts__default["default"].resolveModuleName(importPath, path__default["default"].join(baseUrl, "val.modules.ts"), {
336
+ baseUrl: baseUrl,
337
+ paths: paths
338
+ }, ts__default["default"].sys, cache[cacheKey]);
339
+ var resolved = (_resolvedModule$resol = resolvedModule.resolvedModule) === null || _resolvedModule$resol === void 0 ? void 0 : _resolvedModule$resol.resolvedFileName;
340
+ if (resolved) {
341
+ return path__default["default"].resolve(resolved);
342
+ }
343
+ // Fallback to Node.js resolution
344
+ try {
345
+ return require.resolve(importPath, {
346
+ paths: [projectDir]
347
+ });
348
+ } catch (_unused) {
349
+ return undefined;
350
+ }
351
+ };
352
+ if (!fs__default["default"].existsSync(modulesFilePath)) {
353
+ return {};
354
+ }
355
+ var modulesFileContent = fs__default["default"].readFileSync(modulesFilePath, "utf-8");
356
+ var referencedFiles = Array.from(modulesFileContent.matchAll(/import\(["'](.+\.val(?:\.[jt]s)?)['"]\)/g)).map(function (match) {
357
+ return resolvePath(match[1]);
358
+ }).filter(function (file) {
359
+ return file !== undefined;
360
+ });
361
+ return {
362
+ ExportDefaultDeclaration: function ExportDefaultDeclaration(node) {
363
+ var filename = context.filename || context.getFilename();
364
+ if (filename !== null && filename !== void 0 && filename.includes(".val")) {
365
+ if (node.declaration && node.declaration.type === "CallExpression" && node.declaration.callee.type === "MemberExpression" && node.declaration.callee.object.type === "Identifier" && node.declaration.callee.object.name === "c" && node.declaration.callee.property.type === "Identifier" && node.declaration.callee.property.name === "define" && node.declaration.arguments && node.declaration.arguments.length > 0) {
366
+ if (!referencedFiles.includes(filename)) {
367
+ context.report({
368
+ node: node,
369
+ messageId: "missingFile",
370
+ data: {
371
+ file: filename.replace(projectDir, "")
372
+ }
373
+ });
374
+ }
375
+ }
376
+ }
377
+ }
378
+ };
379
+ }
380
+ };
381
+
382
+ // @ts-check
383
+
278
384
  /**
279
385
  * @type {Plugin["rules"]}
280
386
  */
@@ -283,7 +389,8 @@ var rules = {
283
389
  "no-illegal-imports": noIllegalImports,
284
390
  "export-content-must-be-valid": exportContentMustBeValid,
285
391
  "no-define-with-variable": noDefineWithVariable,
286
- "default-export-val-module": defaultExportValModule
392
+ "default-export-val-module": defaultExportValModule,
393
+ "module-in-val-modules": moduleInValModules
287
394
  };
288
395
 
289
396
  /**
@@ -302,7 +409,8 @@ var configs = {
302
409
  "@valbuild/no-illegal-imports": "error",
303
410
  "@valbuild/export-content-must-be-valid": "error",
304
411
  "@valbuild/no-define-with-variable": "error",
305
- "@valbuild/default-export-val-module": "error"
412
+ "@valbuild/default-export-val-module": "error",
413
+ "@valbuild/module-in-val-modules": "error"
306
414
  }
307
415
  }
308
416
  };
@@ -4,11 +4,13 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var path = require('path');
6
6
  var fs = require('fs');
7
+ var ts = require('typescript');
7
8
 
8
9
  function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
9
10
 
10
11
  var path__default = /*#__PURE__*/_interopDefault(path);
11
12
  var fs__default = /*#__PURE__*/_interopDefault(fs);
13
+ var ts__default = /*#__PURE__*/_interopDefault(ts);
12
14
 
13
15
  // @ts-check
14
16
 
@@ -275,6 +277,110 @@ var defaultExportValModule = {
275
277
 
276
278
  // @ts-check
277
279
 
280
+ /** @type {Record<string, ts.ModuleResolutionCache>} */
281
+ var cache = {};
282
+ /**
283
+ * @type {import('eslint').Rule.RuleModule}
284
+ */
285
+ var moduleInValModules = {
286
+ meta: {
287
+ type: "problem",
288
+ docs: {
289
+ description: "Ensure all .val files with export default c.define are referenced in val.modules",
290
+ recommended: true
291
+ },
292
+ schema: [],
293
+ // No options needed
294
+ messages: {
295
+ missingFile: "'{{file}}' contains export default c.define but is not referenced in val.modules.ts or val.module.js."
296
+ }
297
+ },
298
+ create: function create(context) {
299
+ var projectDir = path__default["default"].resolve(context.cwd || context.getCwd());
300
+ var modulesFilePath = path__default["default"].join(projectDir, "val.modules.ts");
301
+ var baseUrl = projectDir;
302
+ /** @type { {[key: string]: string[];} } */
303
+ var paths = {};
304
+ var cacheKey = baseUrl;
305
+ var tsConfigPath = ts__default["default"].findConfigFile(projectDir, ts__default["default"].sys.fileExists, "tsconfig.json");
306
+ var jsConfigPath = ts__default["default"].findConfigFile(projectDir, ts__default["default"].sys.fileExists, "jsconfig.json");
307
+ if (tsConfigPath) {
308
+ var _tsConfig$compilerOpt, _tsConfig$compilerOpt2;
309
+ var tsConfig = tsConfigPath ? JSON.parse(fs__default["default"].readFileSync(tsConfigPath, "utf-8")) : {};
310
+ baseUrl = (_tsConfig$compilerOpt = tsConfig.compilerOptions) !== null && _tsConfig$compilerOpt !== void 0 && _tsConfig$compilerOpt.baseUrl ? path__default["default"].resolve(projectDir, tsConfig.compilerOptions.baseUrl) : projectDir;
311
+ paths = ((_tsConfig$compilerOpt2 = tsConfig.compilerOptions) === null || _tsConfig$compilerOpt2 === void 0 ? void 0 : _tsConfig$compilerOpt2.paths) || {};
312
+ cacheKey = baseUrl + "/tsconfig.json";
313
+ if (!cache[cacheKey]) {
314
+ cache[cacheKey] = ts__default["default"].createModuleResolutionCache(baseUrl, function (x) {
315
+ return x;
316
+ }, tsConfig.compilerOptions);
317
+ }
318
+ } else if (jsConfigPath) {
319
+ var _jsConfig$compilerOpt, _jsConfig$compilerOpt2;
320
+ var jsConfig = jsConfigPath ? JSON.parse(fs__default["default"].readFileSync(jsConfigPath, "utf-8")) : {};
321
+ baseUrl = (_jsConfig$compilerOpt = jsConfig.compilerOptions) !== null && _jsConfig$compilerOpt !== void 0 && _jsConfig$compilerOpt.baseUrl ? path__default["default"].resolve(projectDir, jsConfig.compilerOptions.baseUrl) : projectDir;
322
+ paths = ((_jsConfig$compilerOpt2 = jsConfig.compilerOptions) === null || _jsConfig$compilerOpt2 === void 0 ? void 0 : _jsConfig$compilerOpt2.paths) || {};
323
+ cacheKey = baseUrl + "/jsconfig.json";
324
+ if (!cache[cacheKey]) {
325
+ cache[cacheKey] = ts__default["default"].createModuleResolutionCache(baseUrl, function (x) {
326
+ return x;
327
+ }, jsConfig.compilerOptions);
328
+ }
329
+ }
330
+
331
+ // Function to resolve import paths
332
+ var resolvePath = function resolvePath(/** @type {string} */importPath) {
333
+ var _resolvedModule$resol;
334
+ // Attempt to resolve using TypeScript path mapping
335
+ var resolvedModule = ts__default["default"].resolveModuleName(importPath, path__default["default"].join(baseUrl, "val.modules.ts"), {
336
+ baseUrl: baseUrl,
337
+ paths: paths
338
+ }, ts__default["default"].sys, cache[cacheKey]);
339
+ var resolved = (_resolvedModule$resol = resolvedModule.resolvedModule) === null || _resolvedModule$resol === void 0 ? void 0 : _resolvedModule$resol.resolvedFileName;
340
+ if (resolved) {
341
+ return path__default["default"].resolve(resolved);
342
+ }
343
+ // Fallback to Node.js resolution
344
+ try {
345
+ return require.resolve(importPath, {
346
+ paths: [projectDir]
347
+ });
348
+ } catch (_unused) {
349
+ return undefined;
350
+ }
351
+ };
352
+ if (!fs__default["default"].existsSync(modulesFilePath)) {
353
+ return {};
354
+ }
355
+ var modulesFileContent = fs__default["default"].readFileSync(modulesFilePath, "utf-8");
356
+ var referencedFiles = Array.from(modulesFileContent.matchAll(/import\(["'](.+\.val(?:\.[jt]s)?)['"]\)/g)).map(function (match) {
357
+ return resolvePath(match[1]);
358
+ }).filter(function (file) {
359
+ return file !== undefined;
360
+ });
361
+ return {
362
+ ExportDefaultDeclaration: function ExportDefaultDeclaration(node) {
363
+ var filename = context.filename || context.getFilename();
364
+ if (filename !== null && filename !== void 0 && filename.includes(".val")) {
365
+ if (node.declaration && node.declaration.type === "CallExpression" && node.declaration.callee.type === "MemberExpression" && node.declaration.callee.object.type === "Identifier" && node.declaration.callee.object.name === "c" && node.declaration.callee.property.type === "Identifier" && node.declaration.callee.property.name === "define" && node.declaration.arguments && node.declaration.arguments.length > 0) {
366
+ if (!referencedFiles.includes(filename)) {
367
+ context.report({
368
+ node: node,
369
+ messageId: "missingFile",
370
+ data: {
371
+ file: filename.replace(projectDir, "")
372
+ }
373
+ });
374
+ }
375
+ }
376
+ }
377
+ }
378
+ };
379
+ }
380
+ };
381
+
382
+ // @ts-check
383
+
278
384
  /**
279
385
  * @type {Plugin["rules"]}
280
386
  */
@@ -283,7 +389,8 @@ var rules = {
283
389
  "no-illegal-imports": noIllegalImports,
284
390
  "export-content-must-be-valid": exportContentMustBeValid,
285
391
  "no-define-with-variable": noDefineWithVariable,
286
- "default-export-val-module": defaultExportValModule
392
+ "default-export-val-module": defaultExportValModule,
393
+ "module-in-val-modules": moduleInValModules
287
394
  };
288
395
 
289
396
  /**
@@ -302,7 +409,8 @@ var configs = {
302
409
  "@valbuild/no-illegal-imports": "error",
303
410
  "@valbuild/export-content-must-be-valid": "error",
304
411
  "@valbuild/no-define-with-variable": "error",
305
- "@valbuild/default-export-val-module": "error"
412
+ "@valbuild/default-export-val-module": "error",
413
+ "@valbuild/module-in-val-modules": "error"
306
414
  }
307
415
  }
308
416
  };
@@ -1,5 +1,6 @@
1
1
  import path from 'path';
2
2
  import fs from 'fs';
3
+ import ts from 'typescript';
3
4
 
4
5
  // @ts-check
5
6
 
@@ -266,6 +267,110 @@ var defaultExportValModule = {
266
267
 
267
268
  // @ts-check
268
269
 
270
+ /** @type {Record<string, ts.ModuleResolutionCache>} */
271
+ var cache = {};
272
+ /**
273
+ * @type {import('eslint').Rule.RuleModule}
274
+ */
275
+ var moduleInValModules = {
276
+ meta: {
277
+ type: "problem",
278
+ docs: {
279
+ description: "Ensure all .val files with export default c.define are referenced in val.modules",
280
+ recommended: true
281
+ },
282
+ schema: [],
283
+ // No options needed
284
+ messages: {
285
+ missingFile: "'{{file}}' contains export default c.define but is not referenced in val.modules.ts or val.module.js."
286
+ }
287
+ },
288
+ create: function create(context) {
289
+ var projectDir = path.resolve(context.cwd || context.getCwd());
290
+ var modulesFilePath = path.join(projectDir, "val.modules.ts");
291
+ var baseUrl = projectDir;
292
+ /** @type { {[key: string]: string[];} } */
293
+ var paths = {};
294
+ var cacheKey = baseUrl;
295
+ var tsConfigPath = ts.findConfigFile(projectDir, ts.sys.fileExists, "tsconfig.json");
296
+ var jsConfigPath = ts.findConfigFile(projectDir, ts.sys.fileExists, "jsconfig.json");
297
+ if (tsConfigPath) {
298
+ var _tsConfig$compilerOpt, _tsConfig$compilerOpt2;
299
+ var tsConfig = tsConfigPath ? JSON.parse(fs.readFileSync(tsConfigPath, "utf-8")) : {};
300
+ baseUrl = (_tsConfig$compilerOpt = tsConfig.compilerOptions) !== null && _tsConfig$compilerOpt !== void 0 && _tsConfig$compilerOpt.baseUrl ? path.resolve(projectDir, tsConfig.compilerOptions.baseUrl) : projectDir;
301
+ paths = ((_tsConfig$compilerOpt2 = tsConfig.compilerOptions) === null || _tsConfig$compilerOpt2 === void 0 ? void 0 : _tsConfig$compilerOpt2.paths) || {};
302
+ cacheKey = baseUrl + "/tsconfig.json";
303
+ if (!cache[cacheKey]) {
304
+ cache[cacheKey] = ts.createModuleResolutionCache(baseUrl, function (x) {
305
+ return x;
306
+ }, tsConfig.compilerOptions);
307
+ }
308
+ } else if (jsConfigPath) {
309
+ var _jsConfig$compilerOpt, _jsConfig$compilerOpt2;
310
+ var jsConfig = jsConfigPath ? JSON.parse(fs.readFileSync(jsConfigPath, "utf-8")) : {};
311
+ baseUrl = (_jsConfig$compilerOpt = jsConfig.compilerOptions) !== null && _jsConfig$compilerOpt !== void 0 && _jsConfig$compilerOpt.baseUrl ? path.resolve(projectDir, jsConfig.compilerOptions.baseUrl) : projectDir;
312
+ paths = ((_jsConfig$compilerOpt2 = jsConfig.compilerOptions) === null || _jsConfig$compilerOpt2 === void 0 ? void 0 : _jsConfig$compilerOpt2.paths) || {};
313
+ cacheKey = baseUrl + "/jsconfig.json";
314
+ if (!cache[cacheKey]) {
315
+ cache[cacheKey] = ts.createModuleResolutionCache(baseUrl, function (x) {
316
+ return x;
317
+ }, jsConfig.compilerOptions);
318
+ }
319
+ }
320
+
321
+ // Function to resolve import paths
322
+ var resolvePath = function resolvePath(/** @type {string} */importPath) {
323
+ var _resolvedModule$resol;
324
+ // Attempt to resolve using TypeScript path mapping
325
+ var resolvedModule = ts.resolveModuleName(importPath, path.join(baseUrl, "val.modules.ts"), {
326
+ baseUrl: baseUrl,
327
+ paths: paths
328
+ }, ts.sys, cache[cacheKey]);
329
+ var resolved = (_resolvedModule$resol = resolvedModule.resolvedModule) === null || _resolvedModule$resol === void 0 ? void 0 : _resolvedModule$resol.resolvedFileName;
330
+ if (resolved) {
331
+ return path.resolve(resolved);
332
+ }
333
+ // Fallback to Node.js resolution
334
+ try {
335
+ return require.resolve(importPath, {
336
+ paths: [projectDir]
337
+ });
338
+ } catch (_unused) {
339
+ return undefined;
340
+ }
341
+ };
342
+ if (!fs.existsSync(modulesFilePath)) {
343
+ return {};
344
+ }
345
+ var modulesFileContent = fs.readFileSync(modulesFilePath, "utf-8");
346
+ var referencedFiles = Array.from(modulesFileContent.matchAll(/import\(["'](.+\.val(?:\.[jt]s)?)['"]\)/g)).map(function (match) {
347
+ return resolvePath(match[1]);
348
+ }).filter(function (file) {
349
+ return file !== undefined;
350
+ });
351
+ return {
352
+ ExportDefaultDeclaration: function ExportDefaultDeclaration(node) {
353
+ var filename = context.filename || context.getFilename();
354
+ if (filename !== null && filename !== void 0 && filename.includes(".val")) {
355
+ if (node.declaration && node.declaration.type === "CallExpression" && node.declaration.callee.type === "MemberExpression" && node.declaration.callee.object.type === "Identifier" && node.declaration.callee.object.name === "c" && node.declaration.callee.property.type === "Identifier" && node.declaration.callee.property.name === "define" && node.declaration.arguments && node.declaration.arguments.length > 0) {
356
+ if (!referencedFiles.includes(filename)) {
357
+ context.report({
358
+ node: node,
359
+ messageId: "missingFile",
360
+ data: {
361
+ file: filename.replace(projectDir, "")
362
+ }
363
+ });
364
+ }
365
+ }
366
+ }
367
+ }
368
+ };
369
+ }
370
+ };
371
+
372
+ // @ts-check
373
+
269
374
  /**
270
375
  * @type {Plugin["rules"]}
271
376
  */
@@ -274,7 +379,8 @@ var rules = {
274
379
  "no-illegal-imports": noIllegalImports,
275
380
  "export-content-must-be-valid": exportContentMustBeValid,
276
381
  "no-define-with-variable": noDefineWithVariable,
277
- "default-export-val-module": defaultExportValModule
382
+ "default-export-val-module": defaultExportValModule,
383
+ "module-in-val-modules": moduleInValModules
278
384
  };
279
385
 
280
386
  /**
@@ -293,7 +399,8 @@ var configs = {
293
399
  "@valbuild/no-illegal-imports": "error",
294
400
  "@valbuild/export-content-must-be-valid": "error",
295
401
  "@valbuild/no-define-with-variable": "error",
296
- "@valbuild/default-export-val-module": "error"
402
+ "@valbuild/default-export-val-module": "error",
403
+ "@valbuild/module-in-val-modules": "error"
297
404
  }
298
405
  }
299
406
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@valbuild/eslint-plugin",
3
- "version": "0.67.0",
3
+ "version": "0.67.2",
4
4
  "description": "ESLint rules for val",
5
5
  "keywords": [
6
6
  "eslint",
@@ -23,7 +23,8 @@
23
23
  },
24
24
  "license": "MIT",
25
25
  "peerDependencies": {
26
- "eslint": "6 || 7 || 8"
26
+ "eslint": "6 || 7 || 8",
27
+ "typescript": "5"
27
28
  },
28
29
  "devDependencies": {
29
30
  "@types/jest": "^29.5.11",
package/src/index.js CHANGED
@@ -11,6 +11,7 @@ import noIllegalImports from "./rules/noIllegalImports";
11
11
  import exportContentMustBeValid from "./rules/exportContentMustBeValid";
12
12
  import noDefineWithVariable from "./rules/noDefineWithVariable";
13
13
  import defaultExportValModule from "./rules/defaultExportValModule";
14
+ import moduleInValModules from "./rules/moduleInValModules";
14
15
 
15
16
  /**
16
17
  * @type {Plugin["rules"]}
@@ -21,6 +22,7 @@ export const rules = {
21
22
  "export-content-must-be-valid": exportContentMustBeValid,
22
23
  "no-define-with-variable": noDefineWithVariable,
23
24
  "default-export-val-module": defaultExportValModule,
25
+ "module-in-val-modules": moduleInValModules,
24
26
  };
25
27
 
26
28
  /**
@@ -40,6 +42,7 @@ export const configs = {
40
42
  "@valbuild/export-content-must-be-valid": "error",
41
43
  "@valbuild/no-define-with-variable": "error",
42
44
  "@valbuild/default-export-val-module": "error",
45
+ "@valbuild/module-in-val-modules": "error",
43
46
  },
44
47
  },
45
48
  };
@@ -0,0 +1,146 @@
1
+ // @ts-check
2
+ import fs from "fs";
3
+ import path from "path";
4
+ import ts from "typescript";
5
+
6
+ /** @type {Record<string, ts.ModuleResolutionCache>} */
7
+ const cache = {};
8
+ /**
9
+ * @type {import('eslint').Rule.RuleModule}
10
+ */
11
+ export default {
12
+ meta: {
13
+ type: "problem",
14
+ docs: {
15
+ description:
16
+ "Ensure all .val files with export default c.define are referenced in val.modules",
17
+ recommended: true,
18
+ },
19
+ schema: [], // No options needed
20
+ messages: {
21
+ missingFile:
22
+ "'{{file}}' contains export default c.define but is not referenced in val.modules.ts or val.module.js.",
23
+ },
24
+ },
25
+ create(context) {
26
+ const projectDir = path.resolve(context.cwd || context.getCwd());
27
+ const modulesFilePath = path.join(projectDir, "val.modules.ts");
28
+
29
+ let baseUrl = projectDir;
30
+ /** @type { {[key: string]: string[];} } */
31
+ let paths = {};
32
+ let cacheKey = baseUrl;
33
+
34
+ const tsConfigPath = ts.findConfigFile(
35
+ projectDir,
36
+ ts.sys.fileExists,
37
+ "tsconfig.json",
38
+ );
39
+ const jsConfigPath = ts.findConfigFile(
40
+ projectDir,
41
+ ts.sys.fileExists,
42
+ "jsconfig.json",
43
+ );
44
+
45
+ if (tsConfigPath) {
46
+ const tsConfig = tsConfigPath
47
+ ? JSON.parse(fs.readFileSync(tsConfigPath, "utf-8"))
48
+ : {};
49
+
50
+ baseUrl = tsConfig.compilerOptions?.baseUrl
51
+ ? path.resolve(projectDir, tsConfig.compilerOptions.baseUrl)
52
+ : projectDir;
53
+ paths = tsConfig.compilerOptions?.paths || {};
54
+ cacheKey = baseUrl + "/tsconfig.json";
55
+ if (!cache[cacheKey]) {
56
+ cache[cacheKey] = ts.createModuleResolutionCache(
57
+ baseUrl,
58
+ (x) => x,
59
+ tsConfig.compilerOptions,
60
+ );
61
+ }
62
+ } else if (jsConfigPath) {
63
+ const jsConfig = jsConfigPath
64
+ ? JSON.parse(fs.readFileSync(jsConfigPath, "utf-8"))
65
+ : {};
66
+
67
+ baseUrl = jsConfig.compilerOptions?.baseUrl
68
+ ? path.resolve(projectDir, jsConfig.compilerOptions.baseUrl)
69
+ : projectDir;
70
+ paths = jsConfig.compilerOptions?.paths || {};
71
+ cacheKey = baseUrl + "/jsconfig.json";
72
+ if (!cache[cacheKey]) {
73
+ cache[cacheKey] = ts.createModuleResolutionCache(
74
+ baseUrl,
75
+ (x) => x,
76
+ jsConfig.compilerOptions,
77
+ );
78
+ }
79
+ }
80
+
81
+ // Function to resolve import paths
82
+ const resolvePath = (/** @type {string} */ importPath) => {
83
+ // Attempt to resolve using TypeScript path mapping
84
+ const resolvedModule = ts.resolveModuleName(
85
+ importPath,
86
+ path.join(baseUrl, "val.modules.ts"),
87
+ {
88
+ baseUrl,
89
+ paths,
90
+ },
91
+ ts.sys,
92
+ cache[cacheKey],
93
+ );
94
+ const resolved = resolvedModule.resolvedModule?.resolvedFileName;
95
+ if (resolved) {
96
+ return path.resolve(resolved);
97
+ }
98
+ // Fallback to Node.js resolution
99
+ try {
100
+ return require.resolve(importPath, { paths: [projectDir] });
101
+ } catch {
102
+ return undefined;
103
+ }
104
+ };
105
+
106
+ if (!fs.existsSync(modulesFilePath)) {
107
+ return {};
108
+ }
109
+
110
+ const modulesFileContent = fs.readFileSync(modulesFilePath, "utf-8");
111
+ const referencedFiles = Array.from(
112
+ modulesFileContent.matchAll(/import\(["'](.+\.val(?:\.[jt]s)?)['"]\)/g),
113
+ )
114
+ .map((match) => {
115
+ return resolvePath(match[1]);
116
+ })
117
+ .filter((file) => file !== undefined);
118
+
119
+ return {
120
+ ExportDefaultDeclaration(node) {
121
+ const filename = context.filename || context.getFilename();
122
+ if (filename?.includes(".val")) {
123
+ if (
124
+ node.declaration &&
125
+ node.declaration.type === "CallExpression" &&
126
+ node.declaration.callee.type === "MemberExpression" &&
127
+ node.declaration.callee.object.type === "Identifier" &&
128
+ node.declaration.callee.object.name === "c" &&
129
+ node.declaration.callee.property.type === "Identifier" &&
130
+ node.declaration.callee.property.name === "define" &&
131
+ node.declaration.arguments &&
132
+ node.declaration.arguments.length > 0
133
+ ) {
134
+ if (!referencedFiles.includes(filename)) {
135
+ context.report({
136
+ node,
137
+ messageId: "missingFile",
138
+ data: { file: filename.replace(projectDir, "") },
139
+ });
140
+ }
141
+ }
142
+ }
143
+ },
144
+ };
145
+ },
146
+ };