forked from prettier/prettier
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.js
195 lines (169 loc) · 5.21 KB
/
options.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
"use strict";
const fs = require("fs");
const path = require("path");
const readlines = require("n-readlines");
const fromPairs = require("lodash/fromPairs");
const { UndefinedParserError } = require("../common/errors");
const { getSupportInfo } = require("../main/support");
const normalizer = require("./options-normalizer");
const { resolveParser } = require("./parser");
const hiddenDefaults = {
astFormat: "estree",
printer: {},
originalText: undefined,
locStart: null,
locEnd: null,
};
// Copy options and fill in default values.
function normalize(options, opts) {
opts = opts || {};
const rawOptions = { ...options };
const supportOptions = getSupportInfo({
plugins: options.plugins,
showUnreleased: true,
showDeprecated: true,
}).options;
const defaults = {
...hiddenDefaults,
...fromPairs(
supportOptions
.filter((optionInfo) => optionInfo.default !== undefined)
.map((option) => [option.name, option.default])
),
};
if (!rawOptions.parser) {
if (!rawOptions.filepath) {
const logger = opts.logger || console;
logger.warn(
"No parser and no filepath given, using 'babel' the parser now " +
"but this will throw an error in the future. " +
"Please specify a parser or a filepath so one can be inferred."
);
rawOptions.parser = "babel";
} else {
rawOptions.parser = inferParser(rawOptions.filepath, rawOptions.plugins);
if (!rawOptions.parser) {
throw new UndefinedParserError(
`No parser could be inferred for file: ${rawOptions.filepath}`
);
}
}
}
const parser = resolveParser(
normalizer.normalizeApiOptions(
rawOptions,
[supportOptions.find((x) => x.name === "parser")],
{ passThrough: true, logger: false }
)
);
rawOptions.astFormat = parser.astFormat;
rawOptions.locEnd = parser.locEnd;
rawOptions.locStart = parser.locStart;
const plugin = getPlugin(rawOptions);
rawOptions.printer = plugin.printers[rawOptions.astFormat];
const pluginDefaults = supportOptions
.filter(
(optionInfo) =>
optionInfo.pluginDefaults &&
optionInfo.pluginDefaults[plugin.name] !== undefined
)
.reduce(
(reduced, optionInfo) =>
Object.assign(reduced, {
[optionInfo.name]: optionInfo.pluginDefaults[plugin.name],
}),
{}
);
const mixedDefaults = { ...defaults, ...pluginDefaults };
Object.keys(mixedDefaults).forEach((k) => {
if (rawOptions[k] == null) {
rawOptions[k] = mixedDefaults[k];
}
});
if (rawOptions.parser === "json") {
rawOptions.trailingComma = "none";
}
return normalizer.normalizeApiOptions(rawOptions, supportOptions, {
passThrough: Object.keys(hiddenDefaults),
...opts,
});
}
function getPlugin(options) {
const { astFormat } = options;
if (!astFormat) {
throw new Error("getPlugin() requires astFormat to be set");
}
const printerPlugin = options.plugins.find(
(plugin) => plugin.printers && plugin.printers[astFormat]
);
if (!printerPlugin) {
throw new Error(`Couldn't find plugin for AST format "${astFormat}"`);
}
return printerPlugin;
}
function getInterpreter(filepath) {
if (typeof filepath !== "string") {
return "";
}
let fd;
try {
fd = fs.openSync(filepath, "r");
} catch (err) {
// istanbul ignore next
return "";
}
try {
const liner = new readlines(fd);
const firstLine = liner.next().toString("utf8");
// #!/bin/env node, #!/usr/bin/env node
const m1 = firstLine.match(/^#!\/(?:usr\/)?bin\/env\s+(\S+)/);
if (m1) {
return m1[1];
}
// #!/bin/node, #!/usr/bin/node, #!/usr/local/bin/node
const m2 = firstLine.match(/^#!\/(?:usr\/(?:local\/)?)?bin\/(\S+)/);
if (m2) {
return m2[1];
}
return "";
} catch (err) {
// There are some weird cases where paths are missing, causing Jest
// failures. It's unclear what these correspond to in the real world.
return "";
} finally {
try {
// There are some weird cases where paths are missing, causing Jest
// failures. It's unclear what these correspond to in the real world.
fs.closeSync(fd);
} catch (err) {
// nop
}
}
}
function inferParser(filepath, plugins) {
const filename = path.basename(filepath).toLowerCase();
const languages = getSupportInfo({ plugins }).languages.filter(
(language) => language.since !== null
);
// If the file has no extension, we can try to infer the language from the
// interpreter in the shebang line, if any; but since this requires FS access,
// do it last.
let language = languages.find(
(language) =>
(language.extensions &&
language.extensions.some((extension) =>
filename.endsWith(extension)
)) ||
(language.filenames &&
language.filenames.some((name) => name.toLowerCase() === filename))
);
if (!language && !filename.includes(".")) {
const interpreter = getInterpreter(filepath);
language = languages.find(
(language) =>
language.interpreters && language.interpreters.includes(interpreter)
);
}
return language && language.parsers[0];
}
module.exports = { normalize, hiddenDefaults, inferParser };