eslint-plugin-use-agnostic 1.2.0 → 1.2.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.
@@ -12,9 +12,7 @@ import { directive21Comments } from "./jscomments/directive21/constants/bases.js
12
12
 
13
13
  const config = Object.freeze({
14
14
  jsDoc: jSDocComments,
15
- // ...jSDocComments,
16
15
  tests: testsComments,
17
- // ...testsComments,
18
16
  [agnostic20ConfigName]: agnostic20Comments,
19
17
  [directive21ConfigName]: directive21Comments,
20
18
  });
@@ -22,7 +20,7 @@ const config = Object.freeze({
22
20
  export default config;
23
21
 
24
22
  /* Notes
25
- comment: "comment", // $COMMENT#COMMENT in code, "comment" on hover when resolved by the VSCode extension JSComments
23
+ comment: "comment", // $COMMENT#COMMENT in code, "comment" on hover when resolved by the VSCode extension JSComments (Comment Variables)
26
24
  Aims of the VSCode extension JSComments:
27
25
  - automatically capitalizes keys, so here:
28
26
  - config.comment would return $COMMENT#COMMENT
@@ -30,4 +28,5 @@ Aims of the VSCode extension JSComments:
30
28
  - config["agnostic20"] would return $COMMENT#AGNOSTIC20#*
31
29
  - config["directive21"] would return $COMMENT#DIRECTIVE21#*
32
30
  - this way, instead doing a find-replace on a big documentation comment, the comment stays within the config and only the config placeholder is found and replaced for the same results
31
+ A working CLI is already on npm: comment-variables.
33
32
  */
@@ -1,12 +1,12 @@
1
1
  // comment variables for JSDoc definitions
2
2
  export const jSDocComments = Object.freeze({
3
3
  // library/_commons/utilities/helpers.js
4
- basePath: "The absolute import path with extension yet resolved.",
4
+ basePath: "The absolute import path with its extension yet resolved.",
5
5
  currentDir:
6
- "The directory of the file containing the import (from `path.dirname(context.filename)`).",
6
+ "The directory of the file containing the import, such as from `path.dirname(context.filename)`.",
7
7
  importPath:
8
- "The import specifier (e.g., `@/components/Button` or `./utils`), from the current node.",
9
- cwd: "The project root (from `context.cwd`).",
8
+ "The import specifier (e.g., `@/components/Button` or `./utils`), such as one from the current node.",
9
+ cwd: "The project root, such as from `context.cwd`.",
10
10
  fileResolvedPath: "The resolved path of the file.",
11
11
  aContext: "An ESLint rule's `context` object.",
12
12
  resolvedDirectives_blockedImports: `The blocked imports object, either for agnostic20 or for directive21.`,
@@ -1,14 +1,6 @@
1
- import fs from "fs";
2
- import path from "path";
3
-
4
- import { Linter } from "eslint";
5
- import { loadConfig, createMatchPath } from "tsconfig-paths";
6
-
7
1
  import {
8
- EXTENSIONS,
9
2
  ARE_NOT_ALLOWED_TO_IMPORT,
10
3
  resolvedDirectives_resolvedModules,
11
- typeScriptAndJSXCompatible,
12
4
  } from "../constants/bases.js";
13
5
 
14
6
  /**
@@ -22,86 +14,6 @@ import {
22
14
  * @typedef {import('../../../types/_commons/typedefs').ResolvedDirectives_BlockedImports<T, U>} ResolvedDirectives_BlockedImports
23
15
  */
24
16
 
25
- /* resolveImportPath */
26
-
27
- /**
28
- * Finds the existing path of an import that does not have an extension specified.
29
- * @param {string} basePath The absolute import path with extension yet resolved.
30
- * @returns The absolute path with its extension or `null` if no path is found.
31
- */
32
- const findExistingPath = (basePath) => {
33
- for (const ext of EXTENSIONS) {
34
- const fullPath = `${basePath}${ext}`;
35
- if (fs.existsSync(fullPath)) return fullPath;
36
- }
37
- return null;
38
- };
39
-
40
- /**
41
- * Resolves an import path to a filesystem path, handling:
42
- * - Base url and aliases (via tsconfig.json `baseUrl` and `paths` compiler options)
43
- * - Missing extensions (appends `.ts`, `.tsx`, etc.)
44
- * - Directory imports (e.g., `./components` → `./components/index.ts`)
45
- * @param {string} currentDir The directory of the file containing the import (from `path.dirname(context.filename)`).
46
- * @param {string} importPath The import specifier (e.g., `@/components/Button` or `./utils`), from the current node.
47
- * @param {string} cwd The project root (from `context.cwd`).
48
- * @returns The absolute resolved path or `null` if no path is found.
49
- */
50
- export const resolveImportPath = (currentDir, importPath, cwd) => {
51
- // Step 1: Resolves baseUrl and aliases
52
- const config = loadConfig(cwd);
53
-
54
- const resolveTSConfig =
55
- config.resultType === "success"
56
- ? createMatchPath(config.absoluteBaseUrl, config.paths)
57
- : null;
58
-
59
- const baseUrlOrAliasedPath = resolveTSConfig
60
- ? resolveTSConfig(importPath, undefined, undefined, EXTENSIONS)
61
- : null;
62
-
63
- // Step 2: Resolves relative/absolute paths
64
- const basePath = baseUrlOrAliasedPath || path.resolve(currentDir, importPath);
65
-
66
- // does not resolve on node_modules
67
- if (basePath.includes("node_modules")) return null;
68
-
69
- // Case 1: File with extension exists
70
- if (path.extname(importPath) && fs.existsSync(basePath)) return basePath;
71
-
72
- // Case 2: Tries appending extensions
73
- const extensionlessImportPath = findExistingPath(basePath);
74
- if (extensionlessImportPath) return extensionlessImportPath;
75
-
76
- // Case 3: Directory import (e.g., `./components` → `./components/index.ts`)
77
- const indexPath = path.join(basePath, "index");
78
- const directoryImportPath = findExistingPath(indexPath);
79
- if (directoryImportPath) return directoryImportPath;
80
-
81
- return null; // not found
82
- };
83
-
84
- /* getSourceCodeFromFilePath */
85
-
86
- /**
87
- * Gets the ESLint-generated SourceCode object of a file from its resolved path.
88
- * @param {string} resolvedPath The resolved path of the file.
89
- * @returns The ESLint-generated SourceCode object of the file.
90
- */
91
- export const getSourceCodeFromFilePath = (resolvedPath) => {
92
- // ensures each instance of the function is based on its own linter
93
- // (just in case somehow some linters were running concurrently)
94
- const linter = new Linter();
95
- // the raw code of the file at the end of the resolved path
96
- const text = fs.readFileSync(resolvedPath, "utf8");
97
- // utilizes linter.verify ...
98
- linter.verify(text, { languageOptions: typeScriptAndJSXCompatible });
99
- // ... to retrieve the raw code as a SourceCode object
100
- const code = linter.getSourceCode();
101
-
102
- return code;
103
- };
104
-
105
17
  /* highlightFirstLineOfCode */
106
18
 
107
19
  /**
@@ -1,5 +1,7 @@
1
1
  import path from "path";
2
2
 
3
+ import { resolveImportingPath } from "resolve-importing-path";
4
+
3
5
  import {
4
6
  EXTENSIONS,
5
7
  useServerJSXMessageId,
@@ -16,10 +18,7 @@ import {
16
18
  specificViolationMessage,
17
19
  } from "../constants/bases.js";
18
20
 
19
- import {
20
- resolveImportPath,
21
- highlightFirstLineOfCode,
22
- } from "../../../_commons/utilities/helpers.js";
21
+ import { highlightFirstLineOfCode } from "../../../_commons/utilities/helpers.js";
23
22
  import {
24
23
  getDirectiveFromCurrentModule,
25
24
  getDirectiveFromImportedModule,
@@ -104,7 +103,7 @@ const importedFileFlow = (context, node) => {
104
103
  const skipTrue = { ...skip, importedFileEffectiveDirective: undefined };
105
104
 
106
105
  // finds the full path of the import
107
- const resolvedImportPath = resolveImportPath(
106
+ const resolvedImportPath = resolveImportingPath(
108
107
  path.dirname(context.filename),
109
108
  node.source.value,
110
109
  context.cwd
@@ -1,3 +1,5 @@
1
+ import { getSourceCodeFromFilePath } from "get-sourcecode-from-file-path";
2
+
1
3
  import {
2
4
  USE_SERVER,
3
5
  LOGICS,
@@ -12,7 +14,6 @@ import {
12
14
  isImportBlocked as commonsIsImportBlocked,
13
15
  makeMessageFromCurrentFileResolvedDirective,
14
16
  findSpecificViolationMessage as commonsFindSpecificViolationMessage,
15
- getSourceCodeFromFilePath,
16
17
  } from "../../../_commons/utilities/helpers.js";
17
18
 
18
19
  /**
@@ -1,5 +1,7 @@
1
1
  import path from "path";
2
2
 
3
+ import { resolveImportingPath } from "resolve-importing-path";
4
+
3
5
  import {
4
6
  EXTENSIONS,
5
7
  reExportNotSameMessageId,
@@ -19,10 +21,7 @@ import {
19
21
  specificFailure,
20
22
  } from "../constants/bases.js";
21
23
 
22
- import {
23
- resolveImportPath,
24
- highlightFirstLineOfCode,
25
- } from "../../../_commons/utilities/helpers.js";
24
+ import { highlightFirstLineOfCode } from "../../../_commons/utilities/helpers.js";
26
25
  import {
27
26
  getCommentedDirectiveFromCurrentModule,
28
27
  getVerifiedCommentedDirective,
@@ -113,7 +112,7 @@ const importedFileFlow = (context, node) => {
113
112
  const skipTrue = { ...skip, importedFileCommentedDirective: undefined };
114
113
 
115
114
  // finds the full path of the import
116
- const resolvedImportPath = resolveImportPath(
115
+ const resolvedImportPath = resolveImportingPath(
117
116
  path.dirname(context.filename),
118
117
  node.source.value,
119
118
  context.cwd
@@ -1,3 +1,5 @@
1
+ import { getSourceCodeFromFilePath } from "get-sourcecode-from-file-path";
2
+
1
3
  import { exportNotStrategized } from "../../../_commons/constants/bases.js";
2
4
  import {
3
5
  USE_AGNOSTIC_STRATEGIES,
@@ -12,7 +14,6 @@ import {
12
14
  isImportBlocked as commonsIsImportBlocked,
13
15
  makeMessageFromCurrentFileResolvedDirective,
14
16
  findSpecificViolationMessage as commonsFindSpecificViolationMessage,
15
- getSourceCodeFromFilePath,
16
17
  } from "../../../_commons/utilities/helpers.js";
17
18
 
18
19
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-use-agnostic",
3
- "version": "1.2.0",
3
+ "version": "1.2.2",
4
4
  "description": "Highlights problematic server-client imports in projects made with the Fullstack React Architecture.",
5
5
  "keywords": [
6
6
  "eslint",
@@ -41,6 +41,8 @@
41
41
  "test": "node tests/agnostic20/import-rules.test.js && node tests/directive21/import-rules.test.js"
42
42
  },
43
43
  "dependencies": {
44
+ "get-sourcecode-from-file-path": "^1.0.0",
45
+ "resolve-importing-path": "^1.0.2",
44
46
  "tsconfig-paths": "^4.2.0",
45
47
  "typescript-eslint": "^8.32.0"
46
48
  },