From 36a65c39b0fc214a9b559d4211326cb2f77e41ec Mon Sep 17 00:00:00 2001 From: electrovir Date: Sun, 22 Oct 2023 15:56:39 +0000 Subject: [PATCH] better preprocessing types --- package-lock.json | 1 + package.json | 3 ++- src/preprocessing.ts | 41 +++++++++++++++++++++++++++-------------- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3cd77c7..ddfdc5a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -38,6 +38,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" diff --git a/package.json b/package.json index 3a4ae57..6c1f9a1 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" diff --git a/src/preprocessing.ts b/src/preprocessing.ts index ceb02a9..ce3453f 100644 --- a/src/preprocessing.ts +++ b/src/preprocessing.ts @@ -1,16 +1,24 @@ 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 & + 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.`); } @@ -18,7 +26,7 @@ function addMultilinePrinter(options: ParserOptions): void { * 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], @@ -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) { @@ -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] ); }); @@ -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); } } @@ -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, ), }, );