@lvce-editor/eslint-plugin-extension-json 18.17.0 → 19.1.0

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