2023-02-18 11:28:58 +08:00
|
|
|
import postcssParse from "postcss/lib/parse";
|
|
|
|
|
import postcssLess from "postcss-less";
|
|
|
|
|
import postcssScssParse from "postcss-scss/lib/scss-parse";
|
2022-02-21 14:22:51 +08:00
|
|
|
import createError from "../common/parser-create-error.js";
|
|
|
|
|
import parseFrontMatter from "../utils/front-matter/parse.js";
|
|
|
|
|
import { hasPragma } from "./pragma.js";
|
2023-02-13 10:14:28 +08:00
|
|
|
import {
|
|
|
|
|
locStart,
|
|
|
|
|
locEnd,
|
|
|
|
|
calculateLoc,
|
|
|
|
|
replaceQuotesInInlineComments,
|
|
|
|
|
} from "./loc.js";
|
2022-02-21 14:22:51 +08:00
|
|
|
import isSCSSNestedPropertyNode from "./utils/is-scss-nested-property-node.js";
|
|
|
|
|
import isModuleRuleName from "./utils/is-module-rule-name.js";
|
2023-02-21 16:35:26 +08:00
|
|
|
import parseValue from "./parse/parse-value.js";
|
|
|
|
|
import parseSelector from "./parse/parse-selector.js";
|
|
|
|
|
import parseMediaQuery from "./parse/parse-media-query.js";
|
|
|
|
|
import { addTypePrefix } from "./parse/utils.js";
|
2017-05-28 21:06:09 -07:00
|
|
|
|
2021-12-07 14:15:03 +08:00
|
|
|
const DEFAULT_SCSS_DIRECTIVE = /(\s*)(!default).*$/;
|
|
|
|
|
const GLOBAL_SCSS_DIRECTIVE = /(\s*)(!global).*$/;
|
2018-01-12 14:12:02 +03:00
|
|
|
|
2023-02-18 11:28:58 +08:00
|
|
|
function parseNestedCSS(node, options) {
|
2017-05-28 21:06:09 -07:00
|
|
|
if (node && typeof node === "object") {
|
|
|
|
|
delete node.parent;
|
2018-02-02 18:06:47 +03:00
|
|
|
|
2017-05-28 21:06:09 -07:00
|
|
|
for (const key in node) {
|
2023-02-18 11:28:58 +08:00
|
|
|
parseNestedCSS(node[key], options);
|
2017-05-28 21:06:09 -07:00
|
|
|
}
|
2018-02-02 18:06:47 +03:00
|
|
|
|
2018-04-13 22:12:59 +03:00
|
|
|
if (!node.type) {
|
|
|
|
|
return node;
|
|
|
|
|
}
|
2017-05-28 21:06:09 -07:00
|
|
|
|
2022-10-22 23:33:08 +08:00
|
|
|
/* c8 ignore next */
|
2022-09-29 10:40:13 +08:00
|
|
|
node.raws ??= {};
|
2018-04-13 22:12:59 +03:00
|
|
|
|
2020-11-09 16:47:12 +08:00
|
|
|
// Custom properties looks like declarations
|
|
|
|
|
if (
|
|
|
|
|
node.type === "css-decl" &&
|
|
|
|
|
typeof node.prop === "string" &&
|
|
|
|
|
node.prop.startsWith("--") &&
|
|
|
|
|
typeof node.value === "string" &&
|
|
|
|
|
node.value.startsWith("{")
|
|
|
|
|
) {
|
|
|
|
|
let rules;
|
2022-05-16 10:28:29 +08:00
|
|
|
if (node.value.trimEnd().endsWith("}")) {
|
2020-11-09 16:47:12 +08:00
|
|
|
const textBefore = options.originalText.slice(
|
|
|
|
|
0,
|
|
|
|
|
node.source.start.offset
|
|
|
|
|
);
|
|
|
|
|
const nodeText =
|
|
|
|
|
"a".repeat(node.prop.length) +
|
|
|
|
|
options.originalText.slice(
|
|
|
|
|
node.source.start.offset + node.prop.length,
|
|
|
|
|
node.source.end.offset + 1
|
|
|
|
|
);
|
2022-11-21 23:41:27 +08:00
|
|
|
const fakeContent = textBefore.replaceAll(/[^\n]/g, " ") + nodeText;
|
2020-11-09 16:47:12 +08:00
|
|
|
let parse;
|
|
|
|
|
if (options.parser === "scss") {
|
|
|
|
|
parse = parseScss;
|
|
|
|
|
} else if (options.parser === "less") {
|
|
|
|
|
parse = parseLess;
|
|
|
|
|
} else {
|
|
|
|
|
parse = parseCss;
|
|
|
|
|
}
|
|
|
|
|
let ast;
|
|
|
|
|
try {
|
2023-02-18 11:28:58 +08:00
|
|
|
ast = parse(fakeContent, { ...options });
|
2021-03-08 18:35:48 +08:00
|
|
|
} catch {
|
2020-11-09 16:47:12 +08:00
|
|
|
// noop
|
|
|
|
|
}
|
2022-11-03 14:23:07 +08:00
|
|
|
if (ast?.nodes?.length === 1 && ast.nodes[0].type === "css-rule") {
|
2020-11-09 16:47:12 +08:00
|
|
|
rules = ast.nodes[0].nodes;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (rules) {
|
|
|
|
|
node.value = {
|
|
|
|
|
type: "css-rule",
|
|
|
|
|
nodes: rules,
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
node.value = {
|
|
|
|
|
type: "value-unknown",
|
|
|
|
|
value: node.raws.value.raw,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-13 22:12:59 +03:00
|
|
|
let selector = "";
|
|
|
|
|
|
|
|
|
|
if (typeof node.selector === "string") {
|
2018-05-30 23:22:18 +03:00
|
|
|
selector = node.raws.selector
|
2022-07-09 15:03:46 +08:00
|
|
|
? node.raws.selector.scss ?? node.raws.selector.raw
|
2018-05-30 23:22:18 +03:00
|
|
|
: node.selector;
|
2018-04-13 22:12:59 +03:00
|
|
|
|
|
|
|
|
if (node.raws.between && node.raws.between.trim().length > 0) {
|
2018-02-07 13:20:13 +03:00
|
|
|
selector += node.raws.between;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-13 22:12:59 +03:00
|
|
|
node.raws.selector = selector;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let value = "";
|
|
|
|
|
|
|
|
|
|
if (typeof node.value === "string") {
|
2018-05-30 23:22:18 +03:00
|
|
|
value = node.raws.value
|
2022-07-09 15:03:46 +08:00
|
|
|
? node.raws.value.scss ?? node.raws.value.raw
|
2018-05-30 23:22:18 +03:00
|
|
|
: node.value;
|
2018-04-13 22:12:59 +03:00
|
|
|
|
|
|
|
|
value = value.trim();
|
|
|
|
|
|
2020-03-20 19:09:07 +03:00
|
|
|
node.raws.value = value;
|
2018-04-13 22:12:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let params = "";
|
|
|
|
|
|
|
|
|
|
if (typeof node.params === "string") {
|
2018-05-30 23:22:18 +03:00
|
|
|
params = node.raws.params
|
2022-07-09 15:03:46 +08:00
|
|
|
? node.raws.params.scss ?? node.raws.params.raw
|
2018-05-30 23:22:18 +03:00
|
|
|
: node.params;
|
2018-04-13 22:12:59 +03:00
|
|
|
|
|
|
|
|
if (node.raws.afterName && node.raws.afterName.trim().length > 0) {
|
|
|
|
|
params = node.raws.afterName + params;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (node.raws.between && node.raws.between.trim().length > 0) {
|
|
|
|
|
params = params + node.raws.between;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
params = params.trim();
|
|
|
|
|
|
|
|
|
|
node.raws.params = params;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ignore LESS mixin declaration
|
|
|
|
|
if (selector.trim().length > 0) {
|
2020-08-21 09:43:24 +08:00
|
|
|
// TODO: confirm this code is dead
|
2022-10-22 23:33:08 +08:00
|
|
|
/* c8 ignore next 3 */
|
2018-01-30 14:16:07 +03:00
|
|
|
if (selector.startsWith("@") && selector.endsWith(":")) {
|
|
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-21 09:43:24 +08:00
|
|
|
// TODO: confirm this code is dead
|
2022-10-22 23:33:08 +08:00
|
|
|
/* c8 ignore next 6 */
|
2018-04-13 22:12:59 +03:00
|
|
|
// Ignore LESS mixins
|
|
|
|
|
if (node.mixin) {
|
2023-02-18 11:28:58 +08:00
|
|
|
node.selector = parseValue(selector, options);
|
2018-04-13 22:12:59 +03:00
|
|
|
|
|
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-15 00:00:45 +03:00
|
|
|
// Check on SCSS nested property
|
2020-11-09 16:47:12 +08:00
|
|
|
if (isSCSSNestedPropertyNode(node, options)) {
|
2018-05-15 00:00:45 +03:00
|
|
|
node.isSCSSNesterProperty = true;
|
2017-05-28 21:06:09 -07:00
|
|
|
}
|
2018-02-02 18:06:47 +03:00
|
|
|
|
2023-02-18 11:28:58 +08:00
|
|
|
node.selector = parseSelector(selector);
|
2018-05-15 00:00:45 +03:00
|
|
|
|
2018-02-02 18:06:47 +03:00
|
|
|
return node;
|
2017-05-28 21:06:09 -07:00
|
|
|
}
|
2018-02-02 18:06:47 +03:00
|
|
|
|
2018-06-25 12:57:41 +03:00
|
|
|
if (value.length > 0) {
|
2018-05-15 00:00:45 +03:00
|
|
|
const defaultSCSSDirectiveIndex = value.match(DEFAULT_SCSS_DIRECTIVE);
|
2018-02-06 18:09:42 +03:00
|
|
|
|
2018-05-15 00:00:45 +03:00
|
|
|
if (defaultSCSSDirectiveIndex) {
|
2020-01-23 20:15:28 +08:00
|
|
|
value = value.slice(0, defaultSCSSDirectiveIndex.index);
|
2018-05-15 00:00:45 +03:00
|
|
|
node.scssDefault = true;
|
2018-02-06 18:09:42 +03:00
|
|
|
|
2018-05-15 00:00:45 +03:00
|
|
|
if (defaultSCSSDirectiveIndex[0].trim() !== "!default") {
|
|
|
|
|
node.raws.scssDefault = defaultSCSSDirectiveIndex[0];
|
2018-01-12 14:12:02 +03:00
|
|
|
}
|
2017-05-28 21:06:09 -07:00
|
|
|
}
|
2018-02-02 18:06:47 +03:00
|
|
|
|
2018-05-15 00:00:45 +03:00
|
|
|
const globalSCSSDirectiveIndex = value.match(GLOBAL_SCSS_DIRECTIVE);
|
|
|
|
|
|
|
|
|
|
if (globalSCSSDirectiveIndex) {
|
2020-01-23 20:15:28 +08:00
|
|
|
value = value.slice(0, globalSCSSDirectiveIndex.index);
|
2018-05-15 00:00:45 +03:00
|
|
|
node.scssGlobal = true;
|
|
|
|
|
|
|
|
|
|
if (globalSCSSDirectiveIndex[0].trim() !== "!global") {
|
|
|
|
|
node.raws.scssGlobal = globalSCSSDirectiveIndex[0];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (value.startsWith("progid:")) {
|
|
|
|
|
return {
|
|
|
|
|
type: "value-unknown",
|
2020-01-14 19:25:24 +08:00
|
|
|
value,
|
2018-05-15 00:00:45 +03:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-18 11:28:58 +08:00
|
|
|
node.value = parseValue(value, options);
|
2017-05-28 21:06:09 -07:00
|
|
|
}
|
2018-02-02 18:06:47 +03:00
|
|
|
|
2020-04-08 23:02:13 +08:00
|
|
|
if (
|
2020-11-09 16:47:12 +08:00
|
|
|
options.parser === "less" &&
|
2020-04-08 23:02:13 +08:00
|
|
|
node.type === "css-decl" &&
|
|
|
|
|
value.startsWith("extend(")
|
|
|
|
|
) {
|
2020-04-30 16:58:21 +08:00
|
|
|
// extend is missing
|
2022-09-29 10:40:13 +08:00
|
|
|
node.extend ||= node.raws.between === ":";
|
2020-04-30 16:58:21 +08:00
|
|
|
|
|
|
|
|
// `:extend()` is parsed as value
|
|
|
|
|
if (node.extend && !node.selector) {
|
|
|
|
|
delete node.value;
|
2023-02-18 11:28:58 +08:00
|
|
|
node.selector = parseSelector(value.slice("extend(".length, -1));
|
2020-04-30 16:58:21 +08:00
|
|
|
}
|
2020-04-08 23:02:13 +08:00
|
|
|
}
|
|
|
|
|
|
2020-03-10 07:14:34 +08:00
|
|
|
if (node.type === "css-atrule") {
|
2020-11-09 16:47:12 +08:00
|
|
|
if (options.parser === "less") {
|
2020-03-10 07:14:34 +08:00
|
|
|
// mixin
|
|
|
|
|
if (node.mixin) {
|
|
|
|
|
const source =
|
|
|
|
|
node.raws.identifier +
|
|
|
|
|
node.name +
|
|
|
|
|
node.raws.afterName +
|
|
|
|
|
node.raws.params;
|
2023-02-18 11:28:58 +08:00
|
|
|
node.selector = parseSelector(source);
|
2020-03-10 07:14:34 +08:00
|
|
|
delete node.params;
|
|
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// function
|
|
|
|
|
if (node.function) {
|
|
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// only css support custom-selector
|
|
|
|
|
if (options.parser === "css" && node.name === "custom-selector") {
|
2021-12-07 14:15:03 +08:00
|
|
|
const customSelector = node.params.match(/:--\S+\s+/)[0].trim();
|
2020-03-10 07:14:34 +08:00
|
|
|
node.customSelector = customSelector;
|
2023-02-18 11:28:58 +08:00
|
|
|
node.selector = parseSelector(
|
2020-03-10 07:14:34 +08:00
|
|
|
node.params.slice(customSelector.length).trim()
|
|
|
|
|
);
|
|
|
|
|
delete node.params;
|
|
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-09 16:47:12 +08:00
|
|
|
if (options.parser === "less") {
|
2020-10-12 04:16:36 +03:00
|
|
|
// postcss-less doesn't recognize variables in some cases.
|
|
|
|
|
// `@color: blue;` is recognized fine, but the cases below aren't:
|
|
|
|
|
|
|
|
|
|
// `@color:blue;`
|
2020-03-10 07:14:34 +08:00
|
|
|
if (node.name.includes(":") && !node.params) {
|
|
|
|
|
node.variable = true;
|
|
|
|
|
const parts = node.name.split(":");
|
|
|
|
|
node.name = parts[0];
|
2023-02-18 11:28:58 +08:00
|
|
|
node.value = parseValue(parts.slice(1).join(":"), options);
|
2020-03-10 07:14:34 +08:00
|
|
|
}
|
|
|
|
|
|
2020-10-12 04:16:36 +03:00
|
|
|
// `@color :blue;`
|
2020-03-10 07:14:34 +08:00
|
|
|
if (
|
2021-04-25 18:13:40 -04:00
|
|
|
!["page", "nest", "keyframes"].includes(node.name) &&
|
2022-11-03 14:23:07 +08:00
|
|
|
node.params?.[0] === ":"
|
2020-03-10 07:14:34 +08:00
|
|
|
) {
|
|
|
|
|
node.variable = true;
|
2023-01-03 06:32:00 +01:00
|
|
|
const text = node.params.slice(1);
|
|
|
|
|
if (text) {
|
2023-02-18 11:28:58 +08:00
|
|
|
node.value = parseValue(text, options);
|
2023-01-03 06:32:00 +01:00
|
|
|
}
|
2020-10-12 04:16:36 +03:00
|
|
|
node.raws.afterName += ":";
|
2020-03-10 07:14:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Less variable
|
|
|
|
|
if (node.variable) {
|
|
|
|
|
delete node.params;
|
2023-01-03 06:32:00 +01:00
|
|
|
|
|
|
|
|
if (!node.value) {
|
|
|
|
|
delete node.value;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-10 07:14:34 +08:00
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-13 22:12:59 +03:00
|
|
|
if (node.type === "css-atrule" && params.length > 0) {
|
2020-01-14 23:30:18 +08:00
|
|
|
const { name } = node;
|
2018-02-22 12:49:03 +03:00
|
|
|
const lowercasedName = node.name.toLowerCase();
|
2018-02-21 21:36:09 +03:00
|
|
|
|
2018-02-22 12:49:03 +03:00
|
|
|
if (name === "warn" || name === "error") {
|
2018-01-20 03:00:06 +03:00
|
|
|
node.params = {
|
|
|
|
|
type: "media-unknown",
|
2018-02-21 21:36:09 +03:00
|
|
|
value: params,
|
2018-01-20 03:00:06 +03:00
|
|
|
};
|
2018-02-02 18:06:47 +03:00
|
|
|
|
|
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-22 12:49:03 +03:00
|
|
|
if (name === "extend" || name === "nest") {
|
2023-02-18 11:28:58 +08:00
|
|
|
node.selector = parseSelector(params);
|
2018-02-02 18:06:47 +03:00
|
|
|
delete node.params;
|
|
|
|
|
|
|
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-22 12:49:03 +03:00
|
|
|
if (name === "at-root") {
|
2021-09-08 11:18:07 +08:00
|
|
|
if (/^\(\s*(?:without|with)\s*:.+\)$/s.test(params)) {
|
2023-02-18 11:28:58 +08:00
|
|
|
node.params = parseValue(params, options);
|
2018-02-02 18:06:47 +03:00
|
|
|
} else {
|
2023-02-18 11:28:58 +08:00
|
|
|
node.selector = parseSelector(params);
|
2018-02-02 18:06:47 +03:00
|
|
|
delete node.params;
|
2018-01-24 12:31:30 +03:00
|
|
|
}
|
|
|
|
|
|
2018-02-02 18:06:47 +03:00
|
|
|
return node;
|
2018-01-20 03:00:06 +03:00
|
|
|
}
|
2018-02-02 18:06:47 +03:00
|
|
|
|
2021-09-09 06:18:12 +02:00
|
|
|
if (isModuleRuleName(lowercasedName)) {
|
2020-03-10 07:14:34 +08:00
|
|
|
node.import = true;
|
|
|
|
|
delete node.filename;
|
2023-02-18 11:28:58 +08:00
|
|
|
node.params = parseValue(params, options);
|
2018-05-15 01:24:34 +05:30
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-02 18:06:47 +03:00
|
|
|
if (
|
2018-02-22 12:49:03 +03:00
|
|
|
[
|
2018-04-25 21:27:12 +03:00
|
|
|
"namespace",
|
|
|
|
|
"supports",
|
2018-02-22 12:49:03 +03:00
|
|
|
"if",
|
|
|
|
|
"else",
|
|
|
|
|
"for",
|
|
|
|
|
"each",
|
|
|
|
|
"while",
|
|
|
|
|
"debug",
|
|
|
|
|
"mixin",
|
|
|
|
|
"include",
|
|
|
|
|
"function",
|
|
|
|
|
"return",
|
|
|
|
|
"define-mixin",
|
2018-04-25 21:27:12 +03:00
|
|
|
"add-mixin",
|
2019-11-17 18:06:14 +08:00
|
|
|
].includes(name)
|
2018-02-02 18:06:47 +03:00
|
|
|
) {
|
|
|
|
|
// Remove unnecessary spaces in SCSS variable arguments
|
2021-12-06 21:31:51 +08:00
|
|
|
// Move spaces after the `...`, so we can keep the range correct
|
|
|
|
|
params = params.replace(/(\$\S+?)(\s+)?\.{3}/, "$1...$2");
|
2018-02-02 18:06:47 +03:00
|
|
|
// Remove unnecessary spaces before SCSS control, mixin and function directives
|
2021-12-06 21:31:51 +08:00
|
|
|
// Move spaces after the `(`, so we can keep the range correct
|
|
|
|
|
params = params.replace(/^(?!if)(\S+)(\s+)\(/, "$1($2");
|
2018-02-02 18:06:47 +03:00
|
|
|
|
2023-02-18 11:28:58 +08:00
|
|
|
node.value = parseValue(params, options);
|
2018-02-02 18:06:47 +03:00
|
|
|
delete node.params;
|
|
|
|
|
|
|
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-17 18:06:14 +08:00
|
|
|
if (["media", "custom-media"].includes(lowercasedName)) {
|
2018-02-22 12:49:03 +03:00
|
|
|
if (params.includes("#{")) {
|
|
|
|
|
// Workaround for media at rule with scss interpolation
|
|
|
|
|
return {
|
|
|
|
|
type: "media-unknown",
|
|
|
|
|
value: params,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-18 11:28:58 +08:00
|
|
|
node.params = parseMediaQuery(params);
|
2018-02-22 12:49:03 +03:00
|
|
|
|
|
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
node.params = params;
|
2018-02-02 18:06:47 +03:00
|
|
|
|
|
|
|
|
return node;
|
2017-05-28 21:06:09 -07:00
|
|
|
}
|
|
|
|
|
}
|
2018-02-02 18:06:47 +03:00
|
|
|
|
2017-05-28 21:06:09 -07:00
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-18 11:28:58 +08:00
|
|
|
function parseWithParser(parse, text, options) {
|
2018-05-21 10:02:09 -04:00
|
|
|
const parsed = parseFrontMatter(text);
|
2018-07-18 02:31:44 +05:30
|
|
|
const { frontMatter } = parsed;
|
2018-05-14 22:17:15 -03:00
|
|
|
text = parsed.content;
|
|
|
|
|
|
2017-05-28 21:06:09 -07:00
|
|
|
let result;
|
2018-04-28 13:53:30 +02:00
|
|
|
|
2017-05-28 21:06:09 -07:00
|
|
|
try {
|
2021-03-09 13:02:12 +08:00
|
|
|
result = parse(text, {
|
|
|
|
|
// Prevent file access https://github.com/postcss/postcss/blob/4f4e2932fc97e2c117e1a4b15f0272ed551ed59d/lib/previous-map.js#L18
|
|
|
|
|
map: false,
|
|
|
|
|
});
|
2020-10-16 10:39:05 +08:00
|
|
|
} catch (error) {
|
|
|
|
|
const { name, reason, line, column } = error;
|
2022-10-22 23:33:08 +08:00
|
|
|
/* c8 ignore 3 */
|
2020-10-16 10:39:05 +08:00
|
|
|
if (typeof line !== "number") {
|
|
|
|
|
throw error;
|
2017-05-28 21:06:09 -07:00
|
|
|
}
|
2022-08-30 10:27:11 +08:00
|
|
|
|
|
|
|
|
throw createError(`${name}: ${reason}`, {
|
|
|
|
|
loc: { start: { line, column } },
|
|
|
|
|
cause: error,
|
|
|
|
|
});
|
2017-05-28 21:06:09 -07:00
|
|
|
}
|
2018-01-24 19:44:09 +03:00
|
|
|
|
2020-11-09 16:47:12 +08:00
|
|
|
options.originalText = text;
|
2023-02-18 11:28:58 +08:00
|
|
|
result = parseNestedCSS(addTypePrefix(result, "css-"), options);
|
2018-05-14 22:17:15 -03:00
|
|
|
|
2020-03-20 19:09:07 +03:00
|
|
|
calculateLoc(result, text);
|
|
|
|
|
|
2018-05-21 10:02:09 -04:00
|
|
|
if (frontMatter) {
|
2020-11-05 19:26:05 +08:00
|
|
|
frontMatter.source = {
|
|
|
|
|
startOffset: 0,
|
|
|
|
|
endOffset: frontMatter.raw.length,
|
|
|
|
|
};
|
2022-08-30 11:00:12 +08:00
|
|
|
result.frontMatter = frontMatter;
|
2018-01-24 19:44:09 +03:00
|
|
|
}
|
|
|
|
|
|
2018-05-14 22:17:15 -03:00
|
|
|
return result;
|
2017-05-28 21:06:09 -07:00
|
|
|
}
|
|
|
|
|
|
2023-02-18 11:28:58 +08:00
|
|
|
function parseCss(text, options = {}) {
|
|
|
|
|
return parseWithParser(postcssParse.default, text, options);
|
2020-04-03 12:41:48 +08:00
|
|
|
}
|
|
|
|
|
|
2023-02-18 11:28:58 +08:00
|
|
|
function parseLess(text, options = {}) {
|
2020-04-03 12:41:48 +08:00
|
|
|
return parseWithParser(
|
2020-03-20 19:09:07 +03:00
|
|
|
// Workaround for https://github.com/shellscape/postcss-less/issues/145
|
|
|
|
|
// See comments for `replaceQuotesInInlineComments` in `loc.js`.
|
2023-02-18 11:28:58 +08:00
|
|
|
(text) => postcssLess.parse(replaceQuotesInInlineComments(text)),
|
2020-04-03 12:41:48 +08:00
|
|
|
text,
|
|
|
|
|
options
|
|
|
|
|
);
|
2017-05-28 21:49:41 -07:00
|
|
|
}
|
|
|
|
|
|
2023-02-18 11:28:58 +08:00
|
|
|
function parseScss(text, options = {}) {
|
|
|
|
|
return parseWithParser(postcssScssParse, text, options);
|
2017-05-28 21:06:09 -07:00
|
|
|
}
|
|
|
|
|
|
2020-04-03 12:41:48 +08:00
|
|
|
const postCssParser = {
|
2018-05-24 15:30:45 -03:00
|
|
|
astFormat: "postcss",
|
2018-06-14 09:51:18 +08:00
|
|
|
hasPragma,
|
2020-11-05 19:26:05 +08:00
|
|
|
locStart,
|
|
|
|
|
locEnd,
|
2018-05-24 15:30:45 -03:00
|
|
|
};
|
|
|
|
|
|
2023-03-24 08:44:25 +08:00
|
|
|
export const css = { ...postCssParser, parse: parseCss };
|
|
|
|
|
export const less = { ...postCssParser, parse: parseLess };
|
|
|
|
|
export const scss = { ...postCssParser, parse: parseScss };
|