Skip to content

Commit e7a7452

Browse files
fiskersosukesuzuki
authored andcommitted
Make options argument in parsers optional (#11892)
1 parent 99649c7 commit e7a7452

7 files changed

Lines changed: 57 additions & 22 deletions

File tree

‎src/language-css/parser-postcss.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ function parseWithParser(parse, text, options) {
619619
}
620620

621621
// TODO: make this only work on css
622-
function parseCss(text, parsers, options) {
622+
function parseCss(text, parsers, options = {}) {
623623
const isSCSSParser = isSCSS(options.parser, text);
624624
const parseFunctions = isSCSSParser
625625
? [parseScss, parseLess]
@@ -640,7 +640,7 @@ function parseCss(text, parsers, options) {
640640
}
641641
}
642642

643-
function parseLess(text, parsers, options) {
643+
function parseLess(text, parsers, options = {}) {
644644
const lessParser = require("postcss-less");
645645
return parseWithParser(
646646
// Workaround for https://github.com/shellscape/postcss-less/issues/145
@@ -651,7 +651,7 @@ function parseLess(text, parsers, options) {
651651
);
652652
}
653653

654-
function parseScss(text, parsers, options) {
654+
function parseScss(text, parsers, options = {}) {
655655
const { parse } = require("postcss-scss");
656656
return parseWithParser(parse, text, options);
657657
}

‎src/language-html/parser-html.js‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const { locStart, locEnd } = require("./loc.js");
2525
* @typedef {import('angular-html-parser/lib/compiler/src/ml_parser/ast').Element} Element
2626
* @typedef {import('angular-html-parser/lib/compiler/src/ml_parser/parser').ParseTreeResult} ParserTreeResult
2727
* @typedef {Omit<import('angular-html-parser').ParseOptions, 'canSelfClose'> & {
28+
* name?: 'html' | 'angular' | 'vue' | 'lwc';
2829
* recognizeSelfClosing?: boolean;
2930
* normalizeTagName?: boolean;
3031
* normalizeAttributeName?: boolean;
@@ -365,6 +366,7 @@ function _parse(text, options, parserOptions, shouldParseFrontMatter = true) {
365366
* @param {ParserOptions} parserOptions
366367
*/
367368
function createParser({
369+
name,
368370
recognizeSelfClosing = false,
369371
normalizeTagName = false,
370372
normalizeAttributeName = false,
@@ -374,14 +376,18 @@ function createParser({
374376
} = {}) {
375377
return {
376378
parse: (text, parsers, options) =>
377-
_parse(text, options, {
378-
recognizeSelfClosing,
379-
normalizeTagName,
380-
normalizeAttributeName,
381-
allowHtmComponentClosingTags,
382-
isTagNameCaseSensitive,
383-
getTagContentType,
384-
}),
379+
_parse(
380+
text,
381+
{ parser: name, ...options },
382+
{
383+
recognizeSelfClosing,
384+
normalizeTagName,
385+
normalizeAttributeName,
386+
allowHtmComponentClosingTags,
387+
isTagNameCaseSensitive,
388+
getTagContentType,
389+
}
390+
),
385391
hasPragma,
386392
astFormat: "html",
387393
locStart,
@@ -392,13 +398,15 @@ function createParser({
392398
module.exports = {
393399
parsers: {
394400
html: createParser({
401+
name: "html",
395402
recognizeSelfClosing: true,
396403
normalizeTagName: true,
397404
normalizeAttributeName: true,
398405
allowHtmComponentClosingTags: true,
399406
}),
400-
angular: createParser(),
407+
angular: createParser({ name: "angular" }),
401408
vue: createParser({
409+
name: "vue",
402410
recognizeSelfClosing: true,
403411
isTagNameCaseSensitive: true,
404412
getTagContentType: (tagName, prefix, hasParent, attrs) => {
@@ -414,6 +422,6 @@ module.exports = {
414422
}
415423
},
416424
}),
417-
lwc: createParser(),
425+
lwc: createParser({ name: "lwc" }),
418426
},
419427
};

‎src/language-js/parse/espree.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function createParseError(error) {
3232
return createError(message, { start: { line: lineNumber, column } });
3333
}
3434

35-
function parse(originalText, parsers, options) {
35+
function parse(originalText, parsers, options = {}) {
3636
const { parse } = require("espree");
3737

3838
const textToParse = replaceHashbang(originalText);

‎src/language-js/parse/flow.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function createParseError(error) {
4545
});
4646
}
4747

48-
function parse(text, parsers, opts) {
48+
function parse(text, parsers, options = {}) {
4949
// Inline the require to avoid loading all the JS if we don't use it
5050
const { parse } = require("flow-parser");
5151
const ast = parse(replaceHashbang(text), parseOptions);
@@ -54,8 +54,8 @@ function parse(text, parsers, opts) {
5454
throw createParseError(error);
5555
}
5656

57-
opts.originalText = text;
58-
return postprocess(ast, opts);
57+
options.originalText = text;
58+
return postprocess(ast, options);
5959
}
6060

6161
// Export as a plugin so we can reuse the same bundle for UMD loading

‎src/language-js/parse/meriyah.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function createParseError(error) {
7171
return createError(message, { start: { line, column } });
7272
}
7373

74-
function parse(text, parsers, options) {
74+
function parse(text, parsers, options = {}) {
7575
const { result: ast, error: moduleParseError } = tryCombinations(
7676
() => parseWithOptions(text, /* module */ true),
7777
() => parseWithOptions(text, /* module */ false)

‎src/language-js/parse/typescript.js‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function createParseError(error) {
3232
});
3333
}
3434

35-
function parse(text, parsers, opts) {
35+
function parse(text, parsers, options = {}) {
3636
const textToParse = replaceHashbang(text);
3737
const jsx = isProbablyJsx(text);
3838

@@ -49,9 +49,9 @@ function parse(text, parsers, opts) {
4949
throw createParseError(firstError);
5050
}
5151

52-
opts.originalText = text;
53-
opts.tsParseResult = result;
54-
return postprocess(result.ast, opts);
52+
options.originalText = text;
53+
options.tsParseResult = result;
54+
return postprocess(result.ast, options);
5555
}
5656

5757
/**

‎tests/integration/__tests__/parser-api.js‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,33 @@ test("allows usage of prettier's supported parsers", () => {
2929
expect(output).toBe("bar();\n");
3030
});
3131

32+
test("parsers should allow omit optional arguments", () => {
33+
let parsers;
34+
try {
35+
prettier.format("{}", {
36+
parser(text, builtinParsers) {
37+
parsers = builtinParsers;
38+
},
39+
});
40+
} catch {
41+
// noop
42+
}
43+
44+
expect(typeof parsers.babel).toBe("function");
45+
const code = {
46+
graphql: "type A {hero: Character}",
47+
default: "{}",
48+
};
49+
for (const [name, parse] of Object.entries(parsers)) {
50+
// Private parser should not be used by users
51+
if (name.startsWith("__")) {
52+
continue;
53+
}
54+
55+
expect(() => parse(code[name] || code.default)).not.toThrow();
56+
}
57+
});
58+
3259
test("allows add empty `trailingComments` array", () => {
3360
const output = prettier.format("(foo /* comment */)( )", {
3461
parser(text, parsers) {

0 commit comments

Comments
 (0)