-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
jsonSchema.ts
208 lines (195 loc) · 5.75 KB
/
jsonSchema.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import type { JSONSchema4 } from '@typescript-eslint/utils/json-schema';
import type * as ts from 'typescript';
import type { CreateLinter } from '../linter/createLinter';
const defaultRuleSchema: JSONSchema4 = {
type: ['string', 'number'],
enum: ['off', 'warn', 'error', 0, 1, 2],
};
// https://github.com/microsoft/TypeScript/issues/17002
function isArray(arg: unknown): arg is readonly unknown[] {
return Array.isArray(arg);
}
/**
* Add the error level to the rule schema items
*
* if you encounter issues with rule schema validation you can check the schema by using the following code in the console:
* monaco.languages.json.jsonDefaults.diagnosticsOptions.schemas.find(item => item.uri.includes('typescript-eslint/consistent-type-imports'))
* monaco.languages.json.jsonDefaults.diagnosticsOptions.schemas.find(item => item.uri.includes('no-unused-labels'))
* monaco.languages.json.jsonDefaults.diagnosticsOptions.schemas.filter(item => item.schema.type === 'array')
*/
export function getRuleJsonSchemaWithErrorLevel(
name: string,
ruleSchema: JSONSchema4 | readonly JSONSchema4[],
): JSONSchema4 {
if (isArray(ruleSchema)) {
const defaultRuleSchemaCopy = { ...defaultRuleSchema };
if (ruleSchema[0]?.$defs) {
defaultRuleSchemaCopy.$defs = ruleSchema[0].$defs;
}
return {
type: 'array',
items: [defaultRuleSchemaCopy, ...ruleSchema],
minItems: 1,
additionalItems: false,
};
}
if ('items' in ruleSchema) {
// example: explicit-member-accessibility
if (isArray(ruleSchema.items)) {
return {
...ruleSchema,
type: 'array',
items: [defaultRuleSchema, ...ruleSchema.items],
maxItems: ruleSchema.maxItems ? ruleSchema.maxItems + 1 : undefined,
minItems: ruleSchema.minItems ? ruleSchema.minItems + 1 : 1,
additionalItems: false,
};
}
// example: naming-convention rule
if (typeof ruleSchema.items === 'object' && ruleSchema.items) {
return {
...ruleSchema,
type: 'array',
items: [defaultRuleSchema],
maxItems: ruleSchema.maxItems ? ruleSchema.maxItems + 1 : undefined,
minItems: ruleSchema.minItems ? ruleSchema.minItems + 1 : 1,
additionalItems: ruleSchema.items,
};
}
}
// example eqeqeq
if (isArray(ruleSchema.anyOf)) {
return {
...ruleSchema,
anyOf: ruleSchema.anyOf.map(item =>
getRuleJsonSchemaWithErrorLevel(name, item),
),
};
}
// example logical-assignment-operators
if (isArray(ruleSchema.oneOf)) {
return {
...ruleSchema,
oneOf: ruleSchema.oneOf.map(item =>
getRuleJsonSchemaWithErrorLevel(name, item),
),
};
}
if (typeof ruleSchema !== 'object' || Object.keys(ruleSchema).length) {
console.error('unsupported rule schema', name, ruleSchema);
}
return {
type: 'array',
items: [defaultRuleSchema],
minItems: 1,
additionalItems: false,
};
}
/**
* Get the JSON schema for the eslint config
* Currently we only support the rules and extends
*/
export function getEslintJsonSchema(
linter: CreateLinter,
createRef: (name: string) => string,
): JSONSchema4 {
const properties: Record<string, JSONSchema4> = {};
for (const [, item] of linter.rules) {
properties[item.name] = {
description: `${item.description}\n ${item.url}`,
title: item.name.startsWith('@typescript') ? 'Rules' : 'Core rules',
default: 'off',
oneOf: [defaultRuleSchema, { $ref: createRef(item.name) }],
};
}
return {
type: 'object',
properties: {
extends: {
oneOf: [
{ type: 'string' },
{
type: 'array',
items: { type: 'string', enum: linter.configs },
uniqueItems: true,
},
],
},
rules: {
type: 'object',
properties: properties,
additionalProperties: false,
},
},
};
}
/**
* Get all typescript options, except for the ones that are not supported by the playground
* this function uses private API from typescript, and this might break in the future
*/
export function getTypescriptOptions(): ts.OptionDeclarations[] {
const allowedCategories = [
'Command-line Options',
'Projects',
'Compiler Diagnostics',
'Editor Support',
'Output Formatting',
'Watch and Build Modes',
'Source Map Options',
];
const filteredNames = [
'moduleResolution',
'moduleDetection',
'plugins',
'typeRoots',
'jsx',
];
return window.ts.optionDeclarations.filter(
item =>
(item.type === 'boolean' ||
item.type === 'list' ||
item.type instanceof Map) &&
item.description &&
item.category &&
!allowedCategories.includes(item.category.message) &&
!filteredNames.includes(item.name),
);
}
/**
* Get the JSON schema for the typescript config
*/
export function getTypescriptJsonSchema(): JSONSchema4 {
const properties = getTypescriptOptions().reduce((options, item) => {
if (item.type === 'boolean') {
options[item.name] = {
type: 'boolean',
description: item.description!.message,
};
} else if (item.type === 'list' && item.element?.type instanceof Map) {
options[item.name] = {
type: 'array',
items: {
type: 'string',
enum: Array.from(item.element.type.keys()),
},
description: item.description!.message,
};
} else if (item.type instanceof Map) {
options[item.name] = {
type: 'string',
description: item.description!.message,
enum: Array.from(item.type.keys()),
};
}
return options;
}, {});
return {
type: 'object',
properties: {
compilerOptions: {
type: 'object',
properties: properties,
},
},
};
}