MCPcopy
hub / github.com/prettier/prettier / normalizeFormatOptions

Function normalizeFormatOptions

src/main/normalize-format-options.js:22–97  ·  view source on GitHub ↗
(options, opts = {})

Source from the content-addressed store, hash-verified

20
21// Copy options and fill in default values.
22async function normalizeFormatOptions(options, opts = {}) {
23 const rawOptions = { ...options };
24
25 if (!rawOptions.parser) {
26 if (!rawOptions.filepath) {
27 throw new UndefinedParserError(
28 "No parser and no file path given, couldn't infer a parser.",
29 );
30 }
31
32 rawOptions.parser = inferParser(rawOptions, {
33 physicalFile: rawOptions.filepath,
34 });
35
36 if (!rawOptions.parser) {
37 throw new UndefinedParserError(
38 `No parser could be inferred for file "${rawOptions.filepath}".`,
39 );
40 }
41 }
42
43 const supportOptions = getSupportInfo({
44 plugins: options.plugins,
45 showDeprecated: true,
46 }).options;
47
48 const defaults = {
49 ...formatOptionsHiddenDefaults,
50 ...Object.fromEntries(
51 supportOptions
52 .filter((optionInfo) => optionInfo.default !== undefined)
53 .map((option) => [option.name, option.default]),
54 ),
55 };
56
57 const parserPlugin = getParserPluginByParserName(
58 rawOptions.plugins,
59 rawOptions.parser,
60 );
61
62 const parser = await initParser(parserPlugin, rawOptions.parser);
63 rawOptions.astFormat = parser.astFormat;
64 rawOptions.locEnd = parser.locEnd;
65 rawOptions.locStart = parser.locStart;
66
67 const printerPlugin = parserPlugin.printers?.[parser.astFormat]
68 ? parserPlugin
69 : getPrinterPluginByAstFormat(rawOptions.plugins, parser.astFormat);
70 const printer = await initPrinter(printerPlugin, parser.astFormat);
71
72 rawOptions.printer = printer;
73 rawOptions.getVisitorKeys = printer.getVisitorKeys;
74
75 const pluginDefaults = printerPlugin.defaultOptions
76 ? Object.fromEntries(
77 Object.entries(printerPlugin.defaultOptions).filter(
78 ([, value]) => value !== undefined,
79 ),

Callers 6

formatWithCursorFunction · 0.85
parseFunction · 0.85
formatAstFunction · 0.85
printToDocFunction · 0.85
printDocToStringFunction · 0.85
textToDocFunction · 0.85

Calls 8

getSupportInfoFunction · 0.90
initParserFunction · 0.90
initPrinterFunction · 0.90
inferParserFunction · 0.85
normalizeOptionsFunction · 0.85
mapMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…