Skip to content

Commit 36a65c3

Browse files
committed
better preprocessing types
1 parent d237712 commit 36a65c3

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

Diff for: package-lock.json

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"format": "virmator format",
3333
"publish": "virmator publish \"npm run compile && npm run test:all\"",
3434
"test": "npm run compile && test-as-package \"virmator test\"",
35-
"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\"",
35+
"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\"",
3636
"test:coverage": "npm run test coverage",
3737
"test:debug": "npm run compile && test-as-package \"virmator test --inspect-brk --parallel=false\"",
3838
"test:docs": "virmator docs check",
@@ -70,6 +70,7 @@
7070
"prettier-plugin-toml": "^1.0.0",
7171
"test-as-package": "^0.0.4",
7272
"ts-node": "^10.9.1",
73+
"type-fest": "^4.5.0",
7374
"typedoc": "^0.25.2",
7475
"typescript": "^5.2.2",
7576
"virmator": "^9.1.3"

Diff for: src/preprocessing.ts

+27-14
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,32 @@
11
import {Parser, ParserOptions, Plugin, Printer} from 'prettier';
22
import {createWrappedMultiTargetProxy} from 'proxy-vir';
3+
import {SetOptional} from 'type-fest';
34
import {pluginMarker} from './plugin-marker';
45
import {multilineArrayPrinter} from './printer/multiline-array-printer';
56
import {setOriginalPrinter} from './printer/original-printer';
67

7-
function addMultilinePrinter(options: ParserOptions): void {
8+
/** Prettier's type definitions are not true. */
9+
type ActualParserOptions = SetOptional<ParserOptions, 'plugins'> &
10+
Partial<{
11+
astFormat: string;
12+
printer: Printer;
13+
}>;
14+
15+
function addMultilinePrinter(options: ActualParserOptions): void {
816
if ('printer' in options) {
9-
setOriginalPrinter((options as any as {printer: Printer}).printer);
17+
setOriginalPrinter(options.printer);
1018
// overwrite the printer with ours
11-
(options as any as {printer: Printer}).printer = multilineArrayPrinter;
19+
options.printer = multilineArrayPrinter;
1220
} else {
13-
const astFormat = (options as any).astFormat;
21+
const astFormat = options.astFormat;
1422
if (!astFormat) {
1523
throw new Error(`Could not find astFormat while adding printer.`);
1624
}
1725
/**
1826
* If the printer hasn't already been assigned in options, rearrange plugins so that ours
1927
* gets chosen.
2028
*/
21-
const plugins = options.plugins;
29+
const plugins = options.plugins ?? [];
2230
const firstMatchedPlugin = plugins.find(
2331
(plugin): plugin is Plugin =>
2432
typeof plugin !== 'string' && !!plugin.printers && !!plugin.printers[astFormat],
@@ -32,7 +40,7 @@ function addMultilinePrinter(options: ParserOptions): void {
3240
}
3341
setOriginalPrinter(matchedPrinter);
3442
const thisPluginIndex = plugins.findIndex((plugin) => {
35-
return (plugin as any).pluginMarker === pluginMarker;
43+
return (plugin as {pluginMarker: any}).pluginMarker === pluginMarker;
3644
});
3745
const thisPlugin = plugins[thisPluginIndex];
3846
if (!thisPlugin) {
@@ -45,11 +53,11 @@ function addMultilinePrinter(options: ParserOptions): void {
4553
}
4654
}
4755

48-
function findPluginsByParserName(parserName: string, options: ParserOptions): Plugin[] {
49-
return (options.plugins ?? []).filter((plugin): plugin is Plugin => {
56+
function findPluginsByParserName(parserName: string, plugins: (Plugin | string)[]): Plugin[] {
57+
return plugins.filter((plugin): plugin is Plugin => {
5058
return (
5159
typeof plugin === 'object' &&
52-
(plugin as any).pluginMarker !== pluginMarker &&
60+
(plugin as {pluginMarker: any}).pluginMarker !== pluginMarker &&
5361
!!plugin.parsers?.[parserName]
5462
);
5563
});
@@ -61,12 +69,17 @@ export function wrapParser(originalParser: Parser, parserName: string) {
6169
initialTarget: originalParser,
6270
});
6371

64-
function multilineArraysPluginPreprocess(text: string, options: ParserOptions) {
65-
const pluginsWithRelevantParsers = findPluginsByParserName(parserName, options);
72+
function multilineArraysPluginPreprocess(text: string, options: ActualParserOptions) {
73+
const pluginsFromOptions = options.plugins ?? [];
74+
const pluginsWithRelevantParsers = findPluginsByParserName(parserName, pluginsFromOptions);
6675
pluginsWithRelevantParsers.forEach((plugin) => {
6776
const currentParser = plugin.parsers?.[parserName];
6877
if (currentParser) {
69-
if ((plugin as any)?.name?.includes('prettier-plugin-sort-json')) {
78+
if (
79+
(plugin as {name?: string | undefined} | undefined)?.name?.includes(
80+
'prettier-plugin-sort-json',
81+
)
82+
) {
7083
parserProxy.proxyModifier.addOverrideTarget(currentParser);
7184
}
7285
}
@@ -83,8 +96,8 @@ export function wrapParser(originalParser: Parser, parserName: string) {
8396
processedText,
8497
{
8598
...options,
86-
plugins: options.plugins.filter(
87-
(plugin) => (plugin as any).pluginMarker !== pluginMarker,
99+
plugins: pluginsFromOptions.filter(
100+
(plugin) => (plugin as {pluginMarker: any}).pluginMarker !== pluginMarker,
88101
),
89102
},
90103
);

0 commit comments

Comments
 (0)