JavaScript/JSON Interface Description Language

2020-07-24 05:10发布

问题:

Take as example MDN Spider Monkey Parser API web page. You can see that an object returned by a Javascript method is described by a meta-language. The following snippet in that page:

interface Node {
  type: string;
  loc: SourceLocation | null;
}

Describes an object called Node which has two properties: type and loc. And the following:

interface Program <: Node {
  type: "Program";
  body: [ Statement ];
}

Describes another object which will inherit properties type and loc from Node plus will have property body.

About this meta-language

This meta-language is obviously used for documentation purposes as Javascript is not typed, it can get quite tricky to understand what an object exposes when having to deal with it.

Example For example, if I had to document a Javascript object consisting of 3 members: a string, an array of ints and a function, I might go something like:

interface MyObject {
  path: string;
  indices: [int];
  evaluateScore: function;
}

This is a clever way to document Javascript objects exposing certain API! But cannot find a standard here!

Questions I want to know more about this meta-language, but cannot find resources in Internet. However, in order to make this question answerable and not too generic, as starters, please consider the following questions:

  1. Does this meta-language have a name?
  2. Is this a possible IDL for JavaScript/JSON?
  3. Does this meta-language have an official specification?
  4. If a specification is present, where can i find it?

Thanks

Note

Please note that this question is not about ASTs. I do not care about them! The page I linked just happens to be about ASTs, but that is because I could not find another web page or library documenting their JS stuff using such a meta-language. I just want to know more about the meta-language used by MDN in that page to document their JavaScript objects and what API they expose.

回答1:

I too spent quite some time looking for a specification, or some kind of description for this format. A few projects seem to be using this type of IDL for documentation of an AST, including Babel, Handlebars, and ESTree.

I found this issue on the ESTree github repository that states it is a custom syntax. I wonder if this is true, since it seems strange that so many large projects would decide to use an undocumented syntax like this.