@lvce-editor/eslint-plugin-extension-json 18.16.0 → 19.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.
- package/dist/index.js +214 -10
- package/package.json +6 -3
package/dist/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import json from '@eslint/json';
|
|
2
|
+
import ts from 'typescript';
|
|
2
3
|
|
|
3
4
|
const isArrayNode = node => {
|
|
4
5
|
return node && node.type === 'Array';
|
|
@@ -31,7 +32,7 @@ const findMember = (node, name) => {
|
|
|
31
32
|
};
|
|
32
33
|
|
|
33
34
|
const requiredFirstDirective = "default-src 'none'";
|
|
34
|
-
const meta$
|
|
35
|
+
const meta$4 = {
|
|
35
36
|
docs: {
|
|
36
37
|
description: 'Require a secure content security policy for extension workers'
|
|
37
38
|
},
|
|
@@ -48,7 +49,7 @@ const hasMainEntry = manifest => {
|
|
|
48
49
|
const isValidPolicyArray = node => {
|
|
49
50
|
return isArrayNode(node) && node.elements.every(element => isStringNode(element.value));
|
|
50
51
|
};
|
|
51
|
-
const create$
|
|
52
|
+
const create$4 = context => {
|
|
52
53
|
return {
|
|
53
54
|
Document(node) {
|
|
54
55
|
if (!isObjectNode(node.body)) {
|
|
@@ -84,11 +85,122 @@ const create$2 = context => {
|
|
|
84
85
|
|
|
85
86
|
const contentSecurityPolicy = {
|
|
86
87
|
__proto__: null,
|
|
87
|
-
create: create$
|
|
88
|
-
meta: meta$
|
|
88
|
+
create: create$4,
|
|
89
|
+
meta: meta$4
|
|
89
90
|
};
|
|
90
91
|
|
|
91
|
-
const meta$
|
|
92
|
+
const meta$3 = {
|
|
93
|
+
docs: {
|
|
94
|
+
description: 'Omit empty optional LVCE extension provider methods'
|
|
95
|
+
},
|
|
96
|
+
messages: {
|
|
97
|
+
omitMethod: 'Omit optional provider method {{name}} instead of implementing it without doing any work.'
|
|
98
|
+
},
|
|
99
|
+
schema: [],
|
|
100
|
+
type: 'suggestion'
|
|
101
|
+
};
|
|
102
|
+
const isApiProviderMember = declaration => {
|
|
103
|
+
const {
|
|
104
|
+
parent
|
|
105
|
+
} = declaration;
|
|
106
|
+
return ts.isPropertySignature(declaration) && !!declaration.questionToken && ts.isInterfaceDeclaration(parent) && parent.name.text.endsWith('Provider') && declaration.getSourceFile().fileName.replaceAll('\\', '/').includes('/node_modules/@lvce-editor/api/');
|
|
107
|
+
};
|
|
108
|
+
const isUndefined = (context, node) => {
|
|
109
|
+
if (node.type === 'UnaryExpression' && node.operator === 'void' && node.argument.type === 'Literal') {
|
|
110
|
+
return true;
|
|
111
|
+
}
|
|
112
|
+
if (node.type !== 'Identifier' || node.name !== 'undefined') {
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
let scope = context.sourceCode.getScope(node);
|
|
116
|
+
while (scope) {
|
|
117
|
+
const variable = scope.set.get('undefined');
|
|
118
|
+
if (variable) {
|
|
119
|
+
return variable.defs.length === 0;
|
|
120
|
+
}
|
|
121
|
+
scope = scope.upper;
|
|
122
|
+
}
|
|
123
|
+
return true;
|
|
124
|
+
};
|
|
125
|
+
const isEmptyFunction = (context, node) => {
|
|
126
|
+
// Parameter initializers/destructuring and generators can do work even with an empty body.
|
|
127
|
+
if (node.generator || node.params.some(parameter => parameter.type !== 'Identifier')) {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
if (node.body.type !== 'BlockStatement') {
|
|
131
|
+
return isUndefined(context, node.body);
|
|
132
|
+
}
|
|
133
|
+
const {
|
|
134
|
+
body
|
|
135
|
+
} = node.body;
|
|
136
|
+
if (body.length === 0) {
|
|
137
|
+
return true;
|
|
138
|
+
}
|
|
139
|
+
return body.length === 1 && body[0].type === 'ReturnStatement' && (!body[0].argument || isUndefined(context, body[0].argument));
|
|
140
|
+
};
|
|
141
|
+
const getPropertyName = node => {
|
|
142
|
+
if (node.key.type === 'Identifier') {
|
|
143
|
+
return node.key.name;
|
|
144
|
+
}
|
|
145
|
+
if (node.key.type === 'Literal') {
|
|
146
|
+
return node.key.value;
|
|
147
|
+
}
|
|
148
|
+
return undefined;
|
|
149
|
+
};
|
|
150
|
+
const returnsUndefined = (checker, property, node) => {
|
|
151
|
+
const methodType = checker.getNonNullableType(checker.getTypeOfSymbolAtLocation(property, node));
|
|
152
|
+
return methodType.getCallSignatures().some(signature => {
|
|
153
|
+
const returnType = checker.getAwaitedType(checker.getReturnTypeOfSignature(signature));
|
|
154
|
+
if (!returnType) {
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
const types = returnType.isUnion() ? returnType.types : [returnType];
|
|
158
|
+
return types.some(type => !!(type.flags & ts.TypeFlags.Undefined));
|
|
159
|
+
});
|
|
160
|
+
};
|
|
161
|
+
const create$3 = context => {
|
|
162
|
+
const services = context.sourceCode.parserServices;
|
|
163
|
+
if (!services?.program || !services.esTreeNodeToTSNodeMap) {
|
|
164
|
+
throw new Error('extension/no-empty-optional-provider-method requires TypeScript type information. Enable parserOptions.projectService.');
|
|
165
|
+
}
|
|
166
|
+
const checker = services.program.getTypeChecker();
|
|
167
|
+
return {
|
|
168
|
+
Property(node) {
|
|
169
|
+
if (node.parent?.type !== 'ObjectExpression' || node.kind !== 'init' || node.computed && node.key.type !== 'Literal' || node.value.type !== 'FunctionExpression' && node.value.type !== 'ArrowFunctionExpression' || !isEmptyFunction(context, node.value)) {
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
const name = getPropertyName(node);
|
|
173
|
+
if (typeof name !== 'string') {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
const objectNode = services.esTreeNodeToTSNodeMap.get(node.parent);
|
|
177
|
+
const contextualType = checker.getContextualType(objectNode);
|
|
178
|
+
// Ambiguous union contexts can require different members, so leave them alone.
|
|
179
|
+
if (!contextualType || contextualType.isUnion()) {
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
const property = contextualType.getProperty(name);
|
|
183
|
+
if (!property || !(property.flags & ts.SymbolFlags.Optional) || !property.declarations?.some(isApiProviderMember) || !returnsUndefined(checker, property, objectNode)) {
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
context.report({
|
|
187
|
+
data: {
|
|
188
|
+
name
|
|
189
|
+
},
|
|
190
|
+
messageId: 'omitMethod',
|
|
191
|
+
node
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
const noEmptyOptionalProviderMethod = {
|
|
198
|
+
__proto__: null,
|
|
199
|
+
create: create$3,
|
|
200
|
+
meta: meta$3
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
const meta$2 = {
|
|
92
204
|
docs: {
|
|
93
205
|
description: 'Disallow empty languages arrays in extension manifests'
|
|
94
206
|
},
|
|
@@ -97,7 +209,7 @@ const meta$1 = {
|
|
|
97
209
|
},
|
|
98
210
|
type: 'problem'
|
|
99
211
|
};
|
|
100
|
-
const create$
|
|
212
|
+
const create$2 = context => {
|
|
101
213
|
return {
|
|
102
214
|
Document(node) {
|
|
103
215
|
if (!isObjectNode(node.body)) {
|
|
@@ -116,6 +228,78 @@ const create$1 = context => {
|
|
|
116
228
|
};
|
|
117
229
|
|
|
118
230
|
const nonEmptyLanguages = {
|
|
231
|
+
__proto__: null,
|
|
232
|
+
create: create$2,
|
|
233
|
+
meta: meta$2
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
const settingTypes = new Set(['array', 'boolean', 'integer', 'null', 'number', 'object', 'string']);
|
|
237
|
+
const isSettingType = node => {
|
|
238
|
+
return node.type === 'String' && settingTypes.has(node.value);
|
|
239
|
+
};
|
|
240
|
+
const isValidType = node => {
|
|
241
|
+
if (node.type !== 'Array') {
|
|
242
|
+
return isSettingType(node);
|
|
243
|
+
}
|
|
244
|
+
const types = new Set();
|
|
245
|
+
for (const {
|
|
246
|
+
value
|
|
247
|
+
} of node.elements) {
|
|
248
|
+
if (value.type !== 'String' || !settingTypes.has(value.value) || types.has(value.value)) {
|
|
249
|
+
return false;
|
|
250
|
+
}
|
|
251
|
+
types.add(value.value);
|
|
252
|
+
}
|
|
253
|
+
return types.size > 0;
|
|
254
|
+
};
|
|
255
|
+
const meta$1 = {
|
|
256
|
+
docs: {
|
|
257
|
+
description: 'Require valid configuration setting declarations in extension manifests'
|
|
258
|
+
},
|
|
259
|
+
messages: {
|
|
260
|
+
invalidConfiguration: 'Extension configuration must be an object that maps setting names to schemas.',
|
|
261
|
+
invalidSetting: 'Extension setting schema must be an object.',
|
|
262
|
+
invalidType: 'Extension setting type must be a JSON Schema type or a non-empty array of distinct JSON Schema types.'
|
|
263
|
+
},
|
|
264
|
+
type: 'problem'
|
|
265
|
+
};
|
|
266
|
+
const create$1 = context => {
|
|
267
|
+
return {
|
|
268
|
+
Document(node) {
|
|
269
|
+
const configuration = findMember(node.body, 'configuration');
|
|
270
|
+
if (!configuration) {
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
if (!isObjectNode(configuration.value)) {
|
|
274
|
+
context.report({
|
|
275
|
+
loc: configuration.value.loc,
|
|
276
|
+
messageId: 'invalidConfiguration'
|
|
277
|
+
});
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
for (const {
|
|
281
|
+
value
|
|
282
|
+
} of configuration.value.members) {
|
|
283
|
+
if (!isObjectNode(value)) {
|
|
284
|
+
context.report({
|
|
285
|
+
loc: value.loc,
|
|
286
|
+
messageId: 'invalidSetting'
|
|
287
|
+
});
|
|
288
|
+
continue;
|
|
289
|
+
}
|
|
290
|
+
const type = findMember(value, 'type');
|
|
291
|
+
if (type && !isValidType(type.value)) {
|
|
292
|
+
context.report({
|
|
293
|
+
loc: type.value.loc,
|
|
294
|
+
messageId: 'invalidType'
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
const validConfiguration = {
|
|
119
303
|
__proto__: null,
|
|
120
304
|
create: create$1,
|
|
121
305
|
meta: meta$1
|
|
@@ -236,6 +420,25 @@ const validKeybindings = {
|
|
|
236
420
|
};
|
|
237
421
|
|
|
238
422
|
const plugin = {
|
|
423
|
+
configs: {},
|
|
424
|
+
meta: {
|
|
425
|
+
name: 'extension',
|
|
426
|
+
version: '0.0.1'
|
|
427
|
+
},
|
|
428
|
+
rules: {
|
|
429
|
+
'no-empty-optional-provider-method': noEmptyOptionalProviderMethod
|
|
430
|
+
}
|
|
431
|
+
};
|
|
432
|
+
const recommended = [{
|
|
433
|
+
files: ['**/*.{ts,mts,cts}'],
|
|
434
|
+
plugins: {
|
|
435
|
+
extension: plugin
|
|
436
|
+
},
|
|
437
|
+
rules: {
|
|
438
|
+
'extension/no-empty-optional-provider-method': 'error'
|
|
439
|
+
}
|
|
440
|
+
}];
|
|
441
|
+
const manifestPlugin = {
|
|
239
442
|
configs: {},
|
|
240
443
|
meta: {
|
|
241
444
|
name: 'extension-json',
|
|
@@ -244,22 +447,23 @@ const plugin = {
|
|
|
244
447
|
rules: {
|
|
245
448
|
'content-security-policy': contentSecurityPolicy,
|
|
246
449
|
'non-empty-languages': nonEmptyLanguages,
|
|
450
|
+
'valid-configuration': validConfiguration,
|
|
247
451
|
'valid-keybindings': validKeybindings
|
|
248
452
|
}
|
|
249
453
|
};
|
|
250
|
-
const
|
|
454
|
+
const recommendedExtensionJson = [{
|
|
251
455
|
files: ['**/extension.json'],
|
|
252
456
|
language: 'json/json',
|
|
253
457
|
plugins: {
|
|
254
|
-
'extension-json':
|
|
255
|
-
// @ts-ignore
|
|
458
|
+
'extension-json': manifestPlugin,
|
|
256
459
|
json
|
|
257
460
|
},
|
|
258
461
|
rules: {
|
|
259
462
|
'extension-json/content-security-policy': 'error',
|
|
260
463
|
'extension-json/non-empty-languages': 'error',
|
|
464
|
+
'extension-json/valid-configuration': 'error',
|
|
261
465
|
'extension-json/valid-keybindings': 'error'
|
|
262
466
|
}
|
|
263
467
|
}];
|
|
264
468
|
|
|
265
|
-
export { recommended as default };
|
|
469
|
+
export { recommended as default, recommendedExtensionJson };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lvce-editor/eslint-plugin-extension-json",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "19.0.1",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -8,8 +8,11 @@
|
|
|
8
8
|
},
|
|
9
9
|
"type": "module",
|
|
10
10
|
"license": "MIT",
|
|
11
|
-
"description": "ESLint rules for LVCE extension manifests.",
|
|
11
|
+
"description": "ESLint rules for LVCE extension providers and manifests.",
|
|
12
12
|
"dependencies": {
|
|
13
|
-
"@eslint/json": "2.
|
|
13
|
+
"@eslint/json": "2.1.1"
|
|
14
|
+
},
|
|
15
|
+
"peerDependencies": {
|
|
16
|
+
"typescript": ">=6.0.0-0 <7.0.0-0"
|
|
14
17
|
}
|
|
15
18
|
}
|