microsoft/vscode

[json] Add support for JSON Schema inference from node_modules

andyrichardson opened this issue ยท 7 comments

Hey there ๐Ÿ‘‹ thanks for the awesome work you've been doiong!

Proposal

When a user installs a package with typescript support, VSCode uses the types from that package in order to give intellisense propts. If a package wants to include a JSON schema for a config file (e.g. .prettierrc.json), the package author needs to instead make a VSCode plugin.

It would be great if packages could declare a JSON Schema and VSCode automatically sources that from node_modules.

What does this solve

For users, this means automatically getting schema completion just by having a package installed. No need to find and install an extension for Babel, Prettier, etc.

For library authors, this removes the need for creating extensions for many editors (assuming this gets standardized). Additionally, JSON schemas are coupled with package releases, avoiding extension/package version mismatching.

Can you give a concrete example? Which JSON file would that be and which npm package would contain that schema? How would VS Code make that connection?

Which JSON file would that be

Off the top of my head, some unique file such as json-schemas.json in the root of the package would make sense.

The syntax demonstrated here with file matching support would be what I would expect. Here's an example of how @babel/code could describe a json schema

[
  { 
    "fileMatch": ["babel.config.json", ".babelrc.json"],
    "schema": {
      "type": "object",
      // ...
    }
  }
]

which npm package would contain that schema

The package that uses/reads that config file. In the case of multiple packages reading that file, there is usually a "core" package (e..g babel/core).

Conflicting declarations are unlikely to happen, and would be up to the package authors to resolve.

How would VS Code make that connection

VSCode could asynchronously check for json-schemas.json in all packages within node_modules, store a record of matchers and where they're declared (e.g. babel.config.json -> /node_modules/@babel/core/json-schemas.json), and then load said schema when a matching file is opened

@andyrichardson ok, I see, that makes sense. I didn't know about the json-schemas.json files.

Currently we can use $schema to specify the schema for a json file but it doesn't seem like we can point this to node_modules without using an explicit node_modules path.

Example:

Works:

{
  "$schema": "https://unpkg.com/somepackage@someversion/schema.json",
}

Works:

{
  "$schema": "node_modules/.pnpm/somepackage@someversion/node_modules/somepackage/schema.json",
}

Doesn't Work:

{
  "$schema": "somepackage/schema.json",
}

It would be nice if support could be added to this last example, making it resolve somepackage the same way packages are resolved in TypeScript files.