Split css parsers (#7932)
* Split css parsers * pass function to parseWithParser * Fix plugins parser * Fix coverage fails * coverage ignore * Update project coverage target to 95%
This commit is contained in:
1 parent
bcf99ade8d
commit
17377f3082
3 files changed
+48
-34
No files matched your search
+1
-1
@@ -7,4 +7,4 @@ coverage:
|
||||
target: 80%
|
||||
project:
|
||||
default:
|
||||
target: auto
|
||||
target: 95%
|
||||
@@ -88,10 +88,10 @@ module.exports = [
|
||||
return eval("require")("../language-css/parser-postcss").parsers.css;
|
||||
},
|
||||
get less() {
|
||||
return eval("require")("../language-css/parser-postcss").parsers.css;
|
||||
return eval("require")("../language-css/parser-postcss").parsers.less;
|
||||
},
|
||||
get scss() {
|
||||
return eval("require")("../language-css/parser-postcss").parsers.css;
|
||||
return eval("require")("../language-css/parser-postcss").parsers.scss;
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
@@ -523,7 +523,7 @@ function parseNestedCSS(node, options) {
|
||||
return node;
|
||||
}
|
||||
|
||||
function parseWithParser(parser, text, options) {
|
||||
function parseWithParser(parse, text, options) {
|
||||
const parsed = parseFrontMatter(text);
|
||||
const { frontMatter } = parsed;
|
||||
text = parsed.content;
|
||||
@@ -531,7 +531,7 @@ function parseWithParser(parser, text, options) {
|
||||
let result;
|
||||
|
||||
try {
|
||||
result = parser.parse(text);
|
||||
result = parse(text);
|
||||
} catch (e) {
|
||||
if (typeof e.line !== "number") {
|
||||
throw e;
|
||||
@@ -550,47 +550,52 @@ function parseWithParser(parser, text, options) {
|
||||
return result;
|
||||
}
|
||||
|
||||
function requireParser(isSCSSParser) {
|
||||
if (isSCSSParser) {
|
||||
return require("postcss-scss");
|
||||
// TODO: make this only work on css
|
||||
function parseCss(text, parsers, options) {
|
||||
const isSCSSParser = isSCSS(options.parser, text);
|
||||
const parseFunctions = isSCSSParser
|
||||
? [parseScss, parseLess]
|
||||
: [parseLess, parseScss];
|
||||
|
||||
let error;
|
||||
for (const parse of parseFunctions) {
|
||||
try {
|
||||
return parse(text, parsers, options);
|
||||
} catch (parseError) {
|
||||
error = error || parseError;
|
||||
}
|
||||
}
|
||||
|
||||
/* istanbul ignore next */
|
||||
if (error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
function parseLess(text, parsers, options) {
|
||||
const lessParser = require("postcss-less");
|
||||
return {
|
||||
return parseWithParser(
|
||||
// Workaround for https://github.com/shellscape/postcss-less/issues/145
|
||||
// See comments for `replaceQuotesInInlineComments` in `loc.js`.
|
||||
parse: (text) => lessParser.parse(replaceQuotesInInlineComments(text)),
|
||||
};
|
||||
(text) => lessParser.parse(replaceQuotesInInlineComments(text)),
|
||||
text,
|
||||
options
|
||||
);
|
||||
}
|
||||
|
||||
function parse(text, parsers, options) {
|
||||
const hasExplicitParserChoice =
|
||||
options.parser === "less" || options.parser === "scss";
|
||||
const isSCSSParser = isSCSS(options.parser, text);
|
||||
|
||||
try {
|
||||
return parseWithParser(requireParser(isSCSSParser), text, options);
|
||||
} catch (originalError) {
|
||||
if (hasExplicitParserChoice) {
|
||||
throw originalError;
|
||||
}
|
||||
|
||||
try {
|
||||
return parseWithParser(requireParser(!isSCSSParser), text, options);
|
||||
} catch (_secondError) {
|
||||
throw originalError;
|
||||
}
|
||||
}
|
||||
function parseScss(text, parsers, options) {
|
||||
const { parse } = require("postcss-scss");
|
||||
return parseWithParser(parse, text, options);
|
||||
}
|
||||
|
||||
const parser = {
|
||||
parse,
|
||||
const postCssParser = {
|
||||
astFormat: "postcss",
|
||||
hasPragma,
|
||||
locStart(node) {
|
||||
if (node.source) {
|
||||
return node.source.startOffset;
|
||||
}
|
||||
/* istanbul ignore next */
|
||||
return null;
|
||||
},
|
||||
locEnd(node) {
|
||||
@@ -604,8 +609,17 @@ const parser = {
|
||||
// Export as a plugin so we can reuse the same bundle for UMD loading
|
||||
module.exports = {
|
||||
parsers: {
|
||||
css: parser,
|
||||
less: parser,
|
||||
scss: parser,
|
||||
css: {
|
||||
...postCssParser,
|
||||
parse: parseCss,
|
||||
},
|
||||
less: {
|
||||
...postCssParser,
|
||||
parse: parseLess,
|
||||
},
|
||||
scss: {
|
||||
...postCssParser,
|
||||
parse: parseScss,
|
||||
},
|
||||
},
|
||||
};
|
||||
Reference in new issue
Block a user