-
Notifications
You must be signed in to change notification settings - Fork 133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support passing babel plugin configuration in JSON #88
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
d5c3443
Support passing babel plugin configuration in JSON
saaryab fbab9be
Clean up extra spaces
saaryab 29da002
Fix comment
saaryab 8d75eb8
Syntax conventions
saaryab fc39ce2
Clearer naming
saaryab da7e32c
Clearer parsing of plugin options as JSON
saaryab a8a3d23
conventions
saaryab 17d6321
remove older comment
saaryab 37c8374
removeunused import
saaryab 04161fd
add types
saaryab c0377f7
add documentation
saaryab 8f6ff7c
Merge branch 'master' into master
byara 5c4bcfb
Use escaped double quote strings
saaryab 25019bf
Merge branch 'master' of github.com:saaryab/prettier-plugin-sort-imports
saaryab 16adea7
Merge branch 'v3.x' into master
saaryab accb8b9
Remove decorators-legacy babel plugin
saaryab 3ab9e39
Remove classProperties babel plugin from default TS plugins
saaryab File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,9 +1,8 @@ | ||
import { RequiredOptions } from 'prettier'; | ||
import { ParserPlugin } from '@babel/parser'; | ||
|
||
export interface PrettierOptions extends RequiredOptions { | ||
importOrder: string[]; | ||
importOrderSeparation: boolean; | ||
importOrderCaseInsensitive: boolean; | ||
experimentalBabelParserPluginsList: ParserPlugin[]; | ||
experimentalBabelParserPluginsList: string[]; // should be of type ParserPlugin from '@babel/parser' but prettier does not support nested arrays in options | ||
} |
24 changes: 24 additions & 0 deletions
24
src/utils/__tests__/get-experimental-parser-plugins.spec.ts
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { getExperimentalParserPlugins } from '../get-experimental-parser-plugins'; | ||
|
||
test('it should return empty list', () => { | ||
expect(getExperimentalParserPlugins([])).toEqual([]); | ||
}); | ||
|
||
test('it should return flow and decorators', () => { | ||
expect(getExperimentalParserPlugins(['flow', 'decorators'])).toEqual(['flow', 'decorators']); | ||
}); | ||
|
||
test('it should return decorators with parsed options', () => { | ||
expect(getExperimentalParserPlugins(["[\"decorators\", { \"decoratorsBeforeExport\": true }]"])) | ||
.toEqual([['decorators', { decoratorsBeforeExport: true }]]); | ||
}); | ||
|
||
test('it should return decorators with parsed options', () => { | ||
expect(getExperimentalParserPlugins(['flow', "[\"decorators\", { \"decoratorsBeforeExport\": true }]"])) | ||
.toEqual(['flow', ['decorators', { decoratorsBeforeExport: true }]]); | ||
}); | ||
|
||
test('it should throw an Error for invalid JSON', () => { | ||
expect(() => getExperimentalParserPlugins(['flow', "[\"decorators\", { decoratorsBeforeExport: true }]"])) | ||
.toThrowError("Invalid JSON in experimentalBabelParserPluginsList: "); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { ParserPlugin, ParserPluginWithOptions } from '@babel/parser'; | ||
|
||
/** | ||
* Returns a list of babel parser plugin names | ||
* @param experimentalBabelParserPluginsList array of experimental babel parser plugins | ||
* @returns list of parser plugins to be passed to babel parser | ||
*/ | ||
export const getExperimentalParserPlugins = (experimentalBabelParserPluginsList: string[]): ParserPlugin[] => { | ||
return experimentalBabelParserPluginsList.map(pluginNameOrJson => { | ||
// ParserPlugin can be either a string or and array of [name: string, options: object] | ||
// in prettier options the array will be sent in a JSON string | ||
const isParserPluginWithOptions = pluginNameOrJson.startsWith("["); | ||
|
||
let plugin; | ||
if (isParserPluginWithOptions) { | ||
try { | ||
plugin = JSON.parse(pluginNameOrJson) as ParserPluginWithOptions; | ||
} catch (e) { | ||
throw Error("Invalid JSON in experimentalBabelParserPluginsList: " + pluginNameOrJson); | ||
} | ||
} else { | ||
plugin = pluginNameOrJson as ParserPlugin; | ||
} | ||
|
||
return plugin; | ||
}); | ||
}; |
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,4 +1,5 @@ | ||
run_spec(__dirname, ["typescript"], { | ||
importOrder: ['^@core/(.*)$', '^@server/(.*)', '^@ui/(.*)$', '^[./]'], | ||
importOrderSeparation: true, | ||
experimentalBabelParserPluginsList : ["classProperties", "[\"decorators\", { \"decoratorsBeforeExport\": true }]"] | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The change I suggested was to remove this line - Instead of heuristically check if it's a JSON (like you do here), always try to parse as JSON.
Or another option that is less heuristical - you can detect plugin names using a regex (maybe `/^@?[a-zA-Z_0-9-]+(/[a-zA-Z_0-9-]+)?$/), and if it doesn't match, assume it's a JSON and then try to parse it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW, what about some short documentation? The JSON string solution isn't something that users would be able to guess, to you should update the README file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the options only support either strings or arrays of
[string, object]
I think this detection is correct and not just a heuristic.Good catch on the documentation - done.