diff --git a/.codecov.yml b/.codecov.yml index e22022cc3..1a43c767b 100644 --- a/.codecov.yml +++ b/.codecov.yml @@ -7,4 +7,4 @@ coverage: target: 80% project: default: - target: auto + target: 95% diff --git a/src/common/internal-plugins.js b/src/common/internal-plugins.js index 16ed6da3c..be59adc22 100644 --- a/src/common/internal-plugins.js +++ b/src/common/internal-plugins.js @@ -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; }, }, }, diff --git a/src/language-css/parser-postcss.js b/src/language-css/parser-postcss.js index ae679d691..186de9959 100644 --- a/src/language-css/parser-postcss.js +++ b/src/language-css/parser-postcss.js @@ -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, + }, }, };