-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.ts
104 lines (96 loc) · 2.83 KB
/
options.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* @file This file contains code related to rule options.
*/
import { type JSONSchema4 } from "json-schema";
import * as path from "node:path";
/**
* The schema for rule options.
*/
export const schema: Array<JSONSchema4> = [
// The extension that rule will enforce.
{
type: "object",
properties: {
extension: {
type: "string",
},
checkAlsoType: {
type: "boolean",
},
knownExtensions: {
type: "array",
items: { type: "string" },
},
ignoreExtensions: {
type: "array",
items: { type: "string" },
},
},
minProperties: 0,
maxProperties: 3,
},
];
/**
* This type is runtime type of options for rules of this plugin, as represented by JSON schema {@link schema}.
*/
export type Options = Readonly<
Partial<{
extension: string;
checkAlsoType: boolean;
knownExtensions: ReadonlyArray<string>;
ignoreExtensions: ReadonlyArray<string>;
}>
>; // We could use "json-schema-to-ts" module here, but unfortunately that operates on JSON Schema 7, while ESLint operates on JSON Schema 4
/**
* The type to use when declaring {@link Options} for ESLint.
*/
export type ESLintOptions = Readonly<[Options]>;
/**
* The type after processing partial {@link Options} into full options.
*/
export type FullOptions = Readonly<{ [P in keyof Options]-?: Options[P] }>;
/**
* The default options for the rules.
*/
export const defaultOptions = {
checkAlsoType: false,
knownExtensions: [".js", ".ts", ".mjs", ".mts", ".cjs", ".cts"],
ignoreExtensions: [".json"],
} as const satisfies Options;
// eslint-disable-next-line jsdoc/require-param
/* eslint-disable jsdoc/check-param-names, jsdoc/require-param */
/**
* Helper function to get full options from ESLint partial options.
* @param filename The path of the file being processed.
* @param param1 The options.
* @returns Named options, using defaults if options partial object did not have element for that option.
*/
export const getOptions = (
filename: string,
[opts]: ESLintOptions,
): FullOptions => ({
extension:
opts?.extension ??
sourceFileExtensionToDefaultExtension[getExtName(filename)] ??
DEFAULT_EXTENSION,
checkAlsoType: opts?.checkAlsoType ?? defaultOptions.checkAlsoType,
knownExtensions: opts?.knownExtensions ?? defaultOptions.knownExtensions,
ignoreExtensions: opts?.ignoreExtensions ?? defaultOptions.ignoreExtensions,
});
/* eslint-enable jsdoc/check-param-names, jsdoc/require-param */
const DEFAULT_EXTENSION = ".js";
const sourceFileExtensionToDefaultExtension: Record<string, string> = {
".js": DEFAULT_EXTENSION,
".ts": DEFAULT_EXTENSION,
".mjs": ".mjs",
".mts": ".mjs",
".cjs": ".cjs",
".cts": ".cjs",
};
const getExtName = (filename: string) => {
try {
return path.extname(filename);
} catch {
return DEFAULT_EXTENSION;
}
};