prettier-plugin-imports
Version:
A prettier plugins to sort imports in provided RegEx order
78 lines (74 loc) • 2.28 kB
text/typescript
import { parsers as babelParsers } from 'prettier/plugins/babel';
import { parsers as flowParsers } from 'prettier/plugins/flow';
import { parsers as htmlParsers } from 'prettier/plugins/html';
import { parsers as typescriptParsers } from 'prettier/plugins/typescript';
import { DEFAULT_IMPORT_ORDER } from './constants';
import { defaultPreprocessor } from './preprocessors/default';
import { vuePreprocessor } from './preprocessors/vue';
import type { PrettierOptions } from './types';
import type { RequiredOptions as PrettierRequiredOptions } from 'prettier';
// Not sure what the type from Prettier should be, but this is a good enough start.
interface PrettierOptionSchema {
type: string;
category: 'Global';
array?: boolean;
default: unknown;
description: string;
}
export const options: Record<
Exclude<keyof PrettierOptions, keyof PrettierRequiredOptions>,
PrettierOptionSchema
> = {
importOrder: {
type: 'path',
category: 'Global',
array: true,
default: [{ value: DEFAULT_IMPORT_ORDER }],
description:
'Provide an order to sort imports. [node.js built-ins are always first]',
},
importOrderParserPlugins: {
type: 'path',
category: 'Global',
array: true,
// By default, we add ts and jsx as parsers but if users define something
// we take that option
default: [{ value: ['typescript', 'jsx'] }],
description: 'Provide a list of plugins for special syntax',
},
importOrderTypeScriptVersion: {
type: 'string',
category: 'Global',
default: '1.0.0',
description:
'Version of TypeScript in use in the project. Determines some output syntax when using TypeScript.',
},
importOrderCaseSensitive: {
type: 'boolean',
category: 'Global',
default: false,
description: 'Provide a case sensitivity boolean flag',
},
};
export const parsers = {
babel: {
...babelParsers.babel,
preprocess: defaultPreprocessor,
},
'babel-ts': {
...babelParsers['babel-ts'],
preprocess: defaultPreprocessor,
},
flow: {
...flowParsers.flow,
preprocess: defaultPreprocessor,
},
typescript: {
...typescriptParsers.typescript,
preprocess: defaultPreprocessor,
},
vue: {
...htmlParsers.vue,
preprocess: vuePreprocessor,
},
};