Skip to content

Commit 7501b24

Browse files
test: update
1 parent c3c171b commit 7501b24

File tree

5 files changed

+147
-80
lines changed

5 files changed

+147
-80
lines changed

bin/process-arguments.js

Lines changed: 86 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,20 @@ const path = require('path');
88
const cliAddedItems = new WeakMap();
99

1010
const getObjectAndProperty = (config, schemaPath, index = 0) => {
11-
if (!schemaPath) return { value: config };
11+
if (!schemaPath) {
12+
return { value: config };
13+
}
14+
1215
const parts = schemaPath.split('.');
1316
const property = parts.pop();
1417
let current = config;
1518
let i = 0;
19+
1620
for (const part of parts) {
1721
const isArray = part.endsWith('[]');
1822
const name = isArray ? part.slice(0, -2) : part;
1923
let value = current[name];
24+
2025
if (isArray) {
2126
// eslint-disable-next-line no-undefined
2227
if (value === undefined) {
@@ -32,14 +37,18 @@ const getObjectAndProperty = (config, schemaPath, index = 0) => {
3237
};
3338
} else {
3439
let addedItems = cliAddedItems.get(value) || 0;
40+
3541
while (addedItems <= index) {
3642
// eslint-disable-next-line no-undefined
3743
value.push(undefined);
3844
// eslint-disable-next-line no-plusplus
3945
addedItems++;
4046
}
47+
4148
cliAddedItems.set(value, addedItems);
49+
4250
const x = value.length - addedItems + index;
51+
4352
// eslint-disable-next-line no-undefined
4453
if (value[x] === undefined) {
4554
value[x] = {};
@@ -51,6 +60,7 @@ const getObjectAndProperty = (config, schemaPath, index = 0) => {
5160
},
5261
};
5362
}
63+
5464
value = value[x];
5565
}
5666
// eslint-disable-next-line no-undefined
@@ -65,37 +75,49 @@ const getObjectAndProperty = (config, schemaPath, index = 0) => {
6575
},
6676
};
6777
}
78+
6879
current = value;
6980
// eslint-disable-next-line no-plusplus
7081
i++;
7182
}
83+
7284
const value = current[property];
85+
7386
if (property.endsWith('[]')) {
7487
const name = property.slice(0, -2);
88+
// eslint-disable-next-line no-shadow
7589
const value = current[name];
90+
7691
// eslint-disable-next-line no-undefined
7792
if (value === undefined) {
7893
// eslint-disable-next-line no-undefined
7994
current[name] = [...Array.from({ length: index }), undefined];
8095
cliAddedItems.set(current[name], index + 1);
96+
8197
// eslint-disable-next-line no-undefined
8298
return { object: current[name], property: index, value: undefined };
8399
} else if (!Array.isArray(value)) {
84100
// eslint-disable-next-line no-undefined
85101
current[name] = [value, ...Array.from({ length: index }), undefined];
86102
cliAddedItems.set(current[name], index + 1);
103+
87104
// eslint-disable-next-line no-undefined
88105
return { object: current[name], property: index + 1, value: undefined };
89106
}
107+
90108
let addedItems = cliAddedItems.get(value) || 0;
109+
91110
while (addedItems <= index) {
92111
// eslint-disable-next-line no-undefined
93112
value.push(undefined);
94113
// eslint-disable-next-line no-plusplus
95114
addedItems++;
96115
}
116+
97117
cliAddedItems.set(value, addedItems);
118+
98119
const x = value.length - addedItems + index;
120+
99121
// eslint-disable-next-line no-undefined
100122
if (value[x] === undefined) {
101123
value[x] = {};
@@ -107,12 +129,14 @@ const getObjectAndProperty = (config, schemaPath, index = 0) => {
107129
},
108130
};
109131
}
132+
110133
return {
111134
object: value,
112135
property: x,
113136
value: value[x],
114137
};
115138
}
139+
116140
return { object: current, property, value };
117141
};
118142

