@checkdigit/eslint-plugin 7.12.0 → 7.13.0-PR.119-f22e

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.
@@ -3,58 +3,62 @@ import { AST_NODE_TYPES, ESLintUtils } from "@typescript-eslint/utils";
3
3
  import getDocumentationUrl from "./get-documentation-url.mjs";
4
4
  var ruleId = "no-random-v4-uuid";
5
5
  var NO_RANDOM_V4_UUID = "NO_RANDOM_V4_UUID";
6
+ var NO_UUID_MODULE_FOR_V4 = "NO_UUID_MODULE_FOR_V4";
6
7
  var createRule = ESLintUtils.RuleCreator((name) => getDocumentationUrl(name));
7
- var processImportDeclaration = (node, uuid4Alias, uuidDefaultAlias) => {
8
- let updatedUuid4Alias = uuid4Alias;
9
- let updatedUuidDefaultAlias = uuidDefaultAlias;
8
+ var processImportDeclaration = (node, aliases) => {
10
9
  node.specifiers.forEach((specifier) => {
11
- switch (specifier.type) {
12
- case AST_NODE_TYPES.ImportSpecifier:
13
- if (specifier.imported.type === AST_NODE_TYPES.Identifier && specifier.imported.name === "v4") {
14
- updatedUuid4Alias = specifier.local.name;
15
- }
16
- break;
17
- case AST_NODE_TYPES.ImportDefaultSpecifier:
18
- updatedUuidDefaultAlias = specifier.local.name;
19
- break;
10
+ if (specifier.type === AST_NODE_TYPES.ImportSpecifier) {
11
+ if (node.source.value === "uuid" && specifier.imported.type === AST_NODE_TYPES.Identifier && specifier.imported.name === "v4") {
12
+ aliases.uuid4Alias = specifier.local.name;
13
+ } else if (node.source.value === "node:crypto" && specifier.imported.type === AST_NODE_TYPES.Identifier && specifier.imported.name === "randomUUID") {
14
+ aliases.cryptoRandomUUIDAlias = specifier.local.name;
15
+ }
16
+ } else if (specifier.type === AST_NODE_TYPES.ImportDefaultSpecifier && node.source.value === "uuid") {
17
+ aliases.uuidDefaultAlias = specifier.local.name;
20
18
  }
21
19
  });
22
- return { uuid4Alias: updatedUuid4Alias, uuidDefaultAlias: updatedUuidDefaultAlias };
23
20
  };
24
- var isUuid4Call = (node, uuid4Alias, uuidDefaultAlias) => node.callee.type === AST_NODE_TYPES.Identifier && node.callee.name === uuid4Alias || node.callee.type === AST_NODE_TYPES.MemberExpression && node.callee.object.type === AST_NODE_TYPES.Identifier && node.callee.object.name === uuidDefaultAlias && node.callee.property.type === AST_NODE_TYPES.Identifier && node.callee.property.name === "v4";
21
+ var isUuid4Call = (node, aliases) => node.callee.type === AST_NODE_TYPES.Identifier && node.callee.name === aliases.uuid4Alias || node.callee.type === AST_NODE_TYPES.MemberExpression && node.callee.object.type === AST_NODE_TYPES.Identifier && node.callee.object.name === aliases.uuidDefaultAlias && node.callee.property.type === AST_NODE_TYPES.Identifier && node.callee.property.name === "v4";
22
+ var isCryptoRandomUUIDCall = (node, alias) => node.callee.type === AST_NODE_TYPES.Identifier && node.callee.name === alias || node.callee.type === AST_NODE_TYPES.MemberExpression && node.callee.object.type === AST_NODE_TYPES.Identifier && node.callee.object.name === "crypto" && node.callee.property.type === AST_NODE_TYPES.Identifier && node.callee.property.name === "randomUUID";
25
23
  var rule = createRule({
26
24
  name: ruleId,
27
25
  meta: {
28
26
  type: "problem",
29
27
  docs: {
30
- description: "Disallow the use of `uuid.v4` for generating random v4 UUIDs"
28
+ description: "Disallow the use of `uuid.v4` and `crypto.randomUUID` for generating random v4 UUIDs, and suggest replacing `uuid` module usage with `crypto.randomUUID`."
31
29
  },
32
30
  schema: [],
33
31
  messages: {
34
- [NO_RANDOM_V4_UUID]: "Avoid using `uuid.v4` for generating random v4 UUIDs."
32
+ [NO_RANDOM_V4_UUID]: "Avoid using `uuid.v4` or `crypto.randomUUID` for generating random v4 UUIDs.",
33
+ [NO_UUID_MODULE_FOR_V4]: "Avoid using the `uuid` module for v4 UUID generation. Use `crypto.randomUUID` instead."
35
34
  }
36
35
  },
37
36
  defaultOptions: [],
38
37
  create(context) {
39
- let uuid4Alias;
40
- let uuidDefaultAlias;
41
- let hasUuidImport = false;
38
+ const aliases = {};
39
+ let uuidImportNode = null;
42
40
  return {
43
41
  ImportDeclaration(node) {
42
+ processImportDeclaration(node, aliases);
44
43
  if (node.source.value === "uuid") {
45
- hasUuidImport = true;
46
- const result = processImportDeclaration(node, uuid4Alias, uuidDefaultAlias);
47
- uuid4Alias = result.uuid4Alias;
48
- uuidDefaultAlias = result.uuidDefaultAlias;
44
+ uuidImportNode = node;
49
45
  }
50
46
  },
51
47
  CallExpression(node) {
52
- if (hasUuidImport && isUuid4Call(node, uuid4Alias, uuidDefaultAlias)) {
48
+ if (isUuid4Call(node, aliases) || isCryptoRandomUUIDCall(node, aliases.cryptoRandomUUIDAlias)) {
53
49
  context.report({
54
50
  node,
55
51
  messageId: NO_RANDOM_V4_UUID
56
52
  });
57
53
  }
54
+ },
55
+ "Program:exit"() {
56
+ if (uuidImportNode && aliases.uuid4Alias !== void 0 && aliases.uuid4Alias !== "" && (aliases.uuidDefaultAlias === void 0 || aliases.uuidDefaultAlias === "")) {
57
+ context.report({
58
+ node: uuidImportNode,
59
+ messageId: NO_UUID_MODULE_FOR_V4
60
+ });
61
+ }
58
62
  }
59
63
  };
60
64
  }
@@ -64,4 +68,4 @@ export {
64
68
  no_random_v4_uuid_default as default,
65
69
  ruleId
66
70
  };
67
- //# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vc3JjL25vLXJhbmRvbS12NC11dWlkLnRzIl0sCiAgIm1hcHBpbmdzIjogIjtBQVFBLFNBQVMsZ0JBQWdCLG1CQUF1QztBQUNoRSxPQUFPLHlCQUF5QjtBQUV6QixJQUFNLFNBQVM7QUFDdEIsSUFBTSxvQkFBb0I7QUFFMUIsSUFBTSxhQUFhLFlBQVksWUFBWSxDQUFDLFNBQVMsb0JBQW9CLElBQUksQ0FBQztBQUc5RSxJQUFNLDJCQUEyQixDQUMvQixNQUNBLFlBQ0EscUJBQ0c7QUFDSCxNQUFJLG9CQUFvQjtBQUN4QixNQUFJLDBCQUEwQjtBQ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IFtdCn0K
@@ -1,5 +1,6 @@
1
1
  import { TSESLint } from '@typescript-eslint/utils';
2
2
  export declare const ruleId = "no-random-v4-uuid";
3
3
  declare const NO_RANDOM_V4_UUID = "NO_RANDOM_V4_UUID";
4
- declare const rule: TSESLint.RuleModule<typeof NO_RANDOM_V4_UUID>;
4
+ declare const NO_UUID_MODULE_FOR_V4 = "NO_UUID_MODULE_FOR_V4";
5
+ declare const rule: TSESLint.RuleModule<typeof NO_RANDOM_V4_UUID | typeof NO_UUID_MODULE_FOR_V4>;
5
6
  export default rule;
package/package.json CHANGED
@@ -1,96 +1 @@
1
- {
2
- "name": "@checkdigit/eslint-plugin",
3
- "version": "7.12.0",
4
- "description": "Check Digit eslint plugins",
5
- "keywords": [
6
- "eslint",
7
- "eslintplugin"
8
- ],
9
- "homepage": "https://github.com/checkdigit/eslint-plugin#readme",
10
- "bugs": {
11
- "url": "https://github.com/checkdigit/eslint-plugin/issues"
12
- },
13
- "repository": {
14
- "type": "git",
15
- "url": "https://github.com/checkdigit/eslint-plugin"
16
- },
17
- "license": "MIT",
18
- "author": "Check Digit, LLC",
19
- "sideEffects": false,
20
- "type": "module",
21
- "exports": {
22
- ".": {
23
- "types": "./dist-types/index.d.ts",
24
- "import": "./dist-mjs/index.mjs",
25
- "default": "./dist-mjs/index.mjs"
26
- }
27
- },
28
- "files": [
29
- "src",
30
- "dist-types",
31
- "dist-mjs",
32
- "!src/**/test/**",
33
- "!src/**/*.test.ts",
34
- "!src/**/*.spec.ts",
35
- "!dist-types/**/test/**",
36
- "!dist-types/**/*.test.d.ts",
37
- "!dist-types/**/*.spec.d.ts",
38
- "!dist-mjs/**/test/**",
39
- "!dist-mjs/**/*.test.mjs",
40
- "!dist-mjs/**/*.spec.mjs",
41
- "SECURITY.md"
42
- ],
43
- "scripts": {
44
- "build:dist-mjs": "rimraf dist-mjs && npx builder --type=module --sourceMap --outDir=dist-mjs && node dist-mjs/index.mjs",
45
- "build:dist-types": "rimraf dist-types && npx builder --type=types --outDir=dist-types",
46
- "ci:compile": "tsc --noEmit",
47
- "ci:coverage": "NODE_OPTIONS=\"--disable-warning ExperimentalWarning --experimental-vm-modules\" jest --coverage=true",
48
- "ci:lint": "npm run lint",
49
- "ci:style": "npm run prettier",
50
- "ci:test": "NODE_OPTIONS=\"--disable-warning ExperimentalWarning --experimental-vm-modules\" jest --coverage=false",
51
- "lint": "eslint --max-warnings 0 .",
52
- "lint:fix": "eslint --max-warnings 0 --fix .",
53
- "prepare": "",
54
- "prepublishOnly": "npm run build:dist-types && npm run build:dist-mjs",
55
- "prettier": "prettier --ignore-path .gitignore --list-different .",
56
- "prettier:fix": "prettier --ignore-path .gitignore --write .",
57
- "test": "npm run ci:compile && npm run ci:test && npm run ci:lint && npm run ci:style"
58
- },
59
- "prettier": "@checkdigit/prettier-config",
60
- "jest": {
61
- "preset": "@checkdigit/jest-config"
62
- },
63
- "dependencies": {
64
- "@typescript-eslint/type-utils": "^8.23.0",
65
- "@typescript-eslint/utils": "^8.23.0",
66
- "http-status-codes": "^2.3.0",
67
- "ts-api-utils": "^2.0.1"
68
- },
69
- "devDependencies": {
70
- "@checkdigit/jest-config": "^6.0.2",
71
- "@checkdigit/prettier-config": "^6.1.0",
72
- "@checkdigit/typescript-config": "^9.0.0",
73
- "@eslint/js": "^9.19.0",
74
- "@types/eslint": "^9.6.1",
75
- "@types/eslint-config-prettier": "^6.11.3",
76
- "@typescript-eslint/parser": "^8.23.0",
77
- "@typescript-eslint/rule-tester": "^8.23.0",
78
- "eslint": "^9.19.0",
79
- "eslint-config-prettier": "^10.0.1",
80
- "eslint-import-resolver-typescript": "^3.7.0",
81
- "eslint-plugin-eslint-plugin": "^6.4.0",
82
- "eslint-plugin-import": "^2.31.0",
83
- "eslint-plugin-no-only-tests": "^3.3.0",
84
- "eslint-plugin-no-secrets": "^2.2.1",
85
- "eslint-plugin-node": "^11.1.0",
86
- "eslint-plugin-sonarjs": "1.0.4",
87
- "rimraf": "^6.0.1",
88
- "typescript-eslint": "^8.23.0"
89
- },
90
- "peerDependencies": {
91
- "eslint": ">=9 <10"
92
- },
93
- "engines": {
94
- "node": ">=20.17"
95
- }
96
- }
1
+ {"name":"@checkdigit/eslint-plugin","version":"7.13.0-PR.119-f22e","description":"Check Digit eslint plugins","keywords":["eslint","eslintplugin"],"homepage":"https://github.com/checkdigit/eslint-plugin#readme","bugs":{"url":"https://github.com/checkdigit/eslint-plugin/issues"},"repository":{"type":"git","url":"https://github.com/checkdigit/eslint-plugin"},"license":"MIT","author":"Check Digit, LLC","sideEffects":false,"type":"module","exports":{".":{"types":"./dist-types/index.d.ts","import":"./dist-mjs/index.mjs","default":"./dist-mjs/index.mjs"}},"files":["src","dist-types","dist-mjs","!src/**/test/**","!src/**/*.test.ts","!src/**/*.spec.ts","!dist-types/**/test/**","!dist-types/**/*.test.d.ts","!dist-types/**/*.spec.d.ts","!dist-mjs/**/test/**","!dist-mjs/**/*.test.mjs","!dist-mjs/**/*.spec.mjs","SECURITY.md"],"scripts":{"build:dist-mjs":"rimraf dist-mjs && npx builder --type=module --sourceMap --outDir=dist-mjs && node dist-mjs/index.mjs","build:dist-types":"rimraf dist-types && npx builder --type=types --outDir=dist-types","ci:compile":"tsc --noEmit","ci:coverage":"NODE_OPTIONS=\"--disable-warning ExperimentalWarning --experimental-vm-modules\" jest --coverage=true","ci:lint":"npm run lint","ci:style":"npm run prettier","ci:test":"NODE_OPTIONS=\"--disable-warning ExperimentalWarning --experimental-vm-modules\" jest --coverage=false","lint":"eslint --max-warnings 0 .","lint:fix":"eslint --max-warnings 0 --fix .","prepare":"","prepublishOnly":"npm run build:dist-types && npm run build:dist-mjs","prettier":"prettier --ignore-path .gitignore --list-different .","prettier:fix":"prettier --ignore-path .gitignore --write .","test":"npm run ci:compile && npm run ci:test && npm run ci:lint && npm run ci:style"},"prettier":"@checkdigit/prettier-config","jest":{"preset":"@checkdigit/jest-config"},"dependencies":{"@typescript-eslint/type-utils":"^8.23.0","@typescript-eslint/utils":"^8.23.0","http-status-codes":"^2.3.0","ts-api-utils":"^2.0.1"},"devDependencies":{"@checkdigit/jest-config":"^6.0.2","@checkdigit/prettier-config":"^6.1.0","@checkdigit/typescript-config":"^9.0.0","@eslint/js":"^9.19.0","@types/eslint":"^9.6.1","@types/eslint-config-prettier":"^6.11.3","@typescript-eslint/parser":"^8.23.0","@typescript-eslint/rule-tester":"^8.23.0","eslint":"^9.19.0","eslint-config-prettier":"^10.0.1","eslint-import-resolver-typescript":"^3.7.0","eslint-plugin-eslint-plugin":"^6.4.0","eslint-plugin-import":"^2.31.0","eslint-plugin-no-only-tests":"^3.3.0","eslint-plugin-no-secrets":"^2.2.1","eslint-plugin-node":"^11.1.0","eslint-plugin-sonarjs":"1.0.4","rimraf":"^6.0.1","typescript-eslint":"^8.23.0"},"peerDependencies":{"eslint":">=9 <10"},"engines":{"node":">=20.17"}}
@@ -1,7 +1,7 @@
1
1
  // no-random-v4-uuid.ts
2
2
 
3
3
  /*
4
- * Copyright (c) 2022-2024 Check Digit, LLC
4
+ * Copyright (c) 2022-2025 Check Digit, LLC
5
5
  *
6
6
  * This code is licensed under the MIT license (see LICENSE.txt for details).
7
7
  */
@@ -11,80 +11,101 @@ import getDocumentationUrl from './get-documentation-url.ts';
11
11
 
12
12
  export const ruleId = 'no-random-v4-uuid';
13
13
  const NO_RANDOM_V4_UUID = 'NO_RANDOM_V4_UUID';
14
+ const NO_UUID_MODULE_FOR_V4 = 'NO_UUID_MODULE_FOR_V4';
14
15
 
15
16
  const createRule = ESLintUtils.RuleCreator((name) => getDocumentationUrl(name));
16
17
 
17
- // process the import declaration to get the alias for uuid.v4 and uuid.
18
18
  const processImportDeclaration = (
19
19
  node: TSESTree.ImportDeclaration,
20
- uuid4Alias: string | undefined,
21
- uuidDefaultAlias: string | undefined,
20
+ aliases: { uuid4Alias?: string; uuidDefaultAlias?: string; cryptoRandomUUIDAlias?: string },
22
21
  ) => {
23
- let updatedUuid4Alias = uuid4Alias;
24
- let updatedUuidDefaultAlias = uuidDefaultAlias;
25
22
  node.specifiers.forEach((specifier) => {
26
- switch (specifier.type) {
27
- case AST_NODE_TYPES.ImportSpecifier:
28
- if (specifier.imported.type === AST_NODE_TYPES.Identifier && specifier.imported.name === 'v4') {
29
- updatedUuid4Alias = specifier.local.name;
30
- }
31
- break;
32
- case AST_NODE_TYPES.ImportDefaultSpecifier:
33
- updatedUuidDefaultAlias = specifier.local.name;
34
- break;
23
+ if (specifier.type === AST_NODE_TYPES.ImportSpecifier) {
24
+ if (
25
+ node.source.value === 'uuid' &&
26
+ specifier.imported.type === AST_NODE_TYPES.Identifier &&
27
+ specifier.imported.name === 'v4'
28
+ ) {
29
+ aliases.uuid4Alias = specifier.local.name;
30
+ } else if (
31
+ node.source.value === 'node:crypto' &&
32
+ specifier.imported.type === AST_NODE_TYPES.Identifier &&
33
+ specifier.imported.name === 'randomUUID'
34
+ ) {
35
+ aliases.cryptoRandomUUIDAlias = specifier.local.name;
36
+ }
37
+ } else if (specifier.type === AST_NODE_TYPES.ImportDefaultSpecifier && node.source.value === 'uuid') {
38
+ aliases.uuidDefaultAlias = specifier.local.name;
35
39
  }
36
40
  });
37
- return { uuid4Alias: updatedUuid4Alias, uuidDefaultAlias: updatedUuidDefaultAlias };
38
41
  };
39
42
 
40
- // checks if the function call is either directly using the alias for uuid.v4 or using uuid.v4 as a member expression.
41
43
  const isUuid4Call = (
42
44
  node: TSESTree.CallExpression,
43
- uuid4Alias: string | undefined,
44
- uuidDefaultAlias: string | undefined,
45
+ aliases: { uuid4Alias?: string; uuidDefaultAlias?: string },
45
46
  ): boolean =>
46
- (node.callee.type === AST_NODE_TYPES.Identifier && node.callee.name === uuid4Alias) ||
47
+ (node.callee.type === AST_NODE_TYPES.Identifier && node.callee.name === aliases.uuid4Alias) ||
47
48
  (node.callee.type === AST_NODE_TYPES.MemberExpression &&
48
49
  node.callee.object.type === AST_NODE_TYPES.Identifier &&
49
- node.callee.object.name === uuidDefaultAlias &&
50
+ node.callee.object.name === aliases.uuidDefaultAlias &&
50
51
  node.callee.property.type === AST_NODE_TYPES.Identifier &&
51
52
  node.callee.property.name === 'v4');
52
53
 
53
- const rule: TSESLint.RuleModule<typeof NO_RANDOM_V4_UUID> = createRule({
54
+ const isCryptoRandomUUIDCall = (node: TSESTree.CallExpression, alias?: string): boolean =>
55
+ (node.callee.type === AST_NODE_TYPES.Identifier && node.callee.name === alias) ||
56
+ (node.callee.type === AST_NODE_TYPES.MemberExpression &&
57
+ node.callee.object.type === AST_NODE_TYPES.Identifier &&
58
+ node.callee.object.name === 'crypto' &&
59
+ node.callee.property.type === AST_NODE_TYPES.Identifier &&
60
+ node.callee.property.name === 'randomUUID');
61
+
62
+ const rule: TSESLint.RuleModule<typeof NO_RANDOM_V4_UUID | typeof NO_UUID_MODULE_FOR_V4> = createRule({
54
63
  name: ruleId,
55
64
  meta: {
56
65
  type: 'problem',
57
66
  docs: {
58
- description: 'Disallow the use of `uuid.v4` for generating random v4 UUIDs',
67
+ description:
68
+ 'Disallow the use of `uuid.v4` and `crypto.randomUUID` for generating random v4 UUIDs, and suggest replacing `uuid` module usage with `crypto.randomUUID`.',
59
69
  },
60
70
  schema: [],
61
71
  messages: {
62
- [NO_RANDOM_V4_UUID]: 'Avoid using `uuid.v4` for generating random v4 UUIDs.',
72
+ [NO_RANDOM_V4_UUID]: 'Avoid using `uuid.v4` or `crypto.randomUUID` for generating random v4 UUIDs.',
73
+ [NO_UUID_MODULE_FOR_V4]: 'Avoid using the `uuid` module for v4 UUID generation. Use `crypto.randomUUID` instead.',
63
74
  },
64
75
  },
65
76
  defaultOptions: [],
66
77
  create(context) {
67
- let uuid4Alias: string | undefined;
68
- let uuidDefaultAlias: string | undefined;
69
- let hasUuidImport = false;
78
+ const aliases: { uuid4Alias?: string; uuidDefaultAlias?: string; cryptoRandomUUIDAlias?: string } = {};
79
+ let uuidImportNode: TSESTree.ImportDeclaration | null = null;
70
80
 
71
81
  return {
72
82
  ImportDeclaration(node: TSESTree.ImportDeclaration) {
83
+ processImportDeclaration(node, aliases);
73
84
  if (node.source.value === 'uuid') {
74
- hasUuidImport = true;
75
- const result = processImportDeclaration(node, uuid4Alias, uuidDefaultAlias);
76
- uuid4Alias = result.uuid4Alias;
77
- uuidDefaultAlias = result.uuidDefaultAlias;
85
+ uuidImportNode = node;
78
86
  }
79
87
  },
80
88
  CallExpression(node: TSESTree.CallExpression) {
81
- if (hasUuidImport && isUuid4Call(node, uuid4Alias, uuidDefaultAlias)) {
89
+ if (isUuid4Call(node, aliases) || isCryptoRandomUUIDCall(node, aliases.cryptoRandomUUIDAlias)) {
82
90
  context.report({
83
91
  node,
84
92
  messageId: NO_RANDOM_V4_UUID,
85
93
  });
86
94
  }
87
95
  },
96
+ 'Program:exit'() {
97
+ if (
98
+ uuidImportNode &&
99
+ aliases.uuid4Alias !== undefined &&
100
+ aliases.uuid4Alias !== '' &&
101
+ (aliases.uuidDefaultAlias === undefined || aliases.uuidDefaultAlias === '')
102
+ ) {
103
+ context.report({
104
+ node: uuidImportNode,
105
+ messageId: NO_UUID_MODULE_FOR_V4,
106
+ });
107
+ }
108
+ },
88
109
  };
89
110
  },
90
111
  });