@mimik/eslint-plugin-document-env 2.0.4 → 2.0.6

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/index.js CHANGED
@@ -2,58 +2,96 @@ const HEADER = ' * | Env variable name | Description | Default | Comments |\n';
2
2
  const LINE = ' * | ----------------- | ----------- | ------- | -------- |\n';
3
3
  const SINGLE = 1;
4
4
  const NO_DESCRIPTION = 2;
5
+ const FIRST = 0;
6
+ const EMPTY = 0;
7
+
8
+ const isProcessEnvAccess = node => (
9
+ node.object?.name === 'process'
10
+ && !node.computed
11
+ && node.property?.name === 'env'
12
+ );
13
+ const regular = propName => new RegExp(
14
+ `^ \\* \\| (${propName.replace(/[|\\{}()[\]^$+*?.]/gu, '\\$&')}) \\| *([^\\|]*)`,
15
+ 'mgu',
16
+ );
17
+ const extractEnvVarName = (node, context) => node.parent?.property?.name
18
+ || context.getSourceCode().text.slice(node.parent.property.start, node.parent.property.end);
19
+ const findEnvDocInComments = (comments, propName) => {
20
+ const regex = regular(propName);
21
+
22
+ return comments.filter(comment =>
23
+ comment.type === 'Block'
24
+ && comment.value.includes(HEADER)
25
+ && comment.value.includes(LINE)
26
+ && comment.value.match(regex),
27
+ );
28
+ };
5
29
 
6
30
  const plugin = {
7
31
  rules: {
8
32
  'validate-document-env': {
9
33
  create(context) {
34
+ const { comments } = context.getSourceCode().ast;
35
+
10
36
  return {
11
37
  MemberExpression(node) {
12
- const objectName = node.object.name;
13
- const propertyName = node.property.name;
14
-
15
- if (objectName === 'process' && !node.computed && propertyName && propertyName === 'env') {
16
- if (node.parent && node.parent.property) {
17
- const allComments = context.getSourceCode().ast.comments;
18
- let foundDocument = false;
19
- let propName = node.parent.property.name;
20
-
21
- if (!propName) propName = context.getSourceCode().text.substring(node.parent.property.start, node.parent.property.end);
22
- const reg = new RegExp(`^ \\* \\| (${propName.replace(/[|\\{}()[\]^$+*?.]/gu, '\\$&')}) \\| *([^\\|]*)`, 'mgu');
23
-
24
- allComments.forEach((comment) => {
25
- if (!foundDocument && comment.type === 'Block' && comment.value.includes(HEADER) && comment.value.includes(LINE)) {
26
- let match = comment.value.match(reg);
27
-
28
- if (match) {
29
- foundDocument = true;
30
- if (match.length > SINGLE) context.report({ data: { name: propName }, messageId: 'duplicatedProcessEnv', node });
31
- else {
32
- match = reg.exec(comment.value);
33
- if (!match[NO_DESCRIPTION]) context.report({ data: { name: propName }, messageId: 'unDesribedProcessEnv', node });
34
- }
35
- }
36
- }
38
+ if (!isProcessEnvAccess(node) || !node.parent?.property) return;
39
+
40
+ const propName = extractEnvVarName(node, context);
41
+ const matchedComment = findEnvDocInComments(comments, propName);
42
+
43
+ if (!matchedComment || matchedComment.length === EMPTY) {
44
+ context.report({
45
+ node,
46
+ messageId: 'unDocumentedProcessEnv',
47
+ data: { name: propName },
48
+ });
49
+ return;
50
+ }
51
+ if (matchedComment.length > SINGLE) {
52
+ context.report({
53
+ node,
54
+ messageId: 'duplicatedProcessEnv',
55
+ data: { name: propName },
56
+ });
57
+ return;
58
+ }
59
+ const regex = regular(propName);
60
+ let match = matchedComment[FIRST].value.match(regex);
61
+
62
+ if (match?.length > SINGLE) {
63
+ context.report({
64
+ node,
65
+ messageId: 'duplicatedProcessEnv',
66
+ data: { name: propName },
67
+ });
68
+ }
69
+ else {
70
+ match = regex.exec(matchedComment[FIRST].value);
71
+ if (!match[NO_DESCRIPTION]) {
72
+ context.report({
73
+ node,
74
+ messageId: 'unDescribedProcessEnv',
75
+ data: { name: propName },
37
76
  });
38
- if (!foundDocument) context.report({ data: { name: propName }, messageId: 'unDocumentedProcessEnv', node });
39
77
  }
40
78
  }
41
79
  },
42
80
  };
43
81
  },
44
82
  meta: {
83
+ type: 'problem',
45
84
  docs: {
46
85
  category: 'Node.js',
47
- description: 'Validate document for environment variables',
86
+ description: 'Validate documentation for environment variables.',
48
87
  recommended: false,
49
88
  },
89
+ schema: [],
50
90
  messages: {
51
91
  duplicatedProcessEnv: 'Duplicated definition for environment variable: {{ name }}',
52
- unDesribedProcessEnv: 'Undescribed environment variable: {{ name }}',
92
+ unDescribedProcessEnv: 'Undescribed environment variable: {{ name }}',
53
93
  unDocumentedProcessEnv: 'Undocumented environment variable: {{ name }}',
54
94
  },
55
- schema: [],
56
- type: 'problem',
57
95
  },
58
96
  },
59
97
  },
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@mimik/eslint-plugin-document-env",
3
- "version": "2.0.4",
3
+ "version": "2.0.6",
4
4
  "description": "validation of environement variable documentation",
5
5
  "main": "./index.js",
6
6
  "type": "module",
7
7
  "scripts": {
8
8
  "lint": "eslint . --no-error-on-unmatched-pattern",
9
- "test": "echo \"Error: no test specified\" && exit 0",
9
+ "test": "node --test test/*.test.js",
10
10
  "prepublishOnly": "npm run lint && npm run test"
11
11
  },
12
12
  "husky": {
@@ -26,13 +26,13 @@
26
26
  "license": "MIT",
27
27
  "repository": {
28
28
  "type": "git",
29
- "url": "https://bitbucket.org/mimiktech/eslit-plugin-document-env"
29
+ "url": "https://bitbucket.org/mimiktech/eslint-plugin-document-env"
30
30
  },
31
31
  "devDependencies": {
32
- "@eslint/js": "9.22.0",
33
- "@stylistic/eslint-plugin": "4.2.0",
34
- "eslint": "9.22.0",
35
- "eslint-plugin-import": "2.31.0",
32
+ "@eslint/js": "9.30.0",
33
+ "@stylistic/eslint-plugin": "5.1.0",
34
+ "eslint": "9.30.0",
35
+ "eslint-plugin-import": "2.32.0",
36
36
  "husky": "9.1.7"
37
37
  }
38
38
  }
@@ -0,0 +1,69 @@
1
+ import { RuleTester } from 'eslint';
2
+ import rule from '../index.js';
3
+
4
+ const tester = new RuleTester({
5
+ languageOptions: { ecmaVersion: 2022, sourceType: 'module' },
6
+ });
7
+
8
+ tester.run('validate-document-env', rule.rules['validate-document-env'], {
9
+ valid: [
10
+ {
11
+ code: `
12
+ /**
13
+ * | Env variable name | Description | Default | Comments |
14
+ * | ----------------- | ----------- | ------- | -------- |
15
+ * | MY_VAR | when existent on non null, will translate an IPV6 address into IPV4 address when possible | |
16
+ */
17
+ const v = process.env.MY_VAR;
18
+ `,
19
+ },
20
+ ],
21
+ invalid: [
22
+ {
23
+ code: `
24
+ const v = process.env.UNDOCUMENTED_VAR;
25
+ `,
26
+ errors: [{ messageId: 'unDocumentedProcessEnv' }],
27
+ },
28
+ {
29
+ code: `
30
+ /**
31
+ * | Env variable name | Description | Default | Comments |
32
+ * | ----------------- | ----------- | ------- | -------- |
33
+ * | UNDESCRIBED_VAR | | none | - |
34
+ */
35
+ const v = process.env.UNDESCRIBED_VAR;
36
+ `,
37
+ errors: [{ messageId: 'unDescribedProcessEnv' }],
38
+ },
39
+ {
40
+ code: `
41
+ /**
42
+ * | Env variable name | Description | Default | Comments |
43
+ * | ----------------- | ----------- | ------- | -------- |
44
+ * | DUP_VAR | a | none | - |
45
+ */
46
+ const separation = 'test';
47
+ /**
48
+ * | Env variable name | Description | Default | Comments |
49
+ * | ----------------- | ----------- | ------- | -------- |
50
+ * | DUP_VAR | b | none | - |
51
+ */
52
+ const v = process.env.DUP_VAR;
53
+ `,
54
+ errors: [{ messageId: 'duplicatedProcessEnv' }],
55
+ },
56
+ {
57
+ code: `
58
+ /**
59
+ * | Env variable name | Description | Default | Comments |
60
+ * | ----------------- | ----------- | ------- | -------- |
61
+ * | DUP_VAR | a | none | - |
62
+ * | DUP_VAR | b | none | - |
63
+ */
64
+ const v = process.env.DUP_VAR;
65
+ `,
66
+ errors: [{ messageId: 'duplicatedProcessEnv' }],
67
+ },
68
+ ],
69
+ });
package/.eslintrc DELETED
@@ -1,34 +0,0 @@
1
- {
2
- "env": {
3
- "node": true
4
- },
5
- "parserOptions": {
6
- "ecmaVersion": 2020
7
- },
8
- "extends": "airbnb",
9
- "rules": {
10
- "import/no-extraneous-dependencies": ["error", {"devDependencies": true}],
11
- "brace-style": [1, "stroustrup", {"allowSingleLine": true}],
12
- "no-confusing-arrow": [0], // arrow isnt confusing
13
- "max-len": [1, 180, { "ignoreComments": true }],
14
- "linebreak-style": 0,
15
- "quotes": [1, "single"],
16
- "semi": [1, "always"],
17
- "no-process-env": ["error"]
18
- },
19
- "settings":{
20
- "react": {
21
- "version": "detect"
22
- }
23
- },
24
- "globals": {
25
- "module": true,
26
- "require": true,
27
- "const": false,
28
- "it": false,
29
- "describe": false,
30
- "before": true,
31
- "after": true,
32
- "JSON": true
33
- }
34
- }