@@ -130,35 +154,60 @@ const parseValueForArgumentConfig = (argConfig, value) => {
130154
}
131155
break;
132156
case 'number':
133-
if (typeof value === 'number') return value;
157+
if (typeof value === 'number') {
158+
return value;
159+
}
160+
134161
if (typeof value === 'string' && /^[+-]?\d*(\.\d*)[eE]\d+$/) {
135162
const n = +value;
136163
if (!isNaN(n)) return n;
137164
}
165+
138166
break;
139167
case 'boolean':
140-
if (typeof value === 'boolean') return value;
141-
if (value === 'true') return true;
142-
if (value === 'false') return false;
168+
if (typeof value === 'boolean') {
169+
return value;
170+
}
171+
172+
if (value === 'true') {
173+
return true;
174+
}
175+
176+
if (value === 'false') {
177+
return false;
178+
}
179+
143180
break;
144181
case 'RegExp':
145-
if (value instanceof RegExp) return value;
182+
if (value instanceof RegExp) {
183+
return value;
184+
}
185+
146186
if (typeof value === 'string') {
147187
// cspell:word yugi
148188
const match = /^\/(.*)\/([yugi]*)$/.exec(value);
189+
149190
if (match && !/[^\\]\//.test(match[1])) {
150191
return new RegExp(match[1], match[2]);
151192
}
152193
}
194+
153195
break;
154196
case 'enum':
155-
if (argConfig.values.includes(value)) return value;
197+
if (argConfig.values.includes(value)) {
198+
return value;
199+
}
200+
156201
for (const item of argConfig.values) {
157202
if (`${item}` === value) return item;
158203
}
204+
159205
break;
160206
case 'reset':
161-
if (value === true) return [];
207+
if (value === true) {
208+
return [];
209+
}
210+
162211
break;
163212
}
164213
};
@@ -184,8 +233,13 @@ const setValue = (config, schemaPath, value, index) => {
184233
schemaPath,
185234
index
186235
);
187-
if (problem) return problem;
236+
237+
if (problem) {
238+
return problem;
239+
}
240+
188241
object[property] = value;
242+
189243
return null;
190244
};
191245

@@ -197,7 +251,9 @@ const processArgumentConfig = (argConfig, config, value, index) => {
197251
path: argConfig.path,
198252
};
199253
}
254+
200255
const parsed = parseValueForArgumentConfig(argConfig, value);
256+
201257
// eslint-disable-next-line no-undefined
202258
if (parsed === undefined) {
203259
return {
@@ -206,41 +262,56 @@ const processArgumentConfig = (argConfig, config, value, index) => {
206262
expected: getExpectedValue(argConfig),
207263
};
208264
}
265+
209266
const problem = setValue(config, argConfig.path, parsed, index);
210-
if (problem) return problem;
267+
268+
if (problem) {
269+
return problem;
270+
}
271+
211272
return null;
212273
};
213274

214275
const processArguments = (args, config, values) => {
215276
const problems = [];
277+
216278
for (const key of Object.keys(values)) {
217279
const arg = args[key];
280+
218281
if (!arg) {
219282
problems.push({
220283
type: 'unknown-argument',
221284
path: '',
222285
argument: key,
223286
});
287+
224288
// eslint-disable-next-line no-continue
225289
continue;
226290
}
291+
227292
const processValue = (value, i) => {
228293
const currentProblems = [];
294+
229295
for (const argConfig of arg.configs) {
230296
const problem = processArgumentConfig(argConfig, config, value, i);
297+
231298
if (!problem) {
232299
return;
233300
}
301+
234302
currentProblems.push({
235303
...problem,
236304
argument: key,
237305
value,
238306
index: i,
239307
});
240308
}
309+
241310
problems.push(...currentProblems);
242311
};
312+
243313
const value = values[key];
314+
244315
if (Array.isArray(value)) {
245316
for (let i = 0; i < value.length; i++) {
246317
processValue(value[i], i);
@@ -250,7 +321,11 @@ const processArguments = (args, config, values) => {
250321
processValue(value, undefined);
251322
}
252323
}
253-
if (problems.length === 0) return null;
324+
325+
if (problems.length === 0) {
326+
return null;
327+
}
328+
254329
return problems;
255330
};
256331

jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module.exports = {
88
'<rootDir>/test/',
99
'<rootDir>/client/',
1010
],
11+
testPathIgnorePatterns: ['<rootDir>/bin/this/process-arguments.js'],
1112
moduleFileExtensions: ['js', 'json'],
1213
testMatch: ['**/test/**/*.test.js'],
1314
snapshotResolver: '<rootDir>/test/helpers/snapshotResolver.js',

0 commit comments

Comments
 (0)