@lvce-editor/eslint-plugin-nvmrc 11.0.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.
Files changed (2) hide show
  1. package/dist/index.js +200 -0
  2. package/package.json +13 -0
package/dist/index.js ADDED
@@ -0,0 +1,200 @@
1
+ const createProgram = () => {
2
+ return {
3
+ type: 'Program',
4
+ body: [],
5
+ sourceType: 'script',
6
+ range: [0, 0],
7
+ loc: {
8
+ start: {
9
+ line: 1,
10
+ column: 0
11
+ },
12
+ end: {
13
+ line: 1,
14
+ column: 0
15
+ }
16
+ },
17
+ comments: [],
18
+ tokens: []
19
+ };
20
+ };
21
+ const parseForESLint = () => {
22
+ return {
23
+ ast: createProgram(),
24
+ services: {
25
+ isNvmrc: true
26
+ },
27
+ scopeManager: null,
28
+ visitorKeys: {
29
+ Program: ['body']
30
+ }
31
+ };
32
+ };
33
+ const meta$1 = {
34
+ name: 'nvmrc-parser',
35
+ version: '0.0.1'
36
+ };
37
+
38
+ const parser = {
39
+ __proto__: null,
40
+ meta: meta$1,
41
+ parseForESLint
42
+ };
43
+
44
+ const defaultMinimumVersion = '20.0.0';
45
+ const defaultMaximumVersion = '24.15.0';
46
+ const defaultBadVersions = ['24.16.0'];
47
+ const versionPattern = String.raw`^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$`;
48
+ const versionRegex = new RegExp(versionPattern);
49
+ const parseVersion = value => {
50
+ const match = versionRegex.exec(value.trim());
51
+ if (!match) {
52
+ return undefined;
53
+ }
54
+ return {
55
+ major: Number(match[1]),
56
+ minor: Number(match[2]),
57
+ patch: Number(match[3]),
58
+ text: `${Number(match[1])}.${Number(match[2])}.${Number(match[3])}`
59
+ };
60
+ };
61
+ const compareVersion = (a, b) => {
62
+ if (a.major !== b.major) {
63
+ return a.major - b.major;
64
+ }
65
+ if (a.minor !== b.minor) {
66
+ return a.minor - b.minor;
67
+ }
68
+ return a.patch - b.patch;
69
+ };
70
+ const getRuleOptions = context => {
71
+ const [options = {}] = context.options;
72
+ return {
73
+ minimumVersion: options.minimumVersion ?? defaultMinimumVersion,
74
+ maximumVersion: options.maximumVersion ?? defaultMaximumVersion,
75
+ badVersions: options.badVersions ?? defaultBadVersions
76
+ };
77
+ };
78
+ const meta = {
79
+ type: 'problem',
80
+ docs: {
81
+ description: 'Disallow invalid or unsupported .nvmrc versions'
82
+ },
83
+ schema: [{
84
+ type: 'object',
85
+ additionalProperties: false,
86
+ properties: {
87
+ minimumVersion: {
88
+ type: 'string',
89
+ pattern: versionPattern
90
+ },
91
+ maximumVersion: {
92
+ type: 'string',
93
+ pattern: versionPattern
94
+ },
95
+ badVersions: {
96
+ type: 'array',
97
+ items: {
98
+ type: 'string',
99
+ pattern: versionPattern
100
+ },
101
+ uniqueItems: true
102
+ }
103
+ }
104
+ }],
105
+ messages: {
106
+ invalidVersion: 'Invalid .nvmrc version: {{value}}',
107
+ versionTooOld: '.nvmrc version {{value}} is older than {{minimumVersion}}',
108
+ versionTooNew: '.nvmrc version {{value}} is newer than {{maximumVersion}}',
109
+ badVersion: '.nvmrc version {{value}} is not allowed'
110
+ }
111
+ };
112
+ const create = context => {
113
+ const parserServices = context.sourceCode.parserServices;
114
+ if (!parserServices?.isNvmrc) {
115
+ return {};
116
+ }
117
+ return {
118
+ Program(node) {
119
+ const value = context.sourceCode.text.trim();
120
+ const version = parseVersion(value);
121
+ if (!version) {
122
+ context.report({
123
+ node,
124
+ messageId: 'invalidVersion',
125
+ data: {
126
+ value
127
+ }
128
+ });
129
+ return;
130
+ }
131
+ const options = getRuleOptions(context);
132
+ const minimumVersion = parseVersion(options.minimumVersion) ?? parseVersion(defaultMinimumVersion);
133
+ const maximumVersion = parseVersion(options.maximumVersion) ?? parseVersion(defaultMaximumVersion);
134
+ const badVersions = new Set(options.badVersions.map(badVersion => parseVersion(badVersion)?.text ?? badVersion));
135
+ if (badVersions.has(version.text)) {
136
+ context.report({
137
+ node,
138
+ messageId: 'badVersion',
139
+ data: {
140
+ value: version.text
141
+ }
142
+ });
143
+ return;
144
+ }
145
+ if (compareVersion(version, minimumVersion) < 0) {
146
+ context.report({
147
+ node,
148
+ messageId: 'versionTooOld',
149
+ data: {
150
+ value: version.text,
151
+ minimumVersion: minimumVersion.text
152
+ }
153
+ });
154
+ return;
155
+ }
156
+ if (compareVersion(version, maximumVersion) > 0) {
157
+ context.report({
158
+ node,
159
+ messageId: 'versionTooNew',
160
+ data: {
161
+ value: version.text,
162
+ maximumVersion: maximumVersion.text
163
+ }
164
+ });
165
+ return;
166
+ }
167
+ }
168
+ };
169
+ };
170
+
171
+ const validVersion = {
172
+ __proto__: null,
173
+ create,
174
+ meta
175
+ };
176
+
177
+ const plugin = {
178
+ meta: {
179
+ name: 'nvmrc',
180
+ version: '0.0.1'
181
+ },
182
+ rules: {
183
+ 'valid-version': validVersion
184
+ },
185
+ configs: {}
186
+ };
187
+ const recommended = [{
188
+ files: ['**/.nvmrc'],
189
+ plugins: {
190
+ nvmrc: plugin
191
+ },
192
+ languageOptions: {
193
+ parser
194
+ },
195
+ rules: {
196
+ 'nvmrc/valid-version': 'error'
197
+ }
198
+ }];
199
+
200
+ export { recommended as default };
package/package.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "name": "@lvce-editor/eslint-plugin-nvmrc",
3
+ "version": "11.0.1",
4
+ "main": "dist/index.js",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/lvce-editor/eslint-config.git"
8
+ },
9
+ "type": "module",
10
+ "keywords": [],
11
+ "license": "MIT",
12
+ "description": "Lint .nvmrc files."
13
+ }