@jesscss/scss-parser 2.0.0-alpha.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +173 -0
- package/lib/ast/lower-user-function-calls.d.ts +6 -0
- package/lib/cst.cjs +14 -0
- package/lib/cst.d.ts +5 -0
- package/lib/cst.js +12 -0
- package/lib/grammar.cjs +99135 -0
- package/lib/grammar.d.ts +336 -0
- package/lib/grammar.js +99131 -0
- package/lib/index.cjs +111 -0
- package/lib/index.d.ts +10 -0
- package/lib/index.js +109 -0
- package/package.json +71 -0
package/lib/index.cjs
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_grammar = require("./grammar.cjs");
|
|
3
|
+
let parseman = require("parseman");
|
|
4
|
+
let _jesscss_core_ast = require("@jesscss/core/ast");
|
|
5
|
+
//#region src/ast/lower-user-function-calls.ts
|
|
6
|
+
/**
|
|
7
|
+
* SCSS call-site lowering: a user `@function f(...)` was lowered (in the grammar)
|
|
8
|
+
* to a `$var`-bound value lambda (`AnonymousMixin` with `params`). A CALL to that
|
|
9
|
+
* function — `f(2)` — parses as an ordinary `FunctionCall`, indistinguishable at
|
|
10
|
+
* the leaf from a builtin Sass call (`darken(...)`). This post-parse pass rewrites
|
|
11
|
+
* every `FunctionCall` whose name is a USER-defined `@function` into the shared
|
|
12
|
+
* `$f(args)` invoke form (a `Reference` with a `Call` step on the bound variable),
|
|
13
|
+
* so it reaches the general "call a value-lambda" evaluator path. Builtin Sass
|
|
14
|
+
* functions are left as `FunctionCall`s and continue to route to `fns`.
|
|
15
|
+
*
|
|
16
|
+
* User-function names are collected from the WHOLE document first (Sass hoists and
|
|
17
|
+
* effectively globalises function definitions), so a call may precede its `@function`.
|
|
18
|
+
* The only SCSS construct that binds an `AnonymousMixin` to a `$var` is `@function`,
|
|
19
|
+
* so "a VariableDeclaration whose value is an AnonymousMixin" is an exact marker.
|
|
20
|
+
*/
|
|
21
|
+
function isRecord(value) {
|
|
22
|
+
return typeof value === "object" && value !== null;
|
|
23
|
+
}
|
|
24
|
+
function isFunctionCall(value) {
|
|
25
|
+
return isRecord(value) && value.type === "FunctionCall" && typeof value.name === "string" && Array.isArray(value.args);
|
|
26
|
+
}
|
|
27
|
+
/** A non-array value slot is a single value node. */
|
|
28
|
+
function isValueNode(slot) {
|
|
29
|
+
return !Array.isArray(slot);
|
|
30
|
+
}
|
|
31
|
+
function isStylesheet$1(value) {
|
|
32
|
+
return isRecord(value) && value.type === "Stylesheet" && Array.isArray(value.children);
|
|
33
|
+
}
|
|
34
|
+
/** Collect every user `@function` name: a VariableDeclaration bound to an
|
|
35
|
+
* AnonymousMixin (the only SCSS shape that produces one). */
|
|
36
|
+
function collectUserFunctionNames(node, into) {
|
|
37
|
+
if (Array.isArray(node)) {
|
|
38
|
+
for (const child of node) collectUserFunctionNames(child, into);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
if (!isRecord(node)) return;
|
|
42
|
+
if (node.type === "VariableDeclaration" && typeof node.name === "string" && isRecord(node.value) && node.value.type === "AnonymousMixin") into.add(node.name);
|
|
43
|
+
for (const key of Object.keys(node)) collectUserFunctionNames(node[key], into);
|
|
44
|
+
}
|
|
45
|
+
/** Best-effort authored spelling of a call argument, for a Reference `raw`
|
|
46
|
+
* fallback (only ever emitted if the invoke fails to resolve). */
|
|
47
|
+
function argRaw(slot) {
|
|
48
|
+
if (!isValueNode(slot)) return slot.map(argRaw).join(" ");
|
|
49
|
+
if (slot.type === "VariableReference") return `$${slot.name}`;
|
|
50
|
+
if ("src" in slot && typeof slot.src === "string") return slot.src;
|
|
51
|
+
return "";
|
|
52
|
+
}
|
|
53
|
+
/** Deep-transform the tree, rewriting user-function `FunctionCall`s (post-order,
|
|
54
|
+
* so nested user calls inside the args lower first). Non-AST scalars pass through. */
|
|
55
|
+
function rewrite(node, userFns) {
|
|
56
|
+
if (Array.isArray(node)) return node.map((child) => rewrite(child, userFns));
|
|
57
|
+
if (!isRecord(node)) return node;
|
|
58
|
+
const out = {};
|
|
59
|
+
for (const key of Object.keys(node)) out[key] = rewrite(node[key], userFns);
|
|
60
|
+
if (isFunctionCall(out) && userFns.has(out.name)) {
|
|
61
|
+
const args = out.args.map((value) => ({ value }));
|
|
62
|
+
const raw = `${out.name}(${out.args.map(argRaw).join(", ")})`;
|
|
63
|
+
return (0, _jesscss_core_ast.reference)((0, _jesscss_core_ast.variableReference)(out.name, "live"), [{
|
|
64
|
+
type: "Call",
|
|
65
|
+
args
|
|
66
|
+
}], raw);
|
|
67
|
+
}
|
|
68
|
+
return out;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Rewrite user-`@function` call sites in a parsed SCSS document to `$f(args)`
|
|
72
|
+
* lambda invokes. Returns the document unchanged when it defines no user function.
|
|
73
|
+
*/
|
|
74
|
+
function lowerUserFunctionCalls(sheet) {
|
|
75
|
+
const userFns = /* @__PURE__ */ new Set();
|
|
76
|
+
collectUserFunctionNames(sheet.children, userFns);
|
|
77
|
+
if (userFns.size === 0) return sheet;
|
|
78
|
+
const lowered = rewrite(sheet, userFns);
|
|
79
|
+
if (!isStylesheet$1(lowered)) throw new TypeError("SCSS user-function lowering did not preserve the Stylesheet root.");
|
|
80
|
+
return lowered;
|
|
81
|
+
}
|
|
82
|
+
//#endregion
|
|
83
|
+
//#region src/index.ts
|
|
84
|
+
/** Structured failure from the public direct SCSS parser. */
|
|
85
|
+
var ScssParseError = class extends SyntaxError {
|
|
86
|
+
code = "parse/syntax-error";
|
|
87
|
+
offset;
|
|
88
|
+
expected;
|
|
89
|
+
constructor(offset, expected) {
|
|
90
|
+
const detail = expected.length > 0 ? ` Expected: ${expected.join(", ")}.` : "";
|
|
91
|
+
super(`SCSS parser error.${detail}`);
|
|
92
|
+
this.name = "ScssParseError";
|
|
93
|
+
this.offset = offset;
|
|
94
|
+
this.expected = expected;
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
function isStylesheet(value) {
|
|
98
|
+
return typeof value === "object" && value !== null && "type" in value && value.type === "Stylesheet" && "children" in value && Array.isArray(value.children);
|
|
99
|
+
}
|
|
100
|
+
/** Parse SCSS directly into the canonical AST v2 document. */
|
|
101
|
+
function parse(input) {
|
|
102
|
+
const entry = require_grammar.scssAstGrammar.Stylesheet;
|
|
103
|
+
const trivia = require_grammar.scssAstGrammar.whitespace;
|
|
104
|
+
if (entry === void 0 || trivia === void 0) throw new TypeError("SCSS AST grammar is missing its public document entry.");
|
|
105
|
+
const result = (0, parseman.run)(entry, input, { trivia });
|
|
106
|
+
if (!result.ok || result.unconsumedFrom !== null || !isStylesheet(result.value)) throw new ScssParseError(result.ok ? result.unconsumedFrom ?? result.span.end : result.span.start, result.expected);
|
|
107
|
+
return (0, _jesscss_core_ast.withTriviaMap)((0, _jesscss_core_ast.withSourceSpan)(lowerUserFunctionCalls(result.value), result.span), (0, _jesscss_core_ast.createTriviaMapFromParseman)(input, result.triviaMap));
|
|
108
|
+
}
|
|
109
|
+
//#endregion
|
|
110
|
+
exports.ScssParseError = ScssParseError;
|
|
111
|
+
exports.parse = parse;
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type Stylesheet } from '@jesscss/core/ast';
|
|
2
|
+
/** Structured failure from the public direct SCSS parser. */
|
|
3
|
+
export declare class ScssParseError extends SyntaxError {
|
|
4
|
+
readonly code: 'parse/syntax-error';
|
|
5
|
+
readonly offset: number;
|
|
6
|
+
readonly expected: readonly string[];
|
|
7
|
+
constructor(offset: number, expected: readonly string[]);
|
|
8
|
+
}
|
|
9
|
+
/** Parse SCSS directly into the canonical AST v2 document. */
|
|
10
|
+
export declare function parse(input: string): Stylesheet;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { scssAstGrammar } from "./grammar.js";
|
|
2
|
+
import { run } from "parseman";
|
|
3
|
+
import { createTriviaMapFromParseman, reference, variableReference, withSourceSpan, withTriviaMap } from "@jesscss/core/ast";
|
|
4
|
+
//#region src/ast/lower-user-function-calls.ts
|
|
5
|
+
/**
|
|
6
|
+
* SCSS call-site lowering: a user `@function f(...)` was lowered (in the grammar)
|
|
7
|
+
* to a `$var`-bound value lambda (`AnonymousMixin` with `params`). A CALL to that
|
|
8
|
+
* function — `f(2)` — parses as an ordinary `FunctionCall`, indistinguishable at
|
|
9
|
+
* the leaf from a builtin Sass call (`darken(...)`). This post-parse pass rewrites
|
|
10
|
+
* every `FunctionCall` whose name is a USER-defined `@function` into the shared
|
|
11
|
+
* `$f(args)` invoke form (a `Reference` with a `Call` step on the bound variable),
|
|
12
|
+
* so it reaches the general "call a value-lambda" evaluator path. Builtin Sass
|
|
13
|
+
* functions are left as `FunctionCall`s and continue to route to `fns`.
|
|
14
|
+
*
|
|
15
|
+
* User-function names are collected from the WHOLE document first (Sass hoists and
|
|
16
|
+
* effectively globalises function definitions), so a call may precede its `@function`.
|
|
17
|
+
* The only SCSS construct that binds an `AnonymousMixin` to a `$var` is `@function`,
|
|
18
|
+
* so "a VariableDeclaration whose value is an AnonymousMixin" is an exact marker.
|
|
19
|
+
*/
|
|
20
|
+
function isRecord(value) {
|
|
21
|
+
return typeof value === "object" && value !== null;
|
|
22
|
+
}
|
|
23
|
+
function isFunctionCall(value) {
|
|
24
|
+
return isRecord(value) && value.type === "FunctionCall" && typeof value.name === "string" && Array.isArray(value.args);
|
|
25
|
+
}
|
|
26
|
+
/** A non-array value slot is a single value node. */
|
|
27
|
+
function isValueNode(slot) {
|
|
28
|
+
return !Array.isArray(slot);
|
|
29
|
+
}
|
|
30
|
+
function isStylesheet$1(value) {
|
|
31
|
+
return isRecord(value) && value.type === "Stylesheet" && Array.isArray(value.children);
|
|
32
|
+
}
|
|
33
|
+
/** Collect every user `@function` name: a VariableDeclaration bound to an
|
|
34
|
+
* AnonymousMixin (the only SCSS shape that produces one). */
|
|
35
|
+
function collectUserFunctionNames(node, into) {
|
|
36
|
+
if (Array.isArray(node)) {
|
|
37
|
+
for (const child of node) collectUserFunctionNames(child, into);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
if (!isRecord(node)) return;
|
|
41
|
+
if (node.type === "VariableDeclaration" && typeof node.name === "string" && isRecord(node.value) && node.value.type === "AnonymousMixin") into.add(node.name);
|
|
42
|
+
for (const key of Object.keys(node)) collectUserFunctionNames(node[key], into);
|
|
43
|
+
}
|
|
44
|
+
/** Best-effort authored spelling of a call argument, for a Reference `raw`
|
|
45
|
+
* fallback (only ever emitted if the invoke fails to resolve). */
|
|
46
|
+
function argRaw(slot) {
|
|
47
|
+
if (!isValueNode(slot)) return slot.map(argRaw).join(" ");
|
|
48
|
+
if (slot.type === "VariableReference") return `$${slot.name}`;
|
|
49
|
+
if ("src" in slot && typeof slot.src === "string") return slot.src;
|
|
50
|
+
return "";
|
|
51
|
+
}
|
|
52
|
+
/** Deep-transform the tree, rewriting user-function `FunctionCall`s (post-order,
|
|
53
|
+
* so nested user calls inside the args lower first). Non-AST scalars pass through. */
|
|
54
|
+
function rewrite(node, userFns) {
|
|
55
|
+
if (Array.isArray(node)) return node.map((child) => rewrite(child, userFns));
|
|
56
|
+
if (!isRecord(node)) return node;
|
|
57
|
+
const out = {};
|
|
58
|
+
for (const key of Object.keys(node)) out[key] = rewrite(node[key], userFns);
|
|
59
|
+
if (isFunctionCall(out) && userFns.has(out.name)) {
|
|
60
|
+
const args = out.args.map((value) => ({ value }));
|
|
61
|
+
const raw = `${out.name}(${out.args.map(argRaw).join(", ")})`;
|
|
62
|
+
return reference(variableReference(out.name, "live"), [{
|
|
63
|
+
type: "Call",
|
|
64
|
+
args
|
|
65
|
+
}], raw);
|
|
66
|
+
}
|
|
67
|
+
return out;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Rewrite user-`@function` call sites in a parsed SCSS document to `$f(args)`
|
|
71
|
+
* lambda invokes. Returns the document unchanged when it defines no user function.
|
|
72
|
+
*/
|
|
73
|
+
function lowerUserFunctionCalls(sheet) {
|
|
74
|
+
const userFns = /* @__PURE__ */ new Set();
|
|
75
|
+
collectUserFunctionNames(sheet.children, userFns);
|
|
76
|
+
if (userFns.size === 0) return sheet;
|
|
77
|
+
const lowered = rewrite(sheet, userFns);
|
|
78
|
+
if (!isStylesheet$1(lowered)) throw new TypeError("SCSS user-function lowering did not preserve the Stylesheet root.");
|
|
79
|
+
return lowered;
|
|
80
|
+
}
|
|
81
|
+
//#endregion
|
|
82
|
+
//#region src/index.ts
|
|
83
|
+
/** Structured failure from the public direct SCSS parser. */
|
|
84
|
+
var ScssParseError = class extends SyntaxError {
|
|
85
|
+
code = "parse/syntax-error";
|
|
86
|
+
offset;
|
|
87
|
+
expected;
|
|
88
|
+
constructor(offset, expected) {
|
|
89
|
+
const detail = expected.length > 0 ? ` Expected: ${expected.join(", ")}.` : "";
|
|
90
|
+
super(`SCSS parser error.${detail}`);
|
|
91
|
+
this.name = "ScssParseError";
|
|
92
|
+
this.offset = offset;
|
|
93
|
+
this.expected = expected;
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
function isStylesheet(value) {
|
|
97
|
+
return typeof value === "object" && value !== null && "type" in value && value.type === "Stylesheet" && "children" in value && Array.isArray(value.children);
|
|
98
|
+
}
|
|
99
|
+
/** Parse SCSS directly into the canonical AST v2 document. */
|
|
100
|
+
function parse(input) {
|
|
101
|
+
const entry = scssAstGrammar.Stylesheet;
|
|
102
|
+
const trivia = scssAstGrammar.whitespace;
|
|
103
|
+
if (entry === void 0 || trivia === void 0) throw new TypeError("SCSS AST grammar is missing its public document entry.");
|
|
104
|
+
const result = run(entry, input, { trivia });
|
|
105
|
+
if (!result.ok || result.unconsumedFrom !== null || !isStylesheet(result.value)) throw new ScssParseError(result.ok ? result.unconsumedFrom ?? result.span.end : result.span.start, result.expected);
|
|
106
|
+
return withTriviaMap(withSourceSpan(lowerUserFunctionCalls(result.value), result.span), createTriviaMapFromParseman(input, result.triviaMap));
|
|
107
|
+
}
|
|
108
|
+
//#endregion
|
|
109
|
+
export { ScssParseError, parse };
|
package/package.json
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@jesscss/scss-parser",
|
|
3
|
+
"publishConfig": {
|
|
4
|
+
"access": "public"
|
|
5
|
+
},
|
|
6
|
+
"description": "",
|
|
7
|
+
"version": "2.0.0-alpha.10",
|
|
8
|
+
"engines": {
|
|
9
|
+
"node": "^20.19.0 || >=22.12.0"
|
|
10
|
+
},
|
|
11
|
+
"main": "lib/index.cjs",
|
|
12
|
+
"types": "lib/index.d.ts",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./lib/index.d.ts",
|
|
16
|
+
"import": "./lib/index.js",
|
|
17
|
+
"require": "./lib/index.cjs"
|
|
18
|
+
},
|
|
19
|
+
"./cst": {
|
|
20
|
+
"types": "./lib/cst.d.ts",
|
|
21
|
+
"import": "./lib/cst.js",
|
|
22
|
+
"require": "./lib/cst.cjs"
|
|
23
|
+
},
|
|
24
|
+
"./grammar": {
|
|
25
|
+
"types": "./lib/grammar.d.ts",
|
|
26
|
+
"import": "./lib/grammar.js",
|
|
27
|
+
"require": "./lib/grammar.cjs"
|
|
28
|
+
},
|
|
29
|
+
"./package.json": "./package.json"
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"lib"
|
|
33
|
+
],
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@jesscss/css-parser": "2.0.0-alpha.10"
|
|
36
|
+
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"parseman": "^0.41.0",
|
|
39
|
+
"@jesscss/core": "2.0.0-alpha.10"
|
|
40
|
+
},
|
|
41
|
+
"peerDependenciesMeta": {
|
|
42
|
+
"@jesscss/core": {
|
|
43
|
+
"optional": true
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"parseman": "^0.41.0",
|
|
48
|
+
"sass-spec": "github:sass/sass-spec",
|
|
49
|
+
"@jesscss/parser-shared": "0.0.0",
|
|
50
|
+
"@jesscss/core": "2.0.0-alpha.10"
|
|
51
|
+
},
|
|
52
|
+
"author": "Matthew Dean <matthew-dean@users.noreply.github.com>",
|
|
53
|
+
"license": "MIT",
|
|
54
|
+
"bugs": {
|
|
55
|
+
"url": "https://github.com/jesscss/jess/issues"
|
|
56
|
+
},
|
|
57
|
+
"homepage": "https://github.com/jesscss/jess#readme",
|
|
58
|
+
"type": "module",
|
|
59
|
+
"module": "lib/index.js",
|
|
60
|
+
"scripts": {
|
|
61
|
+
"postinstall": "node ./scripts/materialize-sass-spec-cache.cjs",
|
|
62
|
+
"ci": "pnpm build && pnpm test",
|
|
63
|
+
"build": "pnpm --filter @jesscss/parser-shared build && pnpm compile",
|
|
64
|
+
"compile": "tsdown --tsconfig tsconfig.build.json --no-dts && tsc -p tsconfig.build.json --emitDeclarationOnly --noCheck",
|
|
65
|
+
"dev": "tsdown --tsconfig tsconfig.build.json --no-dts --watch",
|
|
66
|
+
"sass-spec:cache": "node ./scripts/materialize-sass-spec-cache.cjs",
|
|
67
|
+
"test": "vitest --run --passWithNoTests",
|
|
68
|
+
"lint:fix": "eslint --fix '**/*.{js,ts}'",
|
|
69
|
+
"lint": "eslint '**/*.{js,ts}'"
|
|
70
|
+
}
|
|
71
|
+
}
|