目录
Keegan

feat: support dynamicRef/dynamicRef /dynamicAnchor (JSON Schema 2020-12)  (#337)

  • feat: resolve dynamicRefasastaticdynamicRef as a staticref and register $dynamicAnchor (2020-12)

Implement the first half of JSON Schema 2020-12 dynamicRef/dynamicRef/dynamicAnchor support:

  • Register dynamicAnchornamesasordinaryanchorssoareferencecanresolvetothem,exactlylikedynamicAnchor names as ordinary anchors so a reference can resolve to them, exactly likeanchor.
  • Resolve dynamicReftoitsinitial(lexicallystatic)targetandvalidatetheinstanceagainstit,sodynamicRef to its initial (lexically-static) target and validate the instance against it, sodynamicRef behaves like a plain reftothelocationitinitiallypointsat.Thetargetiskeptashidden,nonenumerablemetadata(ref to the location it initially points at. The target is kept as hidden, non-enumerable metadata (dynamicRefTarget) so sibling keywords on the reference are preserved.
  • Remove the previous ‘unsupported feature’ warning for dynamicRef/dynamicRef/dynamicAnchor and update the corresponding tests.

True dynamic-scope (bookended) resolution is added in a follow-up commit; the 7 official test-suite cases that require it are temporarily skipped.

  • feat: add dynamic-scope resolution for $dynamicRef (2020-12)

Complete JSON Schema 2020-12 $dynamicRef support with true dynamic-scope (“bookended”) resolution:

  • Add a pre-pass (collectDynamicAnchors) that, from the original schema tree before refmergingflattensresourceboundaries,buildsperresourceanchormaps(ref merging flattens resource boundaries, builds per-resource anchor maps (anchorMaps: dynamic-only and all-anchor) and records each dynamicRefslexicalresource(indynamicRef's lexical resource (indynamicRefInfo.scope).
  • Resolve an internal $dynamicRef’s initial target within its own lexical resource so a sibling resource’s identically-named anchor cannot leak in.
  • At validation time, apply the bookending rule: when the initial target is a dynamicAnchorofthereferencedname,resolvetotheoutermostmatchingdynamicAnchor of the referenced name, resolve to the outermost matchingdynamicAnchor currently in the dynamic scope; otherwise behave like a plain $ref.
  • Apply $dynamicRef alongside any sibling keywords on the same node, rather than treating it as a redirect that replaces them.
  • Propagate the hidden dynamicRefmetadatathroughschemamergingsoadynamicRef metadata through schema merging so adynamicRef living directly on a $ref’d resource still resolves.
  • Ignore $dynamicRef as an unknown keyword in drafts before 2020-12.
  • Store the internal, non-enumerable metadata in two consolidated records (DynamicRefInfo and AnchorMaps) rather than five separate hidden fields.

All 32 official dynamicRef test-suite cases now pass; the temporary skips added in the previous commit are removed.

  • test: cover dynamicRef/dynamicRef/dynamicAnchor edge cases (2020-12)

Add project-owned tests for the gaps found in the $dynamicRef coverage review, so the behavior no longer relies solely on the vendored official suite:

  • a sub-schema shared by reference is anchor-collected once (shared-node guard) and raises no spurious duplicate-anchor error
  • a duplicate $dynamicAnchor within one resource is reported as a schema resolution error
  • genuine dynamic-scope resolution: an outer $dynamicAnchor overrides a base resource’s default (extendible pattern)
  • a dynamicReftoaplaindynamicRef to a plainanchor (not bookended) behaves like a plain $ref
  • a dynamicRefwithaJSONpointerfragmentbehaveslikeaplaindynamicRef with a JSON-pointer fragment behaves like a plainref
  • a $dynamicRef resolved through an external schema
  • $dynamicRef is ignored as an unknown keyword in draft 2019-09
  • completion and hover resolve through a $dynamicRef node
  • fix: extend $dynamicRef dynamic scope across external documents (2020-12)

The dynamic-scope pre-pass (collectDynamicAnchors) ran only over the root document’s resources, so a dynamicAnchorthatoverridesabasedefaultwashonoredonlywhenitlivedinthedocumentbeingvalidated.WhentheextendingresourcelivedinaseparatedocumentreachedviadynamicAnchor that overrides a base default was honored only when it lived in the document being validated. When the extending resource lived in a *separate* document reached viaref, its $dynamicAnchor never joined the dynamic scope and the override was lost.

  • Collect each referenced document’s own anchor maps (once) during external link resolution, before refmergingflattensresourceboundaries,soitsref merging flattens resource boundaries, so itsdynamicAnchor declarations can participate in the dynamic scope and its $dynamicRef nodes get their lexical resource recorded.
  • Fold a merged section’s anchor maps into the target during merge, with outermost-resource-wins precedence, so a dynamicAnchordefinedinanexternal(ordynamicAnchor defined in an external (orref’d sub-) resource can override a base default while the base’s own entries keep priority.
  • Derive the effective draft from each resource’s own schemasoareferencedpre202012documentisnotgivenschema so a referenced pre-2020-12 document is not givendynamicAnchor semantics.

Tests cover the cross-document override for both an internal-fragment (dynamicRef "#name") and a non-fragment URI (dynamicRef “other#name”) starting point. Each case uses a fresh service and a fresh schema object: in-place $ref resolution mutates the schema it is given, so reusing one object would let a prior validation leak anchor state and mask the gap.

Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com

  • fix: correct reference-scope handling for 2019-09/2020-12 test suite

Bump the vendored JSON-Schema-Test-Suite from 69acf52 to 92acb61 and fix the spec-compliance gaps it surfaced. The full suite now passes with no skipped assertions.

  • $recursiveRef no longer early-returns, so sibling unevaluatedItems / unevaluatedProperties still evaluate against the node and accumulate their processed-property annotations.
  • Resolve defs/definitionsbeforeif/then/else/itemsduringtraversalsoareferencedresourcefinishesassemblingbeforeareferrermergesit.Fixesadefs/definitions before if/then/else/items during traversal so a referenced resource finishes assembling before a referrer merges it. Fixes adynamicRef chain reached through then: { $ref: … } capturing a half-resolved snapshot.
  • Isolate into an allOf (instead of flatten-merging) when both schemas constrain array items, or when a referencing schema’s unevaluatedItems would otherwise capture a referenced schema’s positional items and its internal self-references.
  • Keep recursiveAnchor/recursiveAnchor/dynamicAnchor and unevaluated* on the allOf wrapper so bookending targets stay discoverable and unevaluated* observes the aggregated annotations of every in-place applicator.
  • Treat a boolean subschema (true/false) as a real schema when following a $ref, not as a missing section.
  • Register a referenced document’s embedded idresourcesasresolvablehandles,andjoinafragmentreferencedresourcesid resources as resolvable handles, and join a fragment-referenced resource'sdynamicAnchor names to the referencing dynamic scope.
  • Fold only the dynamic anchor map across resources; the local map stays per-resource so a sibling’s identically-named anchor cannot leak into an internal $dynamicRef’s lexical resolution.
  • update changelog

Co-authored-by: Keegan Caruso keegancaruso@microsoft.com Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com Co-authored-by: Martin Aeschlimann martinae@microsoft.com

14天前701次提交

vscode-json-languageservice

JSON language service extracted from VSCode to be reused, e.g in the Monaco editor.

npm Package NPM Downloads Build Status License: MIT

Why?

The vscode-json-languageservice contains the language smarts behind the JSON editing experience of Visual Studio Code and the Monaco editor.

  • doValidation analyses an input string and returns syntax and lint errors.
  • doComplete provides completion proposals for a given location.
  • doResolve resolves a completion proposals.
  • doHover provides a hover text for a given location.
  • findDocumentSymbols provides all symbols in the given document.
  • findDocumentColors provides all color symbols in the given document.
  • getColorPresentations returns available color formats for a color symbol.
  • format formats the code at the given range.
  • getFoldingRanges gets folding ranges for the given document.
  • getSelectionRanges gets selection ranges for a given location.
  • getMatchingSchemas matches a document against its schema and returns all AST nodes along with the matching sub schemas.
  • parseJSONDocument creates a JSON document from source code.
  • newJSONDocument creates a JSON document from an AST.

For the complete API see jsonLanguageService.ts and jsonLanguageTypes.ts

Installation

npm install --save vscode-json-languageservice

Sample usage

See sample.ts for an example on how to use the JSON language service.

To run the sample use yarn sample

Development

git clone https://github.com/microsoft/vscode-json-languageservice
cd vscode-json-languageservice
yarn

Use yarn test to compile and run tests

How can I run and debug the service?

  • open the folder in VSCode.
  • set breakpoints, e.g. in jsonCompletion.ts
  • run the Unit tests from the run viewlet and wait until a breakpoint is hit: image

How can I run and debug the service inside an instance of VSCode?

  • run VSCode out of sources setup as described here: https://github.com/Microsoft/vscode/wiki/How-to-Contribute
  • use yarn link vscode-json-languageservice in vscode/extensions/json-language-features/server to run VSCode with the latest changes from vscode-json-languageservice
  • run VSCode out of source (vscode/scripts/code.sh|bat) and open a .json file
  • in VSCode window that is open on the vscode-json-languageservice sources, run command Debug: Attach to Node process and pick the code-oss process with the json-language-features path image
  • set breakpoints, e.g. in jsonCompletion.ts
  • in the instance run from sources, invoke code completion in the .json file
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9 京公网安备 11010802047560号