Skip to content

Implement getSupportInfo() and use it for inference#3033

Merged
azz merged 8 commits intoprettier:masterfrom
azz:lang-support
Nov 3, 2017
Merged

Implement getSupportInfo() and use it for inference#3033
azz merged 8 commits intoprettier:masterfrom
azz:lang-support

Conversation

@azz
Copy link
Copy Markdown
Member

@azz azz commented Oct 14, 2017

TODO

  • CLI
  • Docs

This PR adds a new function to the prettier API: getSupportInfo().

It returns an object, currently with a languages property, which is an array of:

{
  name: "SCSS", // display name for the language
  since: "1.4.0", // version it was added to prettier
  parsers: ["scss"], // built-in parsers that support this language
  group: "CSS", // metadata used to group related languages
  tmScope: "source.scss", // TextMate scope, used by prettier-atom
  aceMode: "scss",
  codemirrorMode: "css",
  codemirrorMimeType: "text/x-scss",
  extensions: [".scss"], // extensions used in parser inference
  files: [], // filenames without extensions, also used in parser inference
  liguistLanguageId: 329, // GitHub linguist language ID
  vscodeLanguageIds: ["scss"] // used by prettier-vscode
},

getSupportInfo accepts an optional argument, version, e.g. prettier.getSupportInfo("1.4.0"), this will filter the list to only include languages that version 1.4.0 of prettier support. This includes changing less/scss/css parsers back to postcss.

In addition, this table is now used by Prettier internally to perform parser inference based on filename and extension.

Maybe we should also add a CLI version?

Closes #3023
Fixes #3058

@ikatyang
Copy link
Copy Markdown
Member

Maybe we should also add a CLI version?

Yeah, the CLI seems necessary for non-JS editor, e.g. vim-prettier.

package.json Outdated
"postcss-values-parser": "1.3.1",
"remark-frontmatter": "1.1.0",
"remark-parse": "4.0.0",
"semver": "^5.4.1",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems your yarn is always too lazy to pin them. 😂

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Has save-prefix "" been removed from yarn?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it works for me (v1.2.1).

src/support.js Outdated

const languages = supportTable
.filter(language => {
return language.since && semver.gte(version, language.since);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be simplified to language => language...


testVersions.forEach(version => {
test(`with version ${version}`, () => {
expect(prettier.getSupportInfo(version)).toMatchSnapshot();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we only snapshot the { [language]: parsers } information? It's hard to read its snapshot and other information seems unrelated here.

// Based on:
// https://github.com/github/linguist/blob/master/lib/linguist/languages.yml

const supportTable = [
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should add a script to generate this table from linguist so that it'd be easy to update in the future.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe. I ended up changing quite a few things though, added more extensions, filenames, added groups, etc.

},
{
name: "HTML",
since: undefined, // unreleased
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No plans to release this yet (in eg 1.8.0), right?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not. Depends on #2976

@mitermayer
Copy link
Copy Markdown
Member

Are there any plans on having this function exposed through the CLI ? I believe the info table supplied by the CLI does not need to be so verbose so would probably be a much simpler output than the one returned by getSupportInfo()

@azz
Copy link
Copy Markdown
Member Author

azz commented Oct 19, 2017

What kind of output would you expect from the CLI version?

@azz
Copy link
Copy Markdown
Member Author

azz commented Oct 29, 2017

Ping @mitermayer, would JSON output or a parseable table be good for CLI?

@mitermayer
Copy link
Copy Markdown
Member

Hi @azz,

Vim has a function json_decode() however to better leverage that we need to make sure that the output of getSupportInfo() would be a plain json with nothing else, otherwise we would still need to use regex to extract the JSON node, and in that case a table would make more sense.

This is the documentation info on json_decode() function for vim 8:

json_decode({string})					*json_decode()*
		This parses a JSON formatted string and returns the equivalent
		in Vim values.  See |json_encode()| for the relation between
		JSON and Vim values.
		The decoding is permissive:
		- A trailing comma in an array and object is ignored.
		- More floating point numbers are recognized, e.g. "1." for
		  "1.0".
		However, a duplicate key in an object is not allowed. *E938*
		The result must be a valid Vim type:
		- An empty object member name is not allowed.
		- Duplicate object member names are not allowed.

@mitermayer
Copy link
Copy Markdown
Member

I actually am not sure we can leverage from the json_decode to parse arbitrary json. So I would suggest printing as a table since it would also be nicer for CLI users, and on the vim side we can use regex to extract info output

@azz
Copy link
Copy Markdown
Member Author

azz commented Oct 29, 2017

Let's just do

console.log(
  prettier.format(
    JSON.stringify(prettier.getSupportInfo()),
    { parser: "json" }
  )
);

?

@mitermayer
Copy link
Copy Markdown
Member

We can always start with that, then i can try to to add that to vim-prettier and see how that goes. Can always submit another PR later to allow the getSupportInfo() cli command to accept other output formats other than JSON if required

@azz azz mentioned this pull request Oct 29, 2017
9 tasks
@azz
Copy link
Copy Markdown
Member Author

azz commented Nov 2, 2017

@mitermayer do you want to give this a try with json_decode before we merge?

@mitermayer
Copy link
Copy Markdown
Member

I won't be able to test this today, so feel free to merge it as it is! Can always wait for next release in case this is not sufficient.

@azz azz merged commit e436ada into prettier:master Nov 3, 2017
@azz azz deleted the lang-support branch November 3, 2017 07:06
@lock lock bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Jan 19, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Jan 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants