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%
|
target: 80%
|
||||||
project:
|
project:
|
||||||
default:
|
default:
|
||||||
target: auto
|
target: 95%
|
||||||
@@ -88,10 +88,10 @@ module.exports = [
|
|||||||
return eval("require")("../language-css/parser-postcss").parsers.css;
|
return eval("require")("../language-css/parser-postcss").parsers.css;
|
||||||
},
|
},
|
||||||
get less() {
|
get less() {
|
||||||
return eval("require")("../language-css/parser-postcss").parsers.css;
|
return eval("require")("../language-css/parser-postcss").parsers.less;
|
||||||
},
|
},
|
||||||
get scss() {
|
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;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseWithParser(parser, text, options) {
|
function parseWithParser(parse, text, options) {
|
||||||
const parsed = parseFrontMatter(text);
|
const parsed = parseFrontMatter(text);
|
||||||
const { frontMatter } = parsed;
|
const { frontMatter } = parsed;
|
||||||
text = parsed.content;
|
text = parsed.content;
|
||||||
@@ -531,7 +531,7 @@ function parseWithParser(parser, text, options) {
|
|||||||
let result;
|
let result;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
result = parser.parse(text);
|
result = parse(text);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (typeof e.line !== "number") {
|
if (typeof e.line !== "number") {
|
||||||
throw e;
|
throw e;
|
||||||
@@ -550,47 +550,52 @@ function parseWithParser(parser, text, options) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
function requireParser(isSCSSParser) {
|
// TODO: make this only work on css
|
||||||
if (isSCSSParser) {
|
function parseCss(text, parsers, options) {
|
||||||
return require("postcss-scss");
|
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");
|
const lessParser = require("postcss-less");
|
||||||
return {
|
return parseWithParser(
|
||||||
// Workaround for https://github.com/shellscape/postcss-less/issues/145
|
// Workaround for https://github.com/shellscape/postcss-less/issues/145
|
||||||
// See comments for `replaceQuotesInInlineComments` in `loc.js`.
|
// 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) {
|
function parseScss(text, parsers, options) {
|
||||||
const hasExplicitParserChoice =
|
const { parse } = require("postcss-scss");
|
||||||
options.parser === "less" || options.parser === "scss";
|
return parseWithParser(parse, text, options);
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const parser = {
|
const postCssParser = {
|
||||||
parse,
|
|
||||||
astFormat: "postcss",
|
astFormat: "postcss",
|
||||||
hasPragma,
|
hasPragma,
|
||||||
locStart(node) {
|
locStart(node) {
|
||||||
if (node.source) {
|
if (node.source) {
|
||||||
return node.source.startOffset;
|
return node.source.startOffset;
|
||||||
}
|
}
|
||||||
|
/* istanbul ignore next */
|
||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
locEnd(node) {
|
locEnd(node) {
|
||||||
@@ -604,8 +609,17 @@ const parser = {
|
|||||||
// Export as a plugin so we can reuse the same bundle for UMD loading
|
// Export as a plugin so we can reuse the same bundle for UMD loading
|
||||||
module.exports = {
|
module.exports = {
|
||||||
parsers: {
|
parsers: {
|
||||||
css: parser,
|
css: {
|
||||||
less: parser,
|
...postCssParser,
|
||||||
scss: parser,
|
parse: parseCss,
|
||||||
|
},
|
||||||
|
less: {
|
||||||
|
...postCssParser,
|
||||||
|
parse: parseLess,
|
||||||
|
},
|
||||||
|
scss: {
|
||||||
|
...postCssParser,
|
||||||
|
parse: parseScss,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
Reference in new issue
Block a user