Make options argument in parsers optional (#11892)

This commit is contained in:
fisker Cheung authored and sosukesuzuki committed 2021-12-04 22:21:51 +09:00
1 parent 99649c7fd2
commit e7a74529a5
7 files changed
+57 -22

No files matched your search

+3 -3
View File
@@ -619,7 +619,7 @@ function parseWithParser(parse, text, options) {
}
// TODO: make this only work on css
function parseCss(text, parsers, options) {
function parseCss(text, parsers, options = {}) {
const isSCSSParser = isSCSS(options.parser, text);
const parseFunctions = isSCSSParser
? [parseScss, parseLess]
@@ -640,7 +640,7 @@ function parseCss(text, parsers, options) {
}
}
function parseLess(text, parsers, options) {
function parseLess(text, parsers, options = {}) {
const lessParser = require("postcss-less");
return parseWithParser(
// Workaround for https://github.com/shellscape/postcss-less/issues/145
@@ -651,7 +651,7 @@ function parseLess(text, parsers, options) {
);
}
function parseScss(text, parsers, options) {
function parseScss(text, parsers, options = {}) {
const { parse } = require("postcss-scss");
return parseWithParser(parse, text, options);
}
+18 -10
View File
@@ -25,6 +25,7 @@ const { locStart, locEnd } = require("./loc.js");
* @typedef {import('angular-html-parser/lib/compiler/src/ml_parser/ast').Element} Element
* @typedef {import('angular-html-parser/lib/compiler/src/ml_parser/parser').ParseTreeResult} ParserTreeResult
* @typedef {Omit<import('angular-html-parser').ParseOptions, 'canSelfClose'> & {
* name?: 'html' | 'angular' | 'vue' | 'lwc';
* recognizeSelfClosing?: boolean;
* normalizeTagName?: boolean;
* normalizeAttributeName?: boolean;
@@ -365,6 +366,7 @@ function _parse(text, options, parserOptions, shouldParseFrontMatter = true) {
* @param {ParserOptions} parserOptions
*/
function createParser({
name,
recognizeSelfClosing = false,
normalizeTagName = false,
normalizeAttributeName = false,
@@ -374,14 +376,18 @@ function createParser({
} = {}) {
return {
parse: (text, parsers, options) =>
_parse(text, options, {
recognizeSelfClosing,
normalizeTagName,
normalizeAttributeName,
allowHtmComponentClosingTags,
isTagNameCaseSensitive,
getTagContentType,
}),
_parse(
text,
{ parser: name, ...options },
{
recognizeSelfClosing,
normalizeTagName,
normalizeAttributeName,
allowHtmComponentClosingTags,
isTagNameCaseSensitive,
getTagContentType,
}
),
hasPragma,
astFormat: "html",
locStart,
@@ -392,13 +398,15 @@ function createParser({
module.exports = {
parsers: {
html: createParser({
name: "html",
recognizeSelfClosing: true,
normalizeTagName: true,
normalizeAttributeName: true,
allowHtmComponentClosingTags: true,
}),
angular: createParser(),
angular: createParser({ name: "angular" }),
vue: createParser({
name: "vue",
recognizeSelfClosing: true,
isTagNameCaseSensitive: true,
getTagContentType: (tagName, prefix, hasParent, attrs) => {
@@ -414,6 +422,6 @@ module.exports = {
}
},
}),
lwc: createParser(),
lwc: createParser({ name: "lwc" }),
},
};
+1 -1
View File
@@ -32,7 +32,7 @@ function createParseError(error) {
return createError(message, { start: { line: lineNumber, column } });
}
function parse(originalText, parsers, options) {
function parse(originalText, parsers, options = {}) {
const { parse } = require("espree");
const textToParse = replaceHashbang(originalText);
+3 -3
View File
@@ -45,7 +45,7 @@ function createParseError(error) {
});
}
function parse(text, parsers, opts) {
function parse(text, parsers, options = {}) {
// Inline the require to avoid loading all the JS if we don't use it
const { parse } = require("flow-parser");
const ast = parse(replaceHashbang(text), parseOptions);
@@ -54,8 +54,8 @@ function parse(text, parsers, opts) {
throw createParseError(error);
}
opts.originalText = text;
return postprocess(ast, opts);
options.originalText = text;
return postprocess(ast, options);
}
// Export as a plugin so we can reuse the same bundle for UMD loading
+1 -1
View File
@@ -71,7 +71,7 @@ function createParseError(error) {
return createError(message, { start: { line, column } });
}
function parse(text, parsers, options) {
function parse(text, parsers, options = {}) {
const { result: ast, error: moduleParseError } = tryCombinations(
() => parseWithOptions(text, /* module */ true),
() => parseWithOptions(text, /* module */ false)
+4 -4
View File
@@ -32,7 +32,7 @@ function createParseError(error) {
});
}
function parse(text, parsers, opts) {
function parse(text, parsers, options = {}) {
const textToParse = replaceHashbang(text);
const jsx = isProbablyJsx(text);
@@ -49,9 +49,9 @@ function parse(text, parsers, opts) {
throw createParseError(firstError);
}
opts.originalText = text;
opts.tsParseResult = result;
return postprocess(result.ast, opts);
options.originalText = text;
options.tsParseResult = result;
return postprocess(result.ast, options);
}
/**
+27
View File
@@ -29,6 +29,33 @@ test("allows usage of prettier's supported parsers", () => {
expect(output).toBe("bar();\n");
});
test("parsers should allow omit optional arguments", () => {
let parsers;
try {
prettier.format("{}", {
parser(text, builtinParsers) {
parsers = builtinParsers;
},
});
} catch {
// noop
}
expect(typeof parsers.babel).toBe("function");
const code = {
graphql: "type A {hero: Character}",
default: "{}",
};
for (const [name, parse] of Object.entries(parsers)) {
// Private parser should not be used by users
if (name.startsWith("__")) {
continue;
}
expect(() => parse(code[name] || code.default)).not.toThrow();
}
});
test("allows add empty `trailingComments` array", () => {
const output = prettier.format("(foo /* comment */)( )", {
parser(text, parsers) {