Skip to content

Commit f7554c6

Browse files
sosukesuzukifisker
andauthored
Revert breaking changes related to adding pure css parser for minor releasing (#9500)
* revert without tests * Downgrade postcss-scss and postcss-less * Fix tests * Revert "Make `isSCSS*` function stricter (#9093)" This reverts commit 01f986b. * Revert "Remove `css` parser from `isLessParser` check (#9092)" This reverts commit c0bca18. * Revert "Add pure css parser (#7933)" This reverts commit 589ebf6. * Disable `parser-postcss.js` minify * Try to fix build script * Update fs.mjs * Update fs.mjs * Style Co-authored-by: fisker <[email protected]>
1 parent 3f4c8c1 commit f7554c6

File tree

53 files changed

+1230
-2210
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1230
-2210
lines changed

changelog_unreleased/css/pr-7933.md

Lines changed: 0 additions & 41 deletions
This file was deleted.

docs/options.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,9 @@ Valid options:
212212
- `"flow"` (via [flow-parser](https://github.com/facebook/flow/tree/master/src/parser))
213213
- `"typescript"` (via [@typescript-eslint/typescript-estree](https://github.com/typescript-eslint/typescript-eslint)) _First available in v1.4.0_
214214
- `"espree"` (via [espree](https://github.com/eslint/espree)) _First available in v2.2.0_
215-
- `"css"` (via [postcss](https://github.com/postcss/postcss)) _First available in v1.7.1_
216-
- `"scss"` (via [postcss-scss](https://github.com/postcss/postcss-scss)) _First available in v1.7.1_
217-
- `"less"` (via [postcss-less](https://github.com/shellscape/postcss-less) _First available in v1.7.1_
215+
- `"css"` (via [postcss-scss](https://github.com/postcss/postcss-scss) and [postcss-less](https://github.com/shellscape/postcss-less), autodetects which to use) _First available in v1.7.1_
216+
- `"scss"` (same parsers as `"css"`, prefers postcss-scss) _First available in v1.7.1_
217+
- `"less"` (same parsers as `"css"`, prefers postcss-less) _First available in v1.7.1_
218218
- `"json"` (via [@babel/parser parseExpression](https://babeljs.io/docs/en/next/babel-parser.html#babelparserparseexpressioncode-options)) _First available in v1.5.0_
219219
- `"json5"` (same parser as `"json"`, but outputs as [json5](https://json5.org/)) _First available in v1.13.0_
220220
- `"json-stringify"` (same parser as `"json"`, but outputs like `JSON.stringify`) _First available in v1.13.0_

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,9 @@
6565
"outdent": "0.7.1",
6666
"parse-srcset": "ikatyang/parse-srcset#54eb9c1cb21db5c62b4d0e275d7249516df6f0ee",
6767
"please-upgrade-node": "3.2.0",
68-
"postcss": "8.1.4",
69-
"postcss-less": "4.0.0",
68+
"postcss-less": "3.1.4",
7069
"postcss-media-query-parser": "0.2.3",
71-
"postcss-scss": "3.0.2",
70+
"postcss-scss": "2.1.1",
7271
"postcss-selector-parser": "2.2.3",
7372
"postcss-values-parser": "2.0.1",
7473
"regexp-util": "1.2.2",

scripts/build/shims/fs.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export const existsSync = () => false;
22
export const readFileSync = () => "";
3+
export default { existsSync, readFileSync };

scripts/build/shims/path.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from "path-browserify";
2+
export { default } from "path-browserify";

src/language-css/clean.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,6 @@ function clean(ast, newObj, parent) {
9595
if (ast.type === "value-number") {
9696
newObj.unit = newObj.unit.toLowerCase();
9797
}
98-
if (ast.type === "value-unknown") {
99-
newObj.value = newObj.value.replace(/;$/g, "");
100-
}
10198

10299
if (
103100
(ast.type === "media-feature" ||

src/language-css/parser-postcss.js

Lines changed: 27 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const { hasPragma } = require("./pragma");
66
const {
77
hasSCSSInterpolation,
88
hasStringOrFunction,
9+
isLessParser,
10+
isSCSS,
911
isSCSSNestedPropertyNode,
1012
isSCSSVariable,
1113
stringifyNode,
@@ -39,7 +41,7 @@ function parseValueNode(valueNode, options) {
3941
const node = nodes[i];
4042

4143
if (
42-
options.parser === "scss" &&
44+
isSCSS(options.parser, node.value) &&
4345
node.type === "number" &&
4446
node.unit === ".." &&
4547
node.value[node.value.length - 1] === "."
@@ -79,8 +81,7 @@ function parseValueNode(valueNode, options) {
7981
// Stringify if the value parser can't handle the content.
8082
if (
8183
hasSCSSInterpolation(groupList) ||
82-
(!hasStringOrFunction(groupList) &&
83-
!isSCSSVariable(groupList[0], options))
84+
(!hasStringOrFunction(groupList) && !isSCSSVariable(groupList[0]))
8485
) {
8586
const stringifiedContent = stringifyNode({
8687
groups: node.group.groups,
@@ -300,64 +301,6 @@ function parseNestedCSS(node, options) {
300301
node.raws = {};
301302
}
302303

303-
// Custom properties looks like declarations
304-
if (
305-
node.type === "css-decl" &&
306-
typeof node.prop === "string" &&
307-
node.prop.startsWith("--") &&
308-
typeof node.value === "string" &&
309-
node.value.startsWith("{")
310-
) {
311-
let rules;
312-
if (node.value.endsWith("}")) {
313-
const textBefore = options.originalText.slice(
314-
0,
315-
node.source.start.offset
316-
);
317-
const nodeText =
318-
"a".repeat(node.prop.length) +
319-
options.originalText.slice(
320-
node.source.start.offset + node.prop.length,
321-
node.source.end.offset + 1
322-
);
323-
const fakeContent = textBefore.replace(/[^\n]/g, " ") + nodeText;
324-
let parse;
325-
if (options.parser === "scss") {
326-
parse = parseScss;
327-
} else if (options.parser === "less") {
328-
parse = parseLess;
329-
} else {
330-
parse = parseCss;
331-
}
332-
let ast;
333-
try {
334-
ast = parse(fakeContent, [], { ...options });
335-
} catch (_) {
336-
// noop
337-
}
338-
if (
339-
ast &&
340-
ast.nodes &&
341-
ast.nodes.length === 1 &&
342-
ast.nodes[0].type === "css-rule"
343-
) {
344-
rules = ast.nodes[0].nodes;
345-
}
346-
}
347-
if (rules) {
348-
node.value = {
349-
type: "css-rule",
350-
nodes: rules,
351-
};
352-
} else {
353-
node.value = {
354-
type: "value-unknown",
355-
value: node.raws.value.raw,
356-
};
357-
}
358-
return node;
359-
}
360-
361304
let selector = "";
362305

363306
if (typeof node.selector === "string") {
@@ -428,7 +371,7 @@ function parseNestedCSS(node, options) {
428371
}
429372

430373
// Check on SCSS nested property
431-
if (isSCSSNestedPropertyNode(node, options)) {
374+
if (isSCSSNestedPropertyNode(node)) {
432375
node.isSCSSNesterProperty = true;
433376
}
434377

@@ -471,7 +414,7 @@ function parseNestedCSS(node, options) {
471414
}
472415

473416
if (
474-
options.parser === "less" &&
417+
isLessParser(options) &&
475418
node.type === "css-decl" &&
476419
value.startsWith("extend(")
477420
) {
@@ -488,7 +431,7 @@ function parseNestedCSS(node, options) {
488431
}
489432

490433
if (node.type === "css-atrule") {
491-
if (options.parser === "less") {
434+
if (isLessParser(options)) {
492435
// mixin
493436
if (node.mixin) {
494437
const source =
@@ -518,7 +461,7 @@ function parseNestedCSS(node, options) {
518461
return node;
519462
}
520463

521-
if (options.parser === "less") {
464+
if (isLessParser(options)) {
522465
// postcss-less doesn't recognize variables in some cases.
523466
// `@color: blue;` is recognized fine, but the cases below aren't:
524467

@@ -657,7 +600,6 @@ function parseWithParser(parse, text, options) {
657600
throw createError(`${name}: ${reason}`, { start: { line, column } });
658601
}
659602

660-
options.originalText = text;
661603
result = parseNestedCSS(addTypePrefix(result, "css-"), options);
662604

663605
calculateLoc(result, text);
@@ -669,9 +611,26 @@ function parseWithParser(parse, text, options) {
669611
return result;
670612
}
671613

614+
// TODO: make this only work on css
672615
function parseCss(text, parsers, options) {
673-
const { parse } = require("postcss");
674-
return parseWithParser(parse, text, options);
616+
const isSCSSParser = isSCSS(options.parser, text);
617+
const parseFunctions = isSCSSParser
618+
? [parseScss, parseLess]
619+
: [parseLess, parseScss];
620+
621+
let error;
622+
for (const parse of parseFunctions) {
623+
try {
624+
return parse(text, parsers, options);
625+
} catch (parseError) {
626+
error = error || parseError;
627+
}
628+
}
629+
630+
/* istanbul ignore next */
631+
if (error) {
632+
throw error;
633+
}
675634
}
676635

677636
function parseLess(text, parsers, options) {

src/language-css/printer-postcss.js

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ const {
3838
insideURLFunctionInImportAtRuleNode,
3939
isKeyframeAtRuleKeywords,
4040
isWideKeywords,
41+
isSCSS,
4142
isLastNode,
43+
isLessParser,
4244
isSCSSControlDirectiveNode,
4345
isDetachedRulesetDeclarationNode,
4446
isRelationalOperatorNode,
@@ -121,9 +123,7 @@ function genericPrint(path, options, print) {
121123
node.selector.type === "selector-unknown" &&
122124
lastLineHasInlineComment(node.selector.value)
123125
? line
124-
: node.selector
125-
? " "
126-
: "",
126+
: " ",
127127
"{",
128128
node.nodes.length > 0
129129
? indent(
@@ -143,8 +143,6 @@ function genericPrint(path, options, print) {
143143
const { between: rawBetween } = node.raws;
144144
const trimmedBetween = rawBetween.trim();
145145
const isColon = trimmedBetween === ":";
146-
const isValueAllSpace =
147-
typeof node.value === "string" && /^ *$/.test(node.value);
148146

149147
let value = hasComposesNode(node)
150148
? removeLines(path.call(print, "value"))
@@ -159,8 +157,8 @@ function genericPrint(path, options, print) {
159157
insideICSSRuleNode(path) ? node.prop : maybeToLowerCase(node.prop),
160158
trimmedBetween.startsWith("//") ? " " : "",
161159
trimmedBetween,
162-
node.extend || isValueAllSpace ? "" : " ",
163-
options.parser === "less" && node.extend && node.selector
160+
node.extend ? "" : " ",
161+
isLessParser(options) && node.extend && node.selector
164162
? concat(["extend(", path.call(print, "selector"), ")"])
165163
: "",
166164
value,
@@ -204,7 +202,7 @@ function genericPrint(path, options, print) {
204202
!parentNode.raws.semicolon &&
205203
options.originalText[options.locEnd(node) - 1] !== ";";
206204

207-
if (options.parser === "less") {
205+
if (isLessParser(options)) {
208206
if (node.mixin) {
209207
return concat([
210208
path.call(print, "selector"),
@@ -245,11 +243,6 @@ function genericPrint(path, options, print) {
245243
]);
246244
}
247245
}
248-
const isImportUnknownValueEndsWithSemiColon =
249-
node.name === "import" &&
250-
node.params &&
251-
node.params.type === "value-unknown" &&
252-
node.params.value.endsWith(";");
253246

254247
return concat([
255248
"@",
@@ -285,7 +278,7 @@ function genericPrint(path, options, print) {
285278
concat([
286279
" ",
287280
path.call(print, "value"),
288-
isSCSSControlDirectiveNode(node, options)
281+
isSCSSControlDirectiveNode(node)
289282
? hasParensAroundNode(node)
290283
? " "
291284
: line
@@ -297,7 +290,7 @@ function genericPrint(path, options, print) {
297290
: "",
298291
node.nodes
299292
? concat([
300-
isSCSSControlDirectiveNode(node, options)
293+
isSCSSControlDirectiveNode(node)
301294
? ""
302295
: (node.selector &&
303296
!node.selector.nodes &&
@@ -318,8 +311,7 @@ function genericPrint(path, options, print) {
318311
softline,
319312
"}",
320313
])
321-
: isTemplatePlaceholderNodeWithoutSemiColon ||
322-
isImportUnknownValueEndsWithSemiColon
314+
: isTemplatePlaceholderNodeWithoutSemiColon
323315
? ""
324316
: ";",
325317
]);
@@ -546,8 +538,7 @@ function genericPrint(path, options, print) {
546538
declAncestorProp.startsWith("grid-template"));
547539
const atRuleAncestorNode = getAncestorNode(path, "css-atrule");
548540
const isControlDirective =
549-
atRuleAncestorNode &&
550-
isSCSSControlDirectiveNode(atRuleAncestorNode, options);
541+
atRuleAncestorNode && isSCSSControlDirectiveNode(atRuleAncestorNode);
551542

552543
const printed = path.map(print, "groups");
553544
const parts = [];
@@ -866,7 +857,7 @@ function genericPrint(path, options, print) {
866857
return group(indent(fill(res)));
867858
}
868859

869-
const isSCSSMapItem = isSCSSMapItemNode(path, options);
860+
const isSCSSMapItem = isSCSSMapItemNode(path);
870861

871862
const lastItem = node.groups[node.groups.length - 1];
872863
const isLastItemComment = lastItem && lastItem.type === "value-comment";
@@ -905,7 +896,7 @@ function genericPrint(path, options, print) {
905896
),
906897
ifBreak(
907898
!isLastItemComment &&
908-
options.parser === "scss" &&
899+
isSCSS(options.parser, options.originalText) &&
909900
isSCSSMapItem &&
910901
shouldPrintComma(options)
911902
? ","

0 commit comments

Comments
 (0)