vscode-json-languageservice 4.2.0-next.0 → 4.2.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.
@@ -38,7 +38,7 @@
38
38
  patternString = patternString.substring(1);
39
39
  }
40
40
  this.globWrappers.push({
41
- regexp: glob_1.createRegex('**/' + patternString, { extended: true, globstar: true }),
41
+ regexp: (0, glob_1.createRegex)('**/' + patternString, { extended: true, globstar: true }),
42
42
  include: include,
43
43
  });
44
44
  }
@@ -71,6 +71,7 @@
71
71
  this.service = service;
72
72
  this.uri = uri;
73
73
  this.dependencies = new Set();
74
+ this.anchors = undefined;
74
75
  if (unresolvedSchemaContent) {
75
76
  this.unresolvedSchema = this.service.promise.resolve(new UnresolvedSchema(unresolvedSchemaContent));
76
77
  }
@@ -85,7 +86,7 @@
85
86
  var _this = this;
86
87
  if (!this.resolvedSchema) {
87
88
  this.resolvedSchema = this.getUnresolvedSchema().then(function (unresolved) {
88
- return _this.service.resolveSchemaContent(unresolved, _this.uri, _this.dependencies);
89
+ return _this.service.resolveSchemaContent(unresolved, _this);
89
90
  });
90
91
  }
91
92
  return this.resolvedSchema;
@@ -95,6 +96,7 @@
95
96
  this.resolvedSchema = undefined;
96
97
  this.unresolvedSchema = undefined;
97
98
  this.dependencies.clear();
99
+ this.anchors = undefined;
98
100
  return hasChanges;
99
101
  };
100
102
  return SchemaHandle;
@@ -302,7 +304,7 @@
302
304
  return new UnresolvedSchema({}, [localize('json.schema.nocontent', 'Unable to load schema from \'{0}\': {1}.', toDisplayString(url), errorMessage)]);
303
305
  });
304
306
  };
305
- JSONSchemaService.prototype.resolveSchemaContent = function (schemaToResolve, schemaURL, dependencies) {
307
+ JSONSchemaService.prototype.resolveSchemaContent = function (schemaToResolve, handle) {
306
308
  var _this = this;
307
309
  var resolveErrors = schemaToResolve.errors.slice(0);
308
310
  var schema = schemaToResolve.schema;
@@ -319,13 +321,11 @@
319
321
  }
320
322
  }
321
323
  var contextService = this.contextService;
322
- var findSection = function (schema, path) {
323
- if (!path) {
324
- return schema;
325
- }
324
+ var findSectionByJSONPointer = function (schema, path) {
325
+ path = decodeURIComponent(path);
326
326
  var current = schema;
327
327
  if (path[0] === '/') {
328
- path = path.substr(1);
328
+ path = path.substring(1);
329
329
  }
330
330
  path.split('/').some(function (part) {
331
331
  part = part.replace(/~1/g, '/').replace(/~0/g, '~');
@@ -334,130 +334,179 @@
334
334
  });
335
335
  return current;
336
336
  };
337
- var merge = function (target, sourceRoot, sourceURI, refSegment) {
338
- var path = refSegment ? decodeURIComponent(refSegment) : undefined;
339
- var section = findSection(sourceRoot, path);
340
- if (section) {
341
- for (var key in section) {
342
- if (section.hasOwnProperty(key) && !target.hasOwnProperty(key)) {
343
- target[key] = section[key];
344
- }
337
+ var findSchemaById = function (schema, handle, id) {
338
+ if (!handle.anchors) {
339
+ handle.anchors = collectAnchors(schema);
340
+ }
341
+ return handle.anchors.get(id);
342
+ };
343
+ var merge = function (target, section) {
344
+ for (var key in section) {
345
+ if (section.hasOwnProperty(key) && !target.hasOwnProperty(key) && key !== 'id' && key !== '$id') {
346
+ target[key] = section[key];
345
347
  }
346
348
  }
349
+ };
350
+ var mergeRef = function (target, sourceRoot, sourceHandle, refSegment) {
351
+ var section;
352
+ if (refSegment === undefined || refSegment.length === 0) {
353
+ section = sourceRoot;
354
+ }
355
+ else if (refSegment.charAt(0) === '/') {
356
+ // A $ref to a JSON Pointer (i.e #/definitions/foo)
357
+ section = findSectionByJSONPointer(sourceRoot, refSegment);
358
+ }
347
359
  else {
348
- resolveErrors.push(localize('json.schema.invalidref', '$ref \'{0}\' in \'{1}\' can not be resolved.', path, sourceURI));
360
+ // A $ref to a sub-schema with an $id (i.e #hello)
361
+ section = findSchemaById(sourceRoot, sourceHandle, refSegment);
362
+ }
363
+ if (section) {
364
+ merge(target, section);
365
+ }
366
+ else {
367
+ resolveErrors.push(localize('json.schema.invalidid', '$ref \'{0}\' in \'{1}\' can not be resolved.', refSegment, sourceHandle.uri));
349
368
  }
350
369
  };
351
- var resolveExternalLink = function (node, uri, refSegment, parentSchemaURL, parentSchemaDependencies) {
370
+ var resolveExternalLink = function (node, uri, refSegment, parentHandle) {
352
371
  if (contextService && !/^[A-Za-z][A-Za-z0-9+\-.+]*:\/\/.*/.test(uri)) {
353
- uri = contextService.resolveRelativePath(uri, parentSchemaURL);
372
+ uri = contextService.resolveRelativePath(uri, parentHandle.uri);
354
373
  }
355
374
  uri = normalizeId(uri);
356
375
  var referencedHandle = _this.getOrAddSchemaHandle(uri);
357
376
  return referencedHandle.getUnresolvedSchema().then(function (unresolvedSchema) {
358
- parentSchemaDependencies.add(uri);
377
+ parentHandle.dependencies.add(uri);
359
378
  if (unresolvedSchema.errors.length) {
360
379
  var loc = refSegment ? uri + '#' + refSegment : uri;
361
380
  resolveErrors.push(localize('json.schema.problemloadingref', 'Problems loading reference \'{0}\': {1}', loc, unresolvedSchema.errors[0]));
362
381
  }
363
- merge(node, unresolvedSchema.schema, uri, refSegment);
364
- return resolveRefs(node, unresolvedSchema.schema, uri, referencedHandle.dependencies);
382
+ mergeRef(node, unresolvedSchema.schema, referencedHandle, refSegment);
383
+ return resolveRefs(node, unresolvedSchema.schema, referencedHandle);
365
384
  });
366
385
  };
367
- var resolveRefs = function (node, parentSchema, parentSchemaURL, parentSchemaDependencies) {
368
- if (!node || typeof node !== 'object') {
369
- return Promise.resolve(null);
370
- }
371
- var toWalk = [node];
372
- var seen = new Set();
386
+ var resolveRefs = function (node, parentSchema, parentHandle) {
373
387
  var openPromises = [];
374
- var collectEntries = function () {
375
- var entries = [];
376
- for (var _i = 0; _i < arguments.length; _i++) {
377
- entries[_i] = arguments[_i];
378
- }
379
- for (var _a = 0, entries_1 = entries; _a < entries_1.length; _a++) {
380
- var entry = entries_1[_a];
381
- if (typeof entry === 'object') {
382
- toWalk.push(entry);
383
- }
384
- }
385
- };
386
- var collectMapEntries = function () {
387
- var maps = [];
388
- for (var _i = 0; _i < arguments.length; _i++) {
389
- maps[_i] = arguments[_i];
390
- }
391
- for (var _a = 0, maps_1 = maps; _a < maps_1.length; _a++) {
392
- var map = maps_1[_a];
393
- if (typeof map === 'object') {
394
- for (var k in map) {
395
- var key = k;
396
- var entry = map[key];
397
- if (typeof entry === 'object') {
398
- toWalk.push(entry);
399
- }
400
- }
401
- }
402
- }
403
- };
404
- var collectArrayEntries = function () {
405
- var arrays = [];
406
- for (var _i = 0; _i < arguments.length; _i++) {
407
- arrays[_i] = arguments[_i];
408
- }
409
- for (var _a = 0, arrays_1 = arrays; _a < arrays_1.length; _a++) {
410
- var array = arrays_1[_a];
411
- if (Array.isArray(array)) {
412
- for (var _b = 0, array_1 = array; _b < array_1.length; _b++) {
413
- var entry = array_1[_b];
414
- if (typeof entry === 'object') {
415
- toWalk.push(entry);
416
- }
417
- }
418
- }
419
- }
420
- };
421
- var handleRef = function (next) {
388
+ _this.traverseNodes(node, function (next) {
422
389
  var seenRefs = new Set();
423
390
  while (next.$ref) {
424
391
  var ref = next.$ref;
425
392
  var segments = ref.split('#', 2);
426
393
  delete next.$ref;
427
394
  if (segments[0].length > 0) {
428
- openPromises.push(resolveExternalLink(next, segments[0], segments[1], parentSchemaURL, parentSchemaDependencies));
395
+ // This is a reference to an external schema
396
+ openPromises.push(resolveExternalLink(next, segments[0], segments[1], parentHandle));
429
397
  return;
430
398
  }
431
399
  else {
400
+ // This is a reference inside the current schema
432
401
  if (!seenRefs.has(ref)) {
433
- merge(next, parentSchema, parentSchemaURL, segments[1]); // can set next.$ref again, use seenRefs to avoid circle
402
+ var id = segments[1];
403
+ mergeRef(next, parentSchema, parentHandle, id);
434
404
  seenRefs.add(ref);
435
405
  }
436
406
  }
437
407
  }
408
+ });
409
+ return _this.promise.all(openPromises);
410
+ };
411
+ var collectAnchors = function (root) {
412
+ var result = new Map();
413
+ _this.traverseNodes(root, function (next) {
414
+ var id = next.$id || next.id;
415
+ if (typeof id === 'string' && id.charAt(0) === '#') {
416
+ // delete next.$id;
417
+ // delete next.id;
418
+ var anchor = id.substring(1);
419
+ if (result.has(anchor)) {
420
+ resolveErrors.push(localize('json.schema.duplicateid', 'Duplicate id declaration: \'{0}\'', id));
421
+ }
422
+ else {
423
+ result.set(anchor, next);
424
+ }
425
+ }
426
+ });
427
+ return result;
428
+ };
429
+ return resolveRefs(schema, schema, handle).then(function (_) {
430
+ return new ResolvedSchema(schema, resolveErrors);
431
+ });
432
+ };
433
+ JSONSchemaService.prototype.traverseNodes = function (root, handle) {
434
+ if (!root || typeof root !== 'object') {
435
+ return Promise.resolve(null);
436
+ }
437
+ var seen = new Set();
438
+ var collectEntries = function () {
439
+ var entries = [];
440
+ for (var _i = 0; _i < arguments.length; _i++) {
441
+ entries[_i] = arguments[_i];
442
+ }
443
+ for (var _a = 0, entries_1 = entries; _a < entries_1.length; _a++) {
444
+ var entry = entries_1[_a];
445
+ if (typeof entry === 'object') {
446
+ toWalk.push(entry);
447
+ }
448
+ }
449
+ };
450
+ var collectMapEntries = function () {
451
+ var maps = [];
452
+ for (var _i = 0; _i < arguments.length; _i++) {
453
+ maps[_i] = arguments[_i];
454
+ }
455
+ for (var _a = 0, maps_1 = maps; _a < maps_1.length; _a++) {
456
+ var map = maps_1[_a];
457
+ if (typeof map === 'object') {
458
+ for (var k in map) {
459
+ var key = k;
460
+ var entry = map[key];
461
+ if (typeof entry === 'object') {
462
+ toWalk.push(entry);
463
+ }
464
+ }
465
+ }
466
+ }
467
+ };
468
+ var collectArrayEntries = function () {
469
+ var arrays = [];
470
+ for (var _i = 0; _i < arguments.length; _i++) {
471
+ arrays[_i] = arguments[_i];
472
+ }
473
+ for (var _a = 0, arrays_1 = arrays; _a < arrays_1.length; _a++) {
474
+ var array = arrays_1[_a];
475
+ if (Array.isArray(array)) {
476
+ for (var _b = 0, array_1 = array; _b < array_1.length; _b++) {
477
+ var entry = array_1[_b];
478
+ if (typeof entry === 'object') {
479
+ toWalk.push(entry);
480
+ }
481
+ }
482
+ }
483
+ }
484
+ };
485
+ var toWalk = [root];
486
+ var next = toWalk.pop();
487
+ while (next) {
488
+ if (!seen.has(next)) {
489
+ seen.add(next);
490
+ handle(next);
438
491
  collectEntries(next.items, next.additionalItems, next.additionalProperties, next.not, next.contains, next.propertyNames, next.if, next.then, next.else);
439
492
  collectMapEntries(next.definitions, next.properties, next.patternProperties, next.dependencies);
440
493
  collectArrayEntries(next.anyOf, next.allOf, next.oneOf, next.items);
441
- };
442
- while (toWalk.length) {
443
- var next = toWalk.pop();
444
- if (seen.has(next)) {
445
- continue;
446
- }
447
- seen.add(next);
448
- handleRef(next);
449
494
  }
450
- return _this.promise.all(openPromises);
451
- };
452
- return resolveRefs(schema, schema, schemaURL, dependencies).then(function (_) { return new ResolvedSchema(schema, resolveErrors); });
495
+ next = toWalk.pop();
496
+ }
453
497
  };
454
- JSONSchemaService.prototype.getSchemaProperty = function (document) {
498
+ ;
499
+ JSONSchemaService.prototype.getSchemaFromProperty = function (resource, document) {
455
500
  var _a, _b;
456
501
  if (((_a = document.root) === null || _a === void 0 ? void 0 : _a.type) === 'object') {
457
502
  for (var _i = 0, _c = document.root.properties; _i < _c.length; _i++) {
458
503
  var p = _c[_i];
459
504
  if (p.keyNode.value === '$schema' && ((_b = p.valueNode) === null || _b === void 0 ? void 0 : _b.type) === 'string') {
460
- return p.valueNode.value;
505
+ var schemaId = p.valueNode.value;
506
+ if (this.contextService && !/^\w[\w\d+.-]*:/.test(schemaId)) { // has scheme
507
+ schemaId = this.contextService.resolveRelativePath(schemaId, resource);
508
+ }
509
+ return schemaId;
461
510
  }
462
511
  }
463
512
  }
@@ -482,7 +531,7 @@
482
531
  return schemas;
483
532
  };
484
533
  JSONSchemaService.prototype.getSchemaURIsForResource = function (resource, document) {
485
- var schemeId = document && this.getSchemaProperty(document);
534
+ var schemeId = document && this.getSchemaFromProperty(resource, document);
486
535
  if (schemeId) {
487
536
  return [schemeId];
488
537
  }
@@ -491,10 +540,7 @@
491
540
  JSONSchemaService.prototype.getSchemaForResource = function (resource, document) {
492
541
  if (document) {
493
542
  // first use $schema if present
494
- var schemeId = this.getSchemaProperty(document);
495
- if (schemeId && Strings.startsWith(schemeId, '.') && this.contextService) {
496
- schemeId = this.contextService.resolveRelativePath(schemeId, resource);
497
- }
543
+ var schemeId = this.getSchemaFromProperty(resource, document);
498
544
  if (schemeId) {
499
545
  var id = normalizeId(schemeId);
500
546
  return this.getOrAddSchemaHandle(id).getResolvedSchema();
@@ -523,7 +569,8 @@
523
569
  JSONSchemaService.prototype.getMatchingSchemas = function (document, jsonDocument, schema) {
524
570
  if (schema) {
525
571
  var id = schema.id || ('schemaservice://untitled/matchingSchemas/' + idCounter++);
526
- return this.resolveSchemaContent(new UnresolvedSchema(schema), id, new Set()).then(function (resolvedSchema) {
572
+ var handle = this.addSchemaHandle(id, schema);
573
+ return handle.getResolvedSchema().then(function (resolvedSchema) {
527
574
  return jsonDocument.getMatchingSchemas(resolvedSchema.schema).filter(function (s) { return !s.inverted; });
528
575
  });
529
576
  }
@@ -60,7 +60,7 @@
60
60
  function newRange(start, end) {
61
61
  return jsonLanguageTypes_1.Range.create(document.positionAt(start), document.positionAt(end));
62
62
  }
63
- var scanner = jsonc_parser_1.createScanner(document.getText(), true);
63
+ var scanner = (0, jsonc_parser_1.createScanner)(document.getText(), true);
64
64
  function getOffsetAfterNextToken(offset, expectedToken) {
65
65
  scanner.setPosition(offset);
66
66
  var token = scanner.scan();
@@ -8,13 +8,12 @@
8
8
  if (v !== undefined) module.exports = v;
9
9
  }
10
10
  else if (typeof define === "function" && define.amd) {
11
- define(["require", "exports", "./jsonSchemaService", "../jsonLanguageTypes", "vscode-nls", "../utils/objects"], factory);
11
+ define(["require", "exports", "../jsonLanguageTypes", "vscode-nls", "../utils/objects"], factory);
12
12
  }
13
13
  })(function (require, exports) {
14
14
  "use strict";
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
16
  exports.JSONValidation = void 0;
17
- var jsonSchemaService_1 = require("./jsonSchemaService");
18
17
  var jsonLanguageTypes_1 = require("../jsonLanguageTypes");
19
18
  var nls = require("vscode-nls");
20
19
  var objects_1 = require("../utils/objects");
@@ -98,7 +97,8 @@
98
97
  };
99
98
  if (schema) {
100
99
  var id = schema.id || ('schemaservice://untitled/' + idCounter++);
101
- return this.jsonSchemaService.resolveSchemaContent(new jsonSchemaService_1.UnresolvedSchema(schema), id, new Set()).then(function (resolvedSchema) {
100
+ var handle = this.jsonSchemaService.registerExternalSchema(id, [], schema);
101
+ return handle.getResolvedSchema().then(function (resolvedSchema) {
102
102
  return getDiagnostics(resolvedSchema);
103
103
  });
104
104
  }
@@ -115,14 +115,14 @@
115
115
  var idCounter = 0;
116
116
  function schemaAllowsComments(schemaRef) {
117
117
  if (schemaRef && typeof schemaRef === 'object') {
118
- if (objects_1.isBoolean(schemaRef.allowComments)) {
118
+ if ((0, objects_1.isBoolean)(schemaRef.allowComments)) {
119
119
  return schemaRef.allowComments;
120
120
  }
121
121
  if (schemaRef.allOf) {
122
122
  for (var _i = 0, _a = schemaRef.allOf; _i < _a.length; _i++) {
123
123
  var schema = _a[_i];
124
124
  var allow = schemaAllowsComments(schema);
125
- if (objects_1.isBoolean(allow)) {
125
+ if ((0, objects_1.isBoolean)(allow)) {
126
126
  return allow;
127
127
  }
128
128
  }
@@ -132,18 +132,18 @@
132
132
  }
133
133
  function schemaAllowsTrailingCommas(schemaRef) {
134
134
  if (schemaRef && typeof schemaRef === 'object') {
135
- if (objects_1.isBoolean(schemaRef.allowTrailingCommas)) {
135
+ if ((0, objects_1.isBoolean)(schemaRef.allowTrailingCommas)) {
136
136
  return schemaRef.allowTrailingCommas;
137
137
  }
138
138
  var deprSchemaRef = schemaRef;
139
- if (objects_1.isBoolean(deprSchemaRef['allowsTrailingCommas'])) { // deprecated
139
+ if ((0, objects_1.isBoolean)(deprSchemaRef['allowsTrailingCommas'])) { // deprecated
140
140
  return deprSchemaRef['allowsTrailingCommas'];
141
141
  }
142
142
  if (schemaRef.allOf) {
143
143
  for (var _i = 0, _a = schemaRef.allOf; _i < _a.length; _i++) {
144
144
  var schema = _a[_i];
145
145
  var allow = schemaAllowsTrailingCommas(schema);
146
- if (objects_1.isBoolean(allow)) {
146
+ if ((0, objects_1.isBoolean)(allow)) {
147
147
  return allow;
148
148
  }
149
149
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vscode-json-languageservice",
3
- "version": "4.2.0-next.0",
3
+ "version": "4.2.0",
4
4
  "description": "Language service for JSON",
5
5
  "main": "./lib/umd/jsonLanguageService.js",
6
6
  "typings": "./lib/umd/jsonLanguageService",
@@ -15,21 +15,21 @@
15
15
  "url": "https://github.com/Microsoft/vscode-json-languageservice"
16
16
  },
17
17
  "devDependencies": {
18
- "@types/mocha": "^9.0.0",
18
+ "@types/mocha": "^9.1.0",
19
19
  "@types/node": "^10.12.21",
20
- "@typescript-eslint/eslint-plugin": "^4.32.0",
21
- "@typescript-eslint/parser": "^4.32.0",
22
- "eslint": "^7.32.0",
23
- "mocha": "^9.1.2",
20
+ "@typescript-eslint/eslint-plugin": "^5.10.1",
21
+ "@typescript-eslint/parser": "^5.10.1",
22
+ "eslint": "^8.7.0",
23
+ "mocha": "^9.2.0",
24
24
  "rimraf": "^3.0.2",
25
- "typescript": "^4.1.3"
25
+ "typescript": "^4.5.5"
26
26
  },
27
27
  "dependencies": {
28
28
  "jsonc-parser": "^3.0.0",
29
- "vscode-languageserver-textdocument": "^1.0.1",
29
+ "vscode-languageserver-textdocument": "^1.0.3",
30
30
  "vscode-languageserver-types": "^3.16.0",
31
31
  "vscode-nls": "^5.0.0",
32
- "vscode-uri": "^3.0.2"
32
+ "vscode-uri": "^3.0.3"
33
33
  },
34
34
  "scripts": {
35
35
  "prepublishOnly": "npm run clean && npm run compile-esm && npm run test && npm run remove-sourcemap-refs",