Skip to content

Commit

Permalink
better preprocessing types
Browse files Browse the repository at this point in the history
  • Loading branch information
electrovir committed Oct 22, 2023
1 parent d237712 commit 36a65c3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"format": "virmator format",
"publish": "virmator publish \"npm run compile && npm run test:all\"",
"test": "npm run compile && test-as-package \"virmator test\"",
"test:all": "concurrently --kill-others-on-fail --kill-signal SIGKILL -c auto --colors --names types,tests,spelling,format,docs \"npm run test:types\" \"npm run test\" \"npm run test:spelling\" \"npm run test:format\" \"npm run test:docs\"",
"test:all": "npm test && concurrently --kill-others-on-fail --kill-signal SIGKILL -c auto --colors --names types,spelling,format,docs \"npm run test:types\" \"npm run test:spelling\" \"npm run test:format\" \"npm run test:docs\"",
"test:coverage": "npm run test coverage",
"test:debug": "npm run compile && test-as-package \"virmator test --inspect-brk --parallel=false\"",
"test:docs": "virmator docs check",
Expand Down Expand Up @@ -70,6 +70,7 @@
"prettier-plugin-toml": "^1.0.0",
"test-as-package": "^0.0.4",
"ts-node": "^10.9.1",
"type-fest": "^4.5.0",
"typedoc": "^0.25.2",
"typescript": "^5.2.2",
"virmator": "^9.1.3"
Expand Down
41 changes: 27 additions & 14 deletions src/preprocessing.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
import {Parser, ParserOptions, Plugin, Printer} from 'prettier';
import {createWrappedMultiTargetProxy} from 'proxy-vir';
import {SetOptional} from 'type-fest';
import {pluginMarker} from './plugin-marker';
import {multilineArrayPrinter} from './printer/multiline-array-printer';
import {setOriginalPrinter} from './printer/original-printer';

function addMultilinePrinter(options: ParserOptions): void {
/** Prettier's type definitions are not true. */
type ActualParserOptions = SetOptional<ParserOptions, 'plugins'> &
Partial<{
astFormat: string;
printer: Printer;
}>;

function addMultilinePrinter(options: ActualParserOptions): void {
if ('printer' in options) {
setOriginalPrinter((options as any as {printer: Printer}).printer);
setOriginalPrinter(options.printer);
// overwrite the printer with ours
(options as any as {printer: Printer}).printer = multilineArrayPrinter;
options.printer = multilineArrayPrinter;
} else {
const astFormat = (options as any).astFormat;
const astFormat = options.astFormat;
if (!astFormat) {
throw new Error(`Could not find astFormat while adding printer.`);
}
/**
* If the printer hasn't already been assigned in options, rearrange plugins so that ours
* gets chosen.
*/
const plugins = options.plugins;
const plugins = options.plugins ?? [];
const firstMatchedPlugin = plugins.find(
(plugin): plugin is Plugin =>
typeof plugin !== 'string' && !!plugin.printers && !!plugin.printers[astFormat],
Expand All @@ -32,7 +40,7 @@ function addMultilinePrinter(options: ParserOptions): void {
}
setOriginalPrinter(matchedPrinter);
const thisPluginIndex = plugins.findIndex((plugin) => {
return (plugin as any).pluginMarker === pluginMarker;
return (plugin as {pluginMarker: any}).pluginMarker === pluginMarker;
});
const thisPlugin = plugins[thisPluginIndex];
if (!thisPlugin) {
Expand All @@ -45,11 +53,11 @@ function addMultilinePrinter(options: ParserOptions): void {
}
}

function findPluginsByParserName(parserName: string, options: ParserOptions): Plugin[] {
return (options.plugins ?? []).filter((plugin): plugin is Plugin => {
function findPluginsByParserName(parserName: string, plugins: (Plugin | string)[]): Plugin[] {
return plugins.filter((plugin): plugin is Plugin => {
return (
typeof plugin === 'object' &&
(plugin as any).pluginMarker !== pluginMarker &&
(plugin as {pluginMarker: any}).pluginMarker !== pluginMarker &&
!!plugin.parsers?.[parserName]
);
});
Expand All @@ -61,12 +69,17 @@ export function wrapParser(originalParser: Parser, parserName: string) {
initialTarget: originalParser,
});

function multilineArraysPluginPreprocess(text: string, options: ParserOptions) {
const pluginsWithRelevantParsers = findPluginsByParserName(parserName, options);
function multilineArraysPluginPreprocess(text: string, options: ActualParserOptions) {
const pluginsFromOptions = options.plugins ?? [];
const pluginsWithRelevantParsers = findPluginsByParserName(parserName, pluginsFromOptions);
pluginsWithRelevantParsers.forEach((plugin) => {
const currentParser = plugin.parsers?.[parserName];
if (currentParser) {
if ((plugin as any)?.name?.includes('prettier-plugin-sort-json')) {
if (
(plugin as {name?: string | undefined} | undefined)?.name?.includes(
'prettier-plugin-sort-json',
)
) {
parserProxy.proxyModifier.addOverrideTarget(currentParser);
}
}
Expand All @@ -83,8 +96,8 @@ export function wrapParser(originalParser: Parser, parserName: string) {
processedText,
{
...options,
plugins: options.plugins.filter(
(plugin) => (plugin as any).pluginMarker !== pluginMarker,
plugins: pluginsFromOptions.filter(
(plugin) => (plugin as {pluginMarker: any}).pluginMarker !== pluginMarker,
),
},
);
Expand Down

0 comments on commit 36a65c3

Please sign in to comment.