-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(prettier-plugin-jsdoc): get the supported languages from Prettier
- Loading branch information
Showing
6 changed files
with
43 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,110 +1,22 @@ | ||
const jsLang = require('linguist-languages/data/JavaScript.json'); | ||
const jsxLang = require('linguist-languages/data/JSX.json'); | ||
const tsLang = require('linguist-languages/data/TypeScript.json'); | ||
const tsxLang = require('linguist-languages/data/TSX.json'); | ||
const R = require('ramda'); | ||
const prettier = require('prettier'); | ||
const { getSupportedLanguages } = require('./constants'); | ||
const { get, provider } = require('./app'); | ||
|
||
/** | ||
* @typedef {LinguistLanguageProperties & LanguageSharedProperties} LinguistLanguage | ||
* @typedef {import('../types').PrettierSupportLanguage} PrettierSupportLanguage | ||
*/ | ||
|
||
/** | ||
* @typedef {LanguagePrivateProperties & | ||
* LanguageSharedProperties & | ||
* LanguageProperties} Language | ||
*/ | ||
|
||
/** | ||
* @typedef {Object} LinguistLanguageProperties | ||
* @property {number} languageId A unique identifier for the language. | ||
*/ | ||
|
||
/** | ||
* @typedef {Object} LanguageSharedProperties | ||
* @property {string[]} extensions The extensions of files that related to the language. | ||
* @property {string[]} [aliases] A list of known aliases for the language. | ||
* @property {string[]} [filenames] An Array of filenames commonly associated with the | ||
* language. | ||
* @see https://github.com/github/linguist/blob/master/lib/linguist/languages.yml | ||
*/ | ||
|
||
/** | ||
* @typedef {Object} LanguagePrivateProperties | ||
* @property {number} linguistLanguageId The unique ID for the language on Linguist. | ||
*/ | ||
|
||
/** | ||
* @typedef {Object} LanguageProperties | ||
* @property {string} since Since which version of Prettier it can be used. | ||
* @property {string[]} parsers The list of parsers that can parse the | ||
* language. | ||
* @property {string[]} vscodeLanguageIds The ID for the language on VSCode. | ||
*/ | ||
|
||
/** | ||
* @typedef {LanguageProperties & Partial<LanguageSharedProperties>} LanguageOverrides | ||
*/ | ||
|
||
/** | ||
* Creates the language definition for Prettier. | ||
* | ||
* @param {LinguistLanguage} linguistLanguage The base data for the language that will | ||
* be used. | ||
* @param {LanguageOverrides} overrides The information of the language for | ||
* Prettier, and possible overrides for the | ||
* Linguist data. | ||
* @returns {Language} | ||
*/ | ||
const createLanguage = (linguistLanguage, overrides) => { | ||
const { languageId, ...rest } = linguistLanguage; | ||
return { | ||
linguistLanguageId: languageId, | ||
...rest, | ||
...overrides, | ||
}; | ||
}; | ||
|
||
/** | ||
* Generates the list of languages the plugin supports. | ||
* | ||
* @returns {Language[]} | ||
* @returns {PrettierSupportLanguage[]} | ||
*/ | ||
const getLanguages = () => { | ||
const useCreateLanguage = get(createLanguage); | ||
return [ | ||
useCreateLanguage(jsLang, { | ||
since: '0.0.0', | ||
parsers: ['babel', 'babel-flow', 'babel-ts', 'flow', 'typescript'], | ||
vscodeLanguageIds: ['javascript', 'mongo'], | ||
extensions: [...jsLang.extensions, '.wxs'], | ||
}), | ||
useCreateLanguage(jsLang, { | ||
name: 'Flow', | ||
since: '0.0.0', | ||
parsers: ['flow', 'babel-flow'], | ||
vscodeLanguageIds: ['javascript'], | ||
aliases: [], | ||
filenames: [], | ||
extensions: ['.js.flow'], | ||
}), | ||
useCreateLanguage(jsxLang, { | ||
since: '0.0.0', | ||
parsers: ['babel', 'babel-flow', 'babel-ts', 'flow', 'typescript'], | ||
vscodeLanguageIds: ['javascriptreact'], | ||
}), | ||
useCreateLanguage(tsLang, { | ||
since: '1.4.0', | ||
parsers: ['typescript', 'babel-ts'], | ||
vscodeLanguageIds: ['typescript'], | ||
}), | ||
useCreateLanguage(tsxLang, { | ||
since: '1.4.0', | ||
parsers: ['typescript', 'babel-ts'], | ||
vscodeLanguageIds: ['typescriptreact'], | ||
}), | ||
]; | ||
}; | ||
const getLanguages = () => | ||
R.filter( | ||
R.propSatisfies(R.includes(R.__, get(getSupportedLanguages)()), 'name'), | ||
prettier.getSupportInfo().languages, | ||
); | ||
|
||
module.exports.getLanguages = getLanguages; | ||
module.exports.createLanguage = createLanguage; | ||
module.exports.provider = provider('getLanguages', module.exports); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,31 @@ | ||
jest.mock('linguist-languages/data/JavaScript.json', () => ({ | ||
languageId: 'js-lang', | ||
extensions: [], | ||
})); | ||
jest.mock('linguist-languages/data/JSX.json', () => ({ | ||
languageId: 'jsx-lang', | ||
})); | ||
jest.mock('linguist-languages/data/TypeScript.json', () => ({ | ||
languageId: 'ts-lang', | ||
})); | ||
jest.mock('linguist-languages/data/TSX.json', () => ({ | ||
languageId: 'tsx-lang', | ||
})); | ||
jest.mock('prettier'); | ||
jest.unmock('../../../src/fns/getLanguages'); | ||
|
||
const prettier = require('prettier'); | ||
const { getLanguages } = require('../../../src/fns/getLanguages'); | ||
|
||
describe('getLanguages', () => { | ||
beforeEach(() => { | ||
prettier.getSupportInfo.mockClear(); | ||
}); | ||
|
||
it('should generate the list of supported languages', () => { | ||
// Given/When/Then | ||
expect(getLanguages()).toEqual([ | ||
{ | ||
linguistLanguageId: 'js-lang', | ||
vscodeLanguageIds: ['javascript', 'mongo'], | ||
since: expect.any(String), | ||
parsers: expect.any(Array), | ||
extensions: expect.any(Array), | ||
}, | ||
{ | ||
name: 'Flow', | ||
linguistLanguageId: 'js-lang', | ||
vscodeLanguageIds: ['javascript'], | ||
since: expect.any(String), | ||
parsers: expect.any(Array), | ||
extensions: expect.any(Array), | ||
aliases: [], | ||
filenames: [], | ||
}, | ||
{ | ||
linguistLanguageId: 'jsx-lang', | ||
vscodeLanguageIds: ['javascriptreact'], | ||
since: expect.any(String), | ||
parsers: expect.any(Array), | ||
}, | ||
{ | ||
linguistLanguageId: 'ts-lang', | ||
vscodeLanguageIds: ['typescript'], | ||
since: expect.any(String), | ||
parsers: expect.any(Array), | ||
}, | ||
{ | ||
linguistLanguageId: 'tsx-lang', | ||
vscodeLanguageIds: ['typescriptreact'], | ||
since: expect.any(String), | ||
parsers: expect.any(Array), | ||
}, | ||
]); | ||
// Given | ||
const supportedLanguage = { | ||
name: 'JavaScript', | ||
linguistLanguageId: 183, | ||
}; | ||
const unsupportedLanguage = { | ||
name: 'NativeScript', | ||
linguistLanguageId: 101, | ||
}; | ||
prettier.getSupportInfo.mockImplementationOnce(() => ({ | ||
languages: [supportedLanguage, unsupportedLanguage], | ||
})); | ||
let result = null; | ||
// When | ||
result = getLanguages(); | ||
// Then | ||
expect(result).toEqual([supportedLanguage]); | ||
}); | ||
}); |