-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathoptions.ts
More file actions
168 lines (151 loc) · 5.03 KB
/
options.ts
File metadata and controls
168 lines (151 loc) · 5.03 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { inspect } from 'node:util';
import type { ZodError } from 'zod';
import { z } from 'zod';
import type { RuntimeData } from './runtime-data';
import {
StringArraySchema,
StringBooleanSchema,
StringFalseSchema,
} from './schema-helper';
import { defined, twoOrMoreMap } from './util/types';
const DEFAULT_DECORATOR_CONFIG = {
inObjectLiterals: [],
};
const DecoratorOptionsSchema = z
.object({
inObjectLiterals: StringArraySchema.describe(
'A list of decorators that are allowed on object literal properties. (Method decorators will always be allowed.) When the codemod finds a field with one of these decorators, it will be translated directly into a class field with the same decorator. Including a decorator in this list means that you believe that the decorator will work correctly on a class field.'
),
})
.partial();
const DecoratorsSchema = z.preprocess(
(arg: unknown) => {
return arg === true || arg === 'true' ? DEFAULT_DECORATOR_CONFIG : arg;
},
z.union([DecoratorOptionsSchema, StringFalseSchema], {
errorMap: (issue, ctx) => {
if (issue.code === z.ZodIssueCode.invalid_union) {
return {
message: `Expected DecoratorOptions object or boolean, received ${inspect(
ctx.data
)}`,
};
}
return { message: ctx.defaultError };
},
})
);
const QuoteSchema = z.union([z.literal('single'), z.literal('double')], {
errorMap: (issue, ctx) => {
if (issue.code === z.ZodIssueCode.invalid_union) {
return {
message: `Expected 'single' or 'double', received ${inspect(ctx.data)}`,
};
}
return { message: ctx.defaultError };
},
});
export const TYPES = [
'adapters',
'components',
'controllers',
'helpers',
'routes',
'services',
] as const;
export type Type = (typeof TYPES)[number];
const TypeSchema = z.union(
twoOrMoreMap(TYPES, (type) => z.literal(type)),
{
errorMap: (issue, ctx) => {
if (issue.code === z.ZodIssueCode.invalid_union) {
const formattedTypes = TYPES.map((type) => `'${type}'`);
const expected = `${formattedTypes
.slice(0, -1)
.join(', ')}, or ${defined(
formattedTypes[formattedTypes.length - 1]
)}`;
return {
message: `Expected ${expected}, received ${inspect(ctx.data)}`,
};
}
return { message: ctx.defaultError };
},
}
);
export const UserOptionsSchema = z.object({
decorators: DecoratorsSchema.describe(
'Enable/disable transformation using decorators, or pass in DecoratorOptions'
),
classFields: StringBooleanSchema.describe(
'Enable/disable transformation using class fields'
),
classicDecorator: StringBooleanSchema.describe(
'Enable/disable adding the [`@classic` decorator](https://github.com/pzuraq/ember-classic-decorator), which helps with transitioning Ember Octane'
),
quote: QuoteSchema.describe(
'Whether to use double or single quotes by default for new statements that are added during the codemod.'
),
ignoreLeakingState: StringArraySchema.describe(
'Allow-list for ObjectExpression or ArrayExpression properties to ignore issues detailed in eslint-plugin-ember/avoid-leaking-state-in-ember-objects.'
),
type: TypeSchema.describe(
'Apply transformation to only passed type.'
).optional(),
moduleRoot: z.string().min(1).optional().describe('FIXME'),
packageBase: z.string().min(1).optional().describe('FIXME'),
});
export type UserOptions = z.infer<typeof UserOptionsSchema>;
const PartialUserOptionsSchema = UserOptionsSchema.partial();
export type PartialUserOptions = z.infer<typeof PartialUserOptionsSchema>;
/**
* Parses a raw config from a given source to ensure compliance with the
* PartialUserOptionsSchema.
*
* If the raw config does not parse correctly, will log an error message and
* return an empty config object.
*
* @param source Source annotation for error messages.
* @param raw Raw config.
*/
export function parseConfig(source: string, raw: unknown): PartialUserOptions {
const parsed = PartialUserOptionsSchema.safeParse(raw);
let options = {};
if (parsed.success) {
options = parsed.data;
} else {
logConfigError(source, parsed.error);
return {};
}
return options;
}
function logConfigError(
source: string,
error: ZodError<PartialUserOptions>
): void {
const flattened = error.flatten();
const errors = flattened.formErrors;
for (const [key, value] of Object.entries(flattened.fieldErrors)) {
errors.push(`[${key}] ${value.join('; ')}`);
}
const message = errors.join('\n\t');
throw new ConfigError(`${source} Config Error\n\t${message}`);
}
interface PrivateOptions {
/** @private */
runtimeData: RuntimeData;
}
export type Options = UserOptions & PrivateOptions;
export const DEFAULT_OPTIONS: UserOptions = {
decorators: DEFAULT_DECORATOR_CONFIG,
classFields: true,
classicDecorator: true,
quote: 'single',
ignoreLeakingState: ['queryParams'],
};
class ConfigError extends Error {
constructor(message: string) {
super(message);
this.name = 'ConfigError';
}
}