@virmator/lint 14.35.2 → 14.36.1

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.
@@ -6,9 +6,9 @@ import sonarJsEslint from 'eslint-plugin-sonarjs';
6
6
  import eslintPluginUnicorn from 'eslint-plugin-unicorn';
7
7
  import globals from 'globals';
8
8
  import tsEslint from 'typescript-eslint';
9
+ import allowedFileExtensionsRule from '../src/rules/allowed-file-extensions.lint.js';
9
10
  import assertionsInTestsRule from '../src/rules/assertions-in-tests.lint.js';
10
11
  import errorNameAsClassFieldRule from '../src/rules/error-name-as-class-field.lint.js';
11
- import knownFileSuffixesRule from '../src/rules/known-file-suffixes.lint.js';
12
12
  import noJsdocCategoryRule from '../src/rules/no-jsdoc-category.lint.js';
13
13
  import noRawDateRule from '../src/rules/no-raw-date.lint.js';
14
14
  import noReadonlyPrimitiveRule from '../src/rules/no-readonly-primitive.lint.js';
@@ -34,18 +34,42 @@ import requireImportExtensionsRule from '../src/rules/require-import-extensions.
34
34
  import singleCspellWordsCommentRule from '../src/rules/single-cspell-words-comment.lint.js';
35
35
 
36
36
  /**
37
- * File name suffixes that any repo using this config may use. A repo with more of its own adds them
38
- * by overriding `@virmator/known-file-suffixes` with this list spread into its own.
37
+ * File extensions that any repo using this config may use. A repo with more of its own adds them by
38
+ * overriding `@virmator/allowed-file-extensions` with this list spread into its own.
39
39
  */
40
- export const defaultAllowedFileSuffixes = [
41
- 'config',
42
- 'e2e',
43
- 'element',
44
- 'error',
45
- 'example',
46
- 'mock',
47
- 'script',
48
- 'test',
40
+ export const defaultAllowedFileExtensions = [
41
+ '.api.ts',
42
+ '.book.ts',
43
+ '.client.ts',
44
+ '.config.mjs',
45
+ '.config.ts',
46
+ '.e2e.ts',
47
+ '.element.book.ts',
48
+ '.element.example.ts',
49
+ '.element.mock.ts',
50
+ '.element.test.e2e.ts',
51
+ '.element.test.ts',
52
+ '.element.ts',
53
+ '.endpoint.implementation.ts',
54
+ '.endpoint.test.ts',
55
+ '.endpoint.ts',
56
+ '.error.test.ts',
57
+ '.error.ts',
58
+ '.example.ts',
59
+ '.js',
60
+ '.mock.script.ts',
61
+ '.mock.test.ts',
62
+ '.mock.ts',
63
+ '.script.mock.ts',
64
+ '.script.test.ts',
65
+ '.script.ts',
66
+ '.test.e2e.ts',
67
+ '.test.node.ts',
68
+ '.test.ts.snapshot.web.mjs',
69
+ '.test.ts.snapshot',
70
+ '.test.ts',
71
+ '.test.web.ts',
72
+ '.ts',
49
73
  ];
50
74
 
51
75
  export const globalVars = {
@@ -98,7 +122,7 @@ export function defineEslintConfig(repoDir: string) {
98
122
  rules: {
99
123
  'assertions-in-tests': assertionsInTestsRule,
100
124
  'error-name-as-class-field': errorNameAsClassFieldRule,
101
- 'known-file-suffixes': knownFileSuffixesRule,
125
+ 'allowed-file-extensions': allowedFileExtensionsRule,
102
126
  'no-jsdoc-category': noJsdocCategoryRule,
103
127
  'no-raw-date': noRawDateRule,
104
128
  'no-readonly-primitive': noReadonlyPrimitiveRule,
@@ -226,10 +250,10 @@ export function defineEslintConfig(repoDir: string) {
226
250
  '@typescript-eslint/no-unused-vars': 'error',
227
251
  '@virmator/assertions-in-tests': 'error',
228
252
  '@virmator/error-name-as-class-field': 'error',
229
- '@virmator/known-file-suffixes': [
253
+ '@virmator/allowed-file-extensions': [
230
254
  'error',
231
255
  {
232
- suffixes: defaultAllowedFileSuffixes,
256
+ extensions: defaultAllowedFileExtensions,
233
257
  },
234
258
  ],
235
259
  /**
@@ -0,0 +1,93 @@
1
+ import { check } from '@augment-vir/assert';
2
+ import { basename } from 'node:path';
3
+ const numberedExtensionRegExp = /-\d+(?=\.|$)/g;
4
+ /** Extracts everything after the first extension separator in a file name. */
5
+ function extractFileExtension(filePath) {
6
+ const fileName = basename(filePath).replace(/^\.+/, '');
7
+ const firstPeriodIndex = fileName.indexOf('.');
8
+ return firstPeriodIndex === -1 ? undefined : fileName.slice(firstPeriodIndex);
9
+ }
10
+ function matchesAllowedExtension({ allowedExtension, extension, }) {
11
+ return check.isString(allowedExtension)
12
+ ? allowedExtension === extension
13
+ : allowedExtension instanceof RegExp && new RegExp(allowedExtension).test(extension);
14
+ }
15
+ const rule = {
16
+ meta: {
17
+ type: 'suggestion',
18
+ docs: {
19
+ description: 'Require file extensions to come from an allowed list.',
20
+ },
21
+ messages: {
22
+ unknownExtension: "Unknown file extension `{{extension}}`. Allowed file extensions: {{allowedExtensions}}. Add this extension to the rule's `extensions` option if it is intentional.",
23
+ },
24
+ schema: [
25
+ {
26
+ type: 'object',
27
+ required: [
28
+ 'extensions',
29
+ ],
30
+ properties: {
31
+ extensions: {
32
+ type: 'array',
33
+ items: {
34
+ anyOf: [
35
+ {
36
+ type: 'string',
37
+ pattern: String.raw `^\.`,
38
+ },
39
+ {
40
+ type: 'object',
41
+ },
42
+ ],
43
+ },
44
+ },
45
+ /**
46
+ * Allows a `-<number>` counter in an extension, such as
47
+ * `element-2.test.e2e.ts`.
48
+ */
49
+ allowNumberedExtensions: {
50
+ type: 'boolean',
51
+ },
52
+ },
53
+ additionalProperties: false,
54
+ },
55
+ ],
56
+ },
57
+ create(context) {
58
+ const options = context.options[0] ?? {};
59
+ const allowedExtensions = options.extensions ?? [];
60
+ return {
61
+ Program() {
62
+ const fileExtension = extractFileExtension(context.filename);
63
+ if (!fileExtension) {
64
+ return;
65
+ }
66
+ const normalizedExtension = options.allowNumberedExtensions
67
+ ? fileExtension.replace(numberedExtensionRegExp, '')
68
+ : fileExtension;
69
+ if (allowedExtensions.some((allowedExtension) => {
70
+ return matchesAllowedExtension({
71
+ allowedExtension,
72
+ extension: normalizedExtension,
73
+ });
74
+ })) {
75
+ return;
76
+ }
77
+ context.report({
78
+ /** The file name is the problem, so the report is anchored to its first line. */
79
+ loc: {
80
+ line: 1,
81
+ column: 0,
82
+ },
83
+ messageId: 'unknownExtension',
84
+ data: {
85
+ extension: normalizedExtension,
86
+ allowedExtensions: allowedExtensions.join(', '),
87
+ },
88
+ });
89
+ },
90
+ };
91
+ },
92
+ };
93
+ export default rule;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@virmator/lint",
3
- "version": "14.35.2",
3
+ "version": "14.36.1",
4
4
  "description": "Default ESLint linting plugin for virmator.",
5
5
  "keywords": [
6
6
  "virmator",
@@ -35,8 +35,8 @@
35
35
  "@augment-vir/assert": "^32.2.4",
36
36
  "@augment-vir/common": "^32.2.4",
37
37
  "@augment-vir/node": "^32.2.4",
38
- "@virmator/core": "^14.35.2",
39
- "globals": "^17.11.0",
38
+ "@virmator/core": "^14.36.1",
39
+ "globals": "^17.12.0",
40
40
  "jiti": "^2.7.0",
41
41
  "mri": "^1.2.0"
42
42
  },
@@ -46,18 +46,18 @@
46
46
  "@stylistic/eslint-plugin": "^5.10.0",
47
47
  "@stylistic/eslint-plugin-ts": "^4.4.1",
48
48
  "@types/estree": "^1.0.9",
49
- "@types/node": "^26.4.0",
50
- "@typescript-eslint/eslint-plugin": "^8.68.0",
49
+ "@types/node": "^26.4.1",
50
+ "@typescript-eslint/eslint-plugin": "^8.69.0",
51
51
  "c8": "^12.0.0",
52
- "eslint": "^10.9.1",
52
+ "eslint": "^10.10.0",
53
53
  "eslint-config-prettier": "^10.1.8",
54
- "eslint-plugin-jsdoc": "^64.3.2",
54
+ "eslint-plugin-jsdoc": "^64.3.6",
55
55
  "eslint-plugin-playwright": "^2.11.0",
56
56
  "eslint-plugin-prettier": "^5.5.6",
57
57
  "eslint-plugin-sonarjs": "^4.2.0",
58
58
  "eslint-plugin-unicorn": "^74.0.0",
59
59
  "istanbul-smart-text-reporter": "^1.1.5",
60
- "typescript-eslint": "^8.68.0"
60
+ "typescript-eslint": "^8.69.0"
61
61
  },
62
62
  "engines": {
63
63
  "node": ">=22"
@@ -0,0 +1,114 @@
1
+ import {check} from '@augment-vir/assert';
2
+ import {type Rule} from 'eslint';
3
+ import {basename} from 'node:path';
4
+
5
+ const numberedExtensionRegExp = /-\d+(?=\.|$)/g;
6
+
7
+ /** Extracts everything after the first extension separator in a file name. */
8
+ function extractFileExtension(filePath: string) {
9
+ const fileName = basename(filePath).replace(/^\.+/, '');
10
+ const firstPeriodIndex = fileName.indexOf('.');
11
+
12
+ return firstPeriodIndex === -1 ? undefined : fileName.slice(firstPeriodIndex);
13
+ }
14
+
15
+ function matchesAllowedExtension({
16
+ allowedExtension,
17
+ extension,
18
+ }: Readonly<{
19
+ allowedExtension: unknown;
20
+ extension: string;
21
+ }>) {
22
+ return check.isString(allowedExtension)
23
+ ? allowedExtension === extension
24
+ : allowedExtension instanceof RegExp && new RegExp(allowedExtension).test(extension);
25
+ }
26
+
27
+ const rule: Rule.RuleModule = {
28
+ meta: {
29
+ type: 'suggestion',
30
+ docs: {
31
+ description: 'Require file extensions to come from an allowed list.',
32
+ },
33
+ messages: {
34
+ unknownExtension:
35
+ "Unknown file extension `{{extension}}`. Allowed file extensions: {{allowedExtensions}}. Add this extension to the rule's `extensions` option if it is intentional.",
36
+ },
37
+ schema: [
38
+ {
39
+ type: 'object',
40
+ required: [
41
+ 'extensions',
42
+ ],
43
+ properties: {
44
+ extensions: {
45
+ type: 'array',
46
+ items: {
47
+ anyOf: [
48
+ {
49
+ type: 'string',
50
+ pattern: String.raw`^\.`,
51
+ },
52
+ {
53
+ type: 'object',
54
+ },
55
+ ],
56
+ },
57
+ },
58
+ /**
59
+ * Allows a `-<number>` counter in an extension, such as
60
+ * `element-2.test.e2e.ts`.
61
+ */
62
+ allowNumberedExtensions: {
63
+ type: 'boolean',
64
+ },
65
+ },
66
+ additionalProperties: false,
67
+ },
68
+ ],
69
+ },
70
+ create(context) {
71
+ const options = context.options[0] ?? {};
72
+ const allowedExtensions: ReadonlyArray<unknown> = options.extensions ?? [];
73
+
74
+ return {
75
+ Program() {
76
+ const fileExtension = extractFileExtension(context.filename);
77
+
78
+ if (!fileExtension) {
79
+ return;
80
+ }
81
+
82
+ const normalizedExtension = options.allowNumberedExtensions
83
+ ? fileExtension.replace(numberedExtensionRegExp, '')
84
+ : fileExtension;
85
+
86
+ if (
87
+ allowedExtensions.some((allowedExtension) => {
88
+ return matchesAllowedExtension({
89
+ allowedExtension,
90
+ extension: normalizedExtension,
91
+ });
92
+ })
93
+ ) {
94
+ return;
95
+ }
96
+
97
+ context.report({
98
+ /** The file name is the problem, so the report is anchored to its first line. */
99
+ loc: {
100
+ line: 1,
101
+ column: 0,
102
+ },
103
+ messageId: 'unknownExtension',
104
+ data: {
105
+ extension: normalizedExtension,
106
+ allowedExtensions: allowedExtensions.join(', '),
107
+ },
108
+ });
109
+ },
110
+ };
111
+ },
112
+ };
113
+
114
+ export default rule;
@@ -1,101 +0,0 @@
1
- import { basename } from 'node:path';
2
- const numberedSuffixRegExp = /-\d+$/;
3
- /**
4
- * The suffix segments of a file name: everything between its first segment (the name itself) and
5
- * its extension. A leading dot is part of the name rather than a segment separator, so
6
- * `.eslintrc.ts` has no suffixes.
7
- */
8
- function extractSuffixes(filePath) {
9
- const fileName = basename(filePath).replace(/^\.+/, '');
10
- return fileName.split('.').slice(1, -1);
11
- }
12
- /**
13
- * Every chain of suffixes that ends at the given index, longest first: `['config', 'base']` at
14
- * index `1` produces `'config.base'` then `'base'`. A dotted entry in the `suffixes` option
15
- * therefore allows its last suffix only directly after the ones before it, so listing `config.base`
16
- * allows `thing.config.base.ts` while still rejecting `thing.base.ts`.
17
- */
18
- function listSuffixChains({ suffixes, suffixIndex, }) {
19
- return suffixes.slice(0, suffixIndex + 1).map((currentSuffix, startIndex) => {
20
- return suffixes.slice(startIndex, suffixIndex + 1).join('.');
21
- });
22
- }
23
- const rule = {
24
- meta: {
25
- type: 'suggestion',
26
- docs: {
27
- description: 'Require file name suffixes to come from a known list.',
28
- },
29
- messages: {
30
- unknownSuffix: "Unknown file name suffix `.{{suffix}}`. Known suffixes: {{knownSuffixes}}. Add this suffix to the rule's `suffixes` option if it is intentional.",
31
- },
32
- schema: [
33
- {
34
- type: 'object',
35
- required: [
36
- 'suffixes',
37
- ],
38
- properties: {
39
- suffixes: {
40
- type: 'array',
41
- items: {
42
- type: 'string',
43
- },
44
- },
45
- /**
46
- * Allows a `-<number>` counter on a suffix, for splitting a single slow test
47
- * file into numbered companions (`my-thing.element-2.test.ts`).
48
- */
49
- allowNumberedSuffixes: {
50
- type: 'boolean',
51
- },
52
- },
53
- additionalProperties: false,
54
- },
55
- ],
56
- },
57
- create(context) {
58
- const options = context.options[0] ?? {};
59
- const knownSuffixes = options.suffixes ?? [];
60
- return {
61
- Program() {
62
- const suffixes = extractSuffixes(context.filename).map((suffix) => {
63
- return options.allowNumberedSuffixes
64
- ? suffix.replace(numberedSuffixRegExp, '')
65
- : suffix;
66
- });
67
- suffixes.forEach((suffix, suffixIndex) => {
68
- const isKnown = listSuffixChains({
69
- suffixes,
70
- suffixIndex,
71
- }).some((suffixChain) => {
72
- return knownSuffixes.includes(suffixChain);
73
- });
74
- if (isKnown) {
75
- return;
76
- }
77
- context.report({
78
- /**
79
- * The file name is the problem, so the report is anchored to its first
80
- * line.
81
- */
82
- loc: {
83
- line: 1,
84
- column: 0,
85
- },
86
- messageId: 'unknownSuffix',
87
- data: {
88
- suffix,
89
- knownSuffixes: knownSuffixes
90
- .map((knownSuffix) => {
91
- return `.${knownSuffix}`;
92
- })
93
- .join(', '),
94
- },
95
- });
96
- });
97
- },
98
- };
99
- },
100
- };
101
- export default rule;
@@ -1,119 +0,0 @@
1
- import {type Rule} from 'eslint';
2
- import {basename} from 'node:path';
3
-
4
- const numberedSuffixRegExp = /-\d+$/;
5
-
6
- /**
7
- * The suffix segments of a file name: everything between its first segment (the name itself) and
8
- * its extension. A leading dot is part of the name rather than a segment separator, so
9
- * `.eslintrc.ts` has no suffixes.
10
- */
11
- function extractSuffixes(filePath: string) {
12
- const fileName = basename(filePath).replace(/^\.+/, '');
13
-
14
- return fileName.split('.').slice(1, -1);
15
- }
16
-
17
- /**
18
- * Every chain of suffixes that ends at the given index, longest first: `['config', 'base']` at
19
- * index `1` produces `'config.base'` then `'base'`. A dotted entry in the `suffixes` option
20
- * therefore allows its last suffix only directly after the ones before it, so listing `config.base`
21
- * allows `thing.config.base.ts` while still rejecting `thing.base.ts`.
22
- */
23
- function listSuffixChains({
24
- suffixes,
25
- suffixIndex,
26
- }: Readonly<{
27
- suffixes: ReadonlyArray<string>;
28
- suffixIndex: number;
29
- }>) {
30
- return suffixes.slice(0, suffixIndex + 1).map((currentSuffix, startIndex) => {
31
- return suffixes.slice(startIndex, suffixIndex + 1).join('.');
32
- });
33
- }
34
-
35
- const rule: Rule.RuleModule = {
36
- meta: {
37
- type: 'suggestion',
38
- docs: {
39
- description: 'Require file name suffixes to come from a known list.',
40
- },
41
- messages: {
42
- unknownSuffix:
43
- "Unknown file name suffix `.{{suffix}}`. Known suffixes: {{knownSuffixes}}. Add this suffix to the rule's `suffixes` option if it is intentional.",
44
- },
45
- schema: [
46
- {
47
- type: 'object',
48
- required: [
49
- 'suffixes',
50
- ],
51
- properties: {
52
- suffixes: {
53
- type: 'array',
54
- items: {
55
- type: 'string',
56
- },
57
- },
58
- /**
59
- * Allows a `-<number>` counter on a suffix, for splitting a single slow test
60
- * file into numbered companions (`my-thing.element-2.test.ts`).
61
- */
62
- allowNumberedSuffixes: {
63
- type: 'boolean',
64
- },
65
- },
66
- additionalProperties: false,
67
- },
68
- ],
69
- },
70
- create(context) {
71
- const options = context.options[0] ?? {};
72
- const knownSuffixes: string[] = options.suffixes ?? [];
73
-
74
- return {
75
- Program() {
76
- const suffixes = extractSuffixes(context.filename).map((suffix) => {
77
- return options.allowNumberedSuffixes
78
- ? suffix.replace(numberedSuffixRegExp, '')
79
- : suffix;
80
- });
81
-
82
- suffixes.forEach((suffix, suffixIndex) => {
83
- const isKnown = listSuffixChains({
84
- suffixes,
85
- suffixIndex,
86
- }).some((suffixChain) => {
87
- return knownSuffixes.includes(suffixChain);
88
- });
89
-
90
- if (isKnown) {
91
- return;
92
- }
93
-
94
- context.report({
95
- /**
96
- * The file name is the problem, so the report is anchored to its first
97
- * line.
98
- */
99
- loc: {
100
- line: 1,
101
- column: 0,
102
- },
103
- messageId: 'unknownSuffix',
104
- data: {
105
- suffix,
106
- knownSuffixes: knownSuffixes
107
- .map((knownSuffix) => {
108
- return `.${knownSuffix}`;
109
- })
110
- .join(', '),
111
- },
112
- });
113
- });
114
- },
115
- };
116
- },
117
- };
118
-
119
- export default rule;