From 7c342f346eaf9864adaa23908ab4ec1b93ebf3ab Mon Sep 17 00:00:00 2001 From: Nutthapat Pongtanyavichai <59821765+Leomotors@users.noreply.github.com> Date: Sat, 22 Oct 2022 15:01:27 +0700 Subject: [PATCH 1/3] fix(types): optional type and jsdoc --- src/types.ts | 4 ++- types/index.d.ts | 91 ++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 88 insertions(+), 7 deletions(-) diff --git a/src/types.ts b/src/types.ts index 6008df3..89eda91 100644 --- a/src/types.ts +++ b/src/types.ts @@ -3,7 +3,9 @@ import { RequiredOptions } from 'prettier'; import { PluginConfig } from '../types'; -export interface PrettierOptions extends PluginConfig, RequiredOptions {} +export interface PrettierOptions + extends Required, + RequiredOptions {} export type ImportGroups = Record; export type ImportOrLine = ImportDeclaration | ExpressionStatement; diff --git a/types/index.d.ts b/types/index.d.ts index 43a8944..e789c33 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,13 +1,92 @@ import { Config } from 'prettier'; export interface PluginConfig { + /** + * A collection of Regular expressions in string format. + * + * ``` + * "importOrder": ["^@core/(.*)$", "^@server/(.*)$", "^@ui/(.*)$", "^[./]"], + * ``` + * + * _Default behavior:_ The plugin moves the third party imports to the top which are not part of the `importOrder` list. + * To move the third party imports at desired place, you can use `` to assign third party imports to the appropriate position: + * + * ``` + * "importOrder": ["^@core/(.*)$", "", "^@server/(.*)$", "^@ui/(.*)$", "^[./]"], + * ``` + */ importOrder: string[]; - importOrderCaseInsensitive: boolean; - // should be of type ParserPlugin from '@babel/parser' but prettier does not support nested arrays in options - importOrderParserPlugins: string[]; - importOrderSeparation: boolean; - importOrderGroupNamespaceSpecifiers: boolean; - importOrderSortSpecifiers: boolean; + + /** + * A boolean value to enable or disable the new line separation + * between sorted import declarations group. The separation takes place according to the `importOrder`. + * + * @default false + */ + importOrderSeparation?: boolean; + + /** + * A boolean value to enable or disable sorting of the specifiers in an import declarations. + * + * @default false + */ + importOrderSortSpecifiers?: boolean; + + /** + * A boolean value to enable or disable sorting the namespace specifiers to the top of the import group. + * + * @default false + */ + importOrderGroupNamespaceSpecifiers?: boolean; + + /** + * A boolean value to enable case-insensitivity in the sorting algorithm +used to order imports within each match group. + * + * For example, when false (or not specified): + * + * ```js + * import ExampleView from './ExampleView'; + * import ExamplesList from './ExamplesList'; + * ``` + * + * compared with `"importOrderCaseInsensitive": true`: + * + * ```js + * import ExamplesList from './ExamplesList'; + * import ExampleView from './ExampleView'; + * ``` + * + * @default false + */ + importOrderCaseInsensitive?: boolean; + + /** + * Previously known as `experimentalBabelParserPluginsList`. + * + * A collection of plugins for babel parser. The plugin passes this list to babel parser, so it can understand the syntaxes + * used in the file being formatted. The plugin uses prettier itself to figure out the parser it needs to use but if that fails, + * you can use this field to enforce the usage of the plugins' babel parser needs. + * + * **To pass the plugins to babel parser**: + * + * ``` + * "importOrderParserPlugins" : ["classProperties", "decorators-legacy"] + * ``` + * + * **To pass the options to the babel parser plugins**: Since prettier options are limited to string, you can pass plugins + * with options as a JSON string of the plugin array: + * `"[\"plugin-name\", { \"pluginOption\": true }]"`. + * + * ``` + * "importOrderParserPlugins" : ["classProperties", "[\"decorators\", { \"decoratorsBeforeExport\": true }]"] + * ``` + * + * **To disable default plugins for babel parser, pass an empty array**: + * + * @default ["typescript", "jsx"] + */ + importOrderParserPlugins?: string[]; } export type PrettierConfig = PluginConfig & Config; From 39718ec23973e68a57751a94d71417dc9402a892 Mon Sep 17 00:00:00 2001 From: Nutthapat Pongtanyavichai <59821765+Leomotors@users.noreply.github.com> Date: Sat, 22 Oct 2022 23:15:42 +0700 Subject: [PATCH 2/3] feat: use ParserPlugin types for importOrderParserPlugins --- types/index.d.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/types/index.d.ts b/types/index.d.ts index e789c33..7b8bf51 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,5 +1,8 @@ +import { ParserPlugin as BaseParserPlugin } from '@babel/parser'; import { Config } from 'prettier'; +type ParserPlugin = Extract | `[${string},${string}]`; + export interface PluginConfig { /** * A collection of Regular expressions in string format. @@ -86,7 +89,7 @@ used to order imports within each match group. * * @default ["typescript", "jsx"] */ - importOrderParserPlugins?: string[]; + importOrderParserPlugins?: ParserPlugin[]; } export type PrettierConfig = PluginConfig & Config; From 3e00641e88bd24c5ede1192b2e79256fe7070481 Mon Sep 17 00:00:00 2001 From: Nutthapat Pongtanyavichai <59821765+Leomotors@users.noreply.github.com> Date: Sat, 22 Oct 2022 23:30:50 +0700 Subject: [PATCH 3/3] feat: update ImportOrderParserPlugin type name --- types/index.d.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index 7b8bf51..4c5f02f 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,7 +1,9 @@ -import { ParserPlugin as BaseParserPlugin } from '@babel/parser'; +import { ParserPlugin } from '@babel/parser'; import { Config } from 'prettier'; -type ParserPlugin = Extract | `[${string},${string}]`; +export type ImportOrderParserPlugin = + | Extract + | `[${string},${string}]`; export interface PluginConfig { /** @@ -89,7 +91,7 @@ used to order imports within each match group. * * @default ["typescript", "jsx"] */ - importOrderParserPlugins?: ParserPlugin[]; + importOrderParserPlugins?: ImportOrderParserPlugin[]; } export type PrettierConfig = PluginConfig & Config;