eslint-plugin-use-agnostic 0.9.6 → 0.9.7

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.
@@ -1,10 +1,6 @@
1
1
  import {
2
- reExportNotSameMessageId,
3
- importBreaksCommentedImportRulesMessageId,
4
- noCommentedDirective,
5
- commentedDirectiveVerificationFailed,
6
- importNotStrategized,
7
2
  exportNotStrategized,
3
+ commentedDirectives_commentedModules,
8
4
  } from "../../../_commons/constants/bases.js";
9
5
  import {
10
6
  USE_SERVER_LOGICS,
@@ -20,9 +16,8 @@ import {
20
16
  directivesArray,
21
17
  strategiesArray,
22
18
  commentedDirectives_4RawImplementations,
23
- commentedStrategies_CommentedDirectives,
24
- commentedDirectives_BlockedImports,
25
- commentedDirectives_CommentedModules,
19
+ commentedStrategies_commentedDirectives,
20
+ commentedDirectives_blockedImports,
26
21
  } from "../constants/bases.js";
27
22
 
28
23
  import {
@@ -32,6 +27,17 @@ import {
32
27
  findSpecificViolationMessage as commonsFindSpecificViolationMessage,
33
28
  } from "../../../_commons/utilities/helpers.js";
34
29
 
30
+ /**
31
+ * @typedef {import('../../../../types/directive21/_commons/typedefs.js').Context} Context
32
+ * @typedef {import('../../../../types/directive21/_commons/typedefs.js').CommentedDirective} CommentedDirective
33
+ * @typedef {import('../../../../types/directive21/_commons/typedefs.js').CommentedDirectiveWithoutUseAgnosticStrategies} CommentedDirectiveWithoutUseAgnosticStrategies
34
+ * @typedef {import('../../../../types/directive21/_commons/typedefs.js').Extension} Extension
35
+ * @typedef {import('../../../../types/directive21/_commons/typedefs.js').ImportDeclaration} ImportDeclaration
36
+ * @typedef {import('../../../../types/directive21/_commons/typedefs.js').ExportNamedDeclaration} ExportNamedDeclaration
37
+ * @typedef {import('../../../../types/directive21/_commons/typedefs.js').ExportAllDeclaration} ExportAllDeclaration
38
+ * @typedef {import('../../../../types/directive21/_commons/typedefs.js').ExportDefaultDeclaration} ExportDefaultDeclaration
39
+ */
40
+
35
41
  /* getCommentedDirectiveFromCurrentModule */
36
42
 
37
43
  /**
@@ -88,7 +94,7 @@ const stripDoubleQuotes = (string) => {
88
94
  * - `'use client contexts'`, `"use client contexts"` denoting a Client Contexts Module.
89
95
  * - `'use agnostic conditions'`, `"use agnostic conditions"` denoting an Agnostic Conditions Module.
90
96
  * - `'use agnostic strategies'`, `"use agnostic strategies"` denoting an Agnostic Strategies Module.
91
- * @param {Readonly<import('@typescript-eslint/utils').TSESLint.RuleContext<typeof reExportNotSameMessageId | typeof importBreaksCommentedImportRulesMessageId | typeof noCommentedDirective | typeof commentedDirectiveVerificationFailed | typeof importNotStrategized | typeof exportNotStrategized, []>>} context The ESLint rule's `context` object.
97
+ * @param {Context} context The ESLint rule's `context` object.
92
98
  * @returns The commented directive, or lack thereof via `null`. Given the strictness of this architecture, the lack of a directive is considered a mistake. (Though rules may provide the opportunity to declare a default, and configs with preset defaults may become provided.)
93
99
  */
94
100
  export const getCommentedDirectiveFromCurrentModule = (context) => {
@@ -137,8 +143,8 @@ export const getCommentedDirectiveFromCurrentModule = (context) => {
137
143
  * - `'use client contexts'`: Client Contexts Modules ONLY export JSX.
138
144
  * - `'use agnostic conditions'`: Agnostic Conditions Modules ONLY export JSX.
139
145
  * - `'use agnostic strategies'`: Agnostic Strategies Modules may export JSX.
140
- * @param {USE_SERVER_LOGICS | USE_CLIENT_LOGICS | USE_AGNOSTIC_LOGICS | USE_SERVER_COMPONENTS | USE_CLIENT_COMPONENTS | USE_AGNOSTIC_COMPONENTS | USE_SERVER_FUNCTIONS | USE_CLIENT_CONTEXTS | USE_AGNOSTIC_CONDITIONS | USE_AGNOSTIC_STRATEGIES} directive The commented directive as written on top of the file (cannot be `null` at that stage).
141
- * @param {TSX | TS | JSX | JS | MJS | CJS} extension The JavaScript (TypeScript) extension of the file.
146
+ * @param {CommentedDirective} directive The commented directive as written on top of the file (cannot be `null` at that stage).
147
+ * @param {Extension} extension The JavaScript (TypeScript) extension of the file.
142
148
  * @returns The verified commented directive, from which imports rules are applied. Returns `null` if the verification failed, upon which an error will be reported depending on the commented directive, since the error logic here is strictly binary.
143
149
  */
144
150
  export const getVerifiedCommentedDirective = (directive, extension) => {
@@ -184,54 +190,34 @@ export const getVerifiedCommentedDirective = (directive, extension) => {
184
190
  * - `'use agnostic conditions'`, `"use agnostic conditions"` denoting an Agnostic Conditions Module.
185
191
  * - `'use agnostic strategies'`, `"use agnostic strategies"` denoting an Agnostic Strategies Module.
186
192
  * @param {string} resolvedImportPath The resolved path of the import.
187
- * @returns {USE_SERVER_LOGICS | USE_CLIENT_LOGICS | USE_AGNOSTIC_LOGICS | USE_SERVER_COMPONENTS | USE_CLIENT_COMPONENTS | USE_AGNOSTIC_COMPONENTS | USE_SERVER_FUNCTIONS | USE_CLIENT_CONTEXTS | USE_AGNOSTIC_CONDITIONS | USE_AGNOSTIC_STRATEGIES | null} The commented directive, or lack thereof via `null`. Given the strictness of this architecture, the lack of a directive is considered a mistake. (Though rules may provide the opportunity to declare a default, and configs with preset defaults may become provided.)
193
+ * @returns The commented directive, or lack thereof via `null`. Given the strictness of this architecture, the lack of a directive is considered a mistake. (Though rules may provide the opportunity to declare a default, and configs with preset defaults may become provided.)
188
194
  */
189
195
  export const getCommentedDirectiveFromImportedModule = (resolvedImportPath) => {
190
196
  // gets the first line of the code of the import
191
197
  const importedFileFirstLine = getImportedFileFirstLine(resolvedImportPath);
192
198
 
193
- // sees if the first line includes any of the directives and finds the directive that it includes
194
- /** @type {USE_SERVER_LOGICS | USE_CLIENT_LOGICS | USE_AGNOSTIC_LOGICS | USE_SERVER_COMPONENTS | USE_CLIENT_COMPONENTS | USE_AGNOSTIC_COMPONENTS | USE_SERVER_FUNCTIONS | USE_CLIENT_CONTEXTS | USE_AGNOSTIC_CONDITIONS | USE_AGNOSTIC_STRATEGIES | ""} */
195
- let includedDirective = "";
196
- const firstLength = directivesArray.length;
197
-
198
- for (let i = 0; i < firstLength; i++) {
199
- const directive = directivesArray[i];
200
- if (importedFileFirstLine.includes(directive)) {
201
- includedDirective = directive;
202
- break;
203
- }
204
- }
205
-
206
- // returns null early if there is none of the directives in the first line
207
- if (includedDirective === "") return null;
208
-
209
- /** @type {USE_SERVER_LOGICS | USE_CLIENT_LOGICS | USE_AGNOSTIC_LOGICS | USE_SERVER_COMPONENTS | USE_CLIENT_COMPONENTS | USE_AGNOSTIC_COMPONENTS | USE_SERVER_FUNCTIONS | USE_CLIENT_CONTEXTS | USE_AGNOSTIC_CONDITIONS | USE_AGNOSTIC_STRATEGIES | ""} */
210
- let importFileDirective = "";
211
- const rawImplementations =
212
- commentedDirectives_4RawImplementations[includedDirective];
213
- const secondLength = rawImplementations.length;
214
-
215
- for (let i = 0; i < secondLength; i++) {
216
- const raw = rawImplementations[i];
217
- if (raw === importedFileFirstLine) {
218
- importFileDirective = includedDirective;
219
- break;
220
- }
221
- }
222
-
223
- // returns null early if despite the presence of the directive it is not properly implemented
224
- if (importFileDirective === "") return null;
225
-
226
- return importFileDirective;
199
+ // sees if the first line includes any of the directives and finds the directive that it includes, with USE_AGNOSTIC_LOGICS as a default
200
+ const includedDirective = directivesArray.reduce((acc, curr) => {
201
+ if (importedFileFirstLine.includes(curr)) return curr;
202
+ else return acc;
203
+ }, USE_AGNOSTIC_LOGICS);
204
+
205
+ // sees if the first line is strictly equal to one of the four raw implementations of the commented directive and returns that directive if true or null if false
206
+ if (
207
+ commentedDirectives_4RawImplementations[includedDirective].some(
208
+ (raw) => raw === importedFileFirstLine
209
+ )
210
+ )
211
+ return includedDirective;
212
+ else return null;
227
213
  };
228
214
 
229
215
  /* getStrategizedDirective */
230
216
 
231
217
  /**
232
218
  * Gets the interpreted directive from a specified commented Strategy (such as `@serverLogics`) nested inside the import declaration for an import from an Agnostic Strategies Module.
233
- * @param {Readonly<import('@typescript-eslint/utils').TSESLint.RuleContext<typeof reExportNotSameMessageId | typeof importBreaksCommentedImportRulesMessageId | typeof noCommentedDirective | typeof commentedDirectiveVerificationFailed | typeof importNotStrategized | typeof exportNotStrategized, []>>} context The ESLint rule's `context` object.
234
- * @param {import('@typescript-eslint/types').TSESTree.ImportDeclaration} node The ESLint `node` of the rule's current traversal.
219
+ * @param {Context} context The ESLint rule's `context` object.
220
+ * @param {ImportDeclaration} node The ESLint `node` of the rule's current traversal.
235
221
  * @returns The interpreted directive, a.k.a. strategized directive, or lack thereof via `null`.
236
222
  */
237
223
  export const getStrategizedDirective = (context, node) => {
@@ -248,7 +234,7 @@ export const getStrategizedDirective = (context, node) => {
248
234
  // returns null early if no strategy was identified
249
235
  if (!strategy) return null;
250
236
 
251
- const commentedDirective = commentedStrategies_CommentedDirectives[strategy];
237
+ const commentedDirective = commentedStrategies_commentedDirectives[strategy];
252
238
 
253
239
  return commentedDirective;
254
240
  };
@@ -257,16 +243,16 @@ export const getStrategizedDirective = (context, node) => {
257
243
 
258
244
  /**
259
245
  * Returns a boolean deciding if an imported file's commented directive is incompatible with the current file's commented directive.
260
- * @param {USE_SERVER_LOGICS | USE_SERVER_COMPONENTS | USE_SERVER_FUNCTIONS | USE_CLIENT_LOGICS | USE_CLIENT_COMPONENTS | USE_AGNOSTIC_LOGICS | USE_AGNOSTIC_COMPONENTS} currentFileCommentedDirective The current file's commented directive.
261
- * @param {USE_SERVER_LOGICS | USE_SERVER_COMPONENTS | USE_SERVER_FUNCTIONS | USE_CLIENT_LOGICS | USE_CLIENT_COMPONENTS | USE_AGNOSTIC_LOGICS | USE_AGNOSTIC_COMPONENTS} importedFileCommentedDirective The imported file's commented directive.
262
- * @returns {boolean} Returns `true` if the import is blocked, as established in `commentedDirectives_BlockedImports`.
246
+ * @param {CommentedDirective} currentFileCommentedDirective The current file's commented directive.
247
+ * @param {CommentedDirectiveWithoutUseAgnosticStrategies} importedFileCommentedDirective The imported file's commented directive.
248
+ * @returns `true` if the import is blocked, as established in `commentedDirectives_BlockedImports`.
263
249
  */
264
250
  export const isImportBlocked = (
265
251
  currentFileCommentedDirective,
266
252
  importedFileCommentedDirective
267
253
  ) =>
268
254
  commonsIsImportBlocked(
269
- commentedDirectives_BlockedImports,
255
+ commentedDirectives_blockedImports,
270
256
  currentFileCommentedDirective,
271
257
  importedFileCommentedDirective
272
258
  );
@@ -275,13 +261,13 @@ export const isImportBlocked = (
275
261
 
276
262
  /**
277
263
  * Lists in an message the commented modules incompatible with a commented module based on its commented directive.
278
- * @param {USE_SERVER_LOGICS | USE_CLIENT_LOGICS | USE_AGNOSTIC_LOGICS | USE_SERVER_COMPONENTS | USE_CLIENT_COMPONENTS | USE_AGNOSTIC_COMPONENTS | USE_SERVER_FUNCTIONS | USE_CLIENT_CONTEXTS | USE_AGNOSTIC_CONDITIONS | USE_AGNOSTIC_STRATEGIES} commentedDirective The commented directive of the commented module.
279
- * @returns {string} The message listing the incompatible commented modules.
264
+ * @param {CommentedDirective} commentedDirective The commented directive of the commented module.
265
+ * @returns The message listing the incompatible commented modules.
280
266
  */
281
267
  export const makeMessageFromCommentedDirective = (commentedDirective) =>
282
268
  makeMessageFromResolvedDirective(
283
- commentedDirectives_CommentedModules,
284
- commentedDirectives_BlockedImports,
269
+ commentedDirectives_commentedModules,
270
+ commentedDirectives_blockedImports,
285
271
  commentedDirective
286
272
  );
287
273
 
@@ -289,16 +275,16 @@ export const makeMessageFromCommentedDirective = (commentedDirective) =>
289
275
 
290
276
  /**
291
277
  * Finds the `message` for the specific violation of commented directives import rules based on `commentedDirectives_BlockedImports`.
292
- * @param {USE_SERVER_LOGICS | USE_CLIENT_LOGICS | USE_AGNOSTIC_LOGICS | USE_SERVER_COMPONENTS | USE_CLIENT_COMPONENTS | USE_AGNOSTIC_COMPONENTS | USE_SERVER_FUNCTIONS | USE_CLIENT_CONTEXTS | USE_AGNOSTIC_CONDITIONS | USE_AGNOSTIC_STRATEGIES} currentFileCommentedDirective The current file's commented directive.
293
- * @param {USE_SERVER_LOGICS | USE_CLIENT_LOGICS | USE_AGNOSTIC_LOGICS | USE_SERVER_COMPONENTS | USE_CLIENT_COMPONENTS | USE_AGNOSTIC_COMPONENTS | USE_SERVER_FUNCTIONS | USE_CLIENT_CONTEXTS | USE_AGNOSTIC_CONDITIONS} importedFileCommentedDirective The imported file's commented directive.
294
- * @returns {string} The corresponding `message`.
278
+ * @param {CommentedDirective} currentFileCommentedDirective The current file's commented directive.
279
+ * @param {CommentedDirectiveWithoutUseAgnosticStrategies} importedFileCommentedDirective The imported file's commented directive.
280
+ * @returns The corresponding `message`.
295
281
  */
296
282
  export const findSpecificViolationMessage = (
297
283
  currentFileCommentedDirective,
298
284
  importedFileCommentedDirective
299
285
  ) =>
300
286
  commonsFindSpecificViolationMessage(
301
- commentedDirectives_BlockedImports,
287
+ commentedDirectives_blockedImports,
302
288
  currentFileCommentedDirective,
303
289
  importedFileCommentedDirective
304
290
  );
@@ -307,9 +293,9 @@ export const findSpecificViolationMessage = (
307
293
 
308
294
  /**
309
295
  * Verifies the current node's export strategy if `"use agnostic strategies"` by reporting `exportNotStrategized` in case an export is not strategized in an Agnostic Strategies Module.
310
- * @param {Readonly<import('@typescript-eslint/utils').TSESLint.RuleContext<typeof reExportNotSameMessageId | typeof importBreaksCommentedImportRulesMessageId | typeof noCommentedDirective | typeof commentedDirectiveVerificationFailed | typeof importNotStrategized | typeof exportNotStrategized, []>>} context The ESLint rule's `context` object.
311
- * @param {import('@typescript-eslint/types').TSESTree.ExportNamedDeclaration | import('@typescript-eslint/types').TSESTree.ExportAllDeclaration | import('@typescript-eslint/types').TSESTree.ExportDefaultDeclaration} node The ESLint `node` of the rule's current traversal.
312
- * @param {USE_SERVER_LOGICS | USE_CLIENT_LOGICS | USE_AGNOSTIC_LOGICS | USE_SERVER_COMPONENTS | USE_CLIENT_COMPONENTS | USE_AGNOSTIC_COMPONENTS | USE_SERVER_FUNCTIONS | USE_CLIENT_CONTEXTS | USE_AGNOSTIC_CONDITIONS | USE_AGNOSTIC_STRATEGIES} currentFileCommentedDirective The current file's commented directive.
296
+ * @param {Context} context The ESLint rule's `context` object.
297
+ * @param {ExportNamedDeclaration | ExportAllDeclaration | ExportDefaultDeclaration} node The ESLint `node` of the rule's current traversal.
298
+ * @param {CommentedDirective} currentFileCommentedDirective The current file's commented directive.
313
299
  * @returns The commented directive, the addressed strategy (as a commented directive) or null in case of failure.
314
300
  */
315
301
  export const addressDirectiveIfAgnosticStrategies = (
@@ -7,9 +7,13 @@ import {
7
7
  enforceCommentedDirectivesRuleName,
8
8
  } from "../_commons/constants/bases.js";
9
9
 
10
+ /**
11
+ * @typedef {import('../../types/agnostic20/_commons/typedefs.js').Plugin} Plugin
12
+ */
13
+
10
14
  /**
11
15
  * Makes the directive21 config for the use-agnostic ESLint plugin.
12
- * @param {import('eslint').ESLint.Plugin} plugin The use-agnostic ESLint plugin itself.
16
+ * @param {Plugin} plugin The use-agnostic ESLint plugin itself.
13
17
  * @returns The directive21 config's name as a key and its config as its value.
14
18
  */
15
19
  export const makeDirective21Config = (plugin) => ({
package/library/index.js CHANGED
@@ -11,14 +11,19 @@ import enforceCommentedDirectivesImportRules from "./directive21/_commons/rules/
11
11
  import { makeAgnostic20Config } from "./agnostic20/config.js";
12
12
  import { makeDirective21Config } from "./directive21/config.js";
13
13
 
14
- /** @type {import("tsconfig-paths/lib/filesystem.js").PackageJson} */
15
- const packageDotJSON = JSON.parse(
14
+ /**
15
+ * @typedef {import("../types/_commons/typedefs.js").PackageJson} PackageJson
16
+ * @typedef {import("../types/_commons/typedefs.js").Plugin} Plugin
17
+ */
18
+
19
+ /** @type {PackageJson} */
20
+ const packageJSON = JSON.parse(
16
21
  fs.readFileSync(new URL("../package.json", import.meta.url), "utf8")
17
22
  );
18
23
 
19
- /** @type {import('eslint').ESLint.Plugin} */
24
+ /** @type {Plugin} */
20
25
  const plugin = {
21
- meta: { ...packageDotJSON },
26
+ meta: { ...packageJSON },
22
27
  configs: {}, // applied below
23
28
  rules: {
24
29
  [enforceEffectiveDirectivesRuleName]: enforceEffectiveDirectivesImportRules,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-use-agnostic",
3
- "version": "0.9.6",
3
+ "version": "0.9.7",
4
4
  "description": "Highlights problematic server-client imports in projects made with the Fullstack React Architecture.",
5
5
  "keywords": [
6
6
  "eslint",