@jesscss/scss-parser 2.0.0-alpha.11 → 2.0.0-alpha.13

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/lib/index.cjs CHANGED
@@ -1,111 +1,18 @@
1
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
2
+ const require_parse_with = require("./chunks/parse-with.cjs");
3
+ let src_grammar_ast_js = require("./grammar/ast.cjs");
4
+ //#region src/index.ts
6
5
  /**
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`.
6
+ * Parse SCSS directly into the canonical AST v2 document.
15
7
  *
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.
8
+ * Spans carry offsets only. For `startLine`/`startColumn` facts import `parse`
9
+ * from `@jesscss/scss-parser/positions` the same function bound to the
10
+ * line-aware compiled table. This entry never loads that table.
73
11
  */
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
12
  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));
13
+ return require_parse_with.parseWith(src_grammar_ast_js.scssGrammar, input);
108
14
  }
109
15
  //#endregion
110
- exports.ScssParseError = ScssParseError;
16
+ exports.ScssImportPostludeError = require_parse_with.ScssImportPostludeError;
17
+ exports.ScssParseError = require_parse_with.ScssParseError;
111
18
  exports.parse = parse;
package/lib/index.d.ts CHANGED
@@ -1,10 +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. */
1
+ import type { Stylesheet } from '@jesscss/core/ast';
2
+ export { ScssImportPostludeError, ScssParseError } from './parse-error.js';
3
+ /**
4
+ * Parse SCSS directly into the canonical AST v2 document.
5
+ *
6
+ * Spans carry offsets only. For `startLine`/`startColumn` facts import `parse`
7
+ * from `@jesscss/scss-parser/positions` — the same function bound to the
8
+ * line-aware compiled table. This entry never loads that table.
9
+ */
10
10
  export declare function parse(input: string): Stylesheet;
package/lib/index.js CHANGED
@@ -1,109 +1,15 @@
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
1
+ import { n as ScssImportPostludeError, r as ScssParseError, t as parseWith } from "./chunks/parse-with.js";
2
+ import { scssGrammar } from "./grammar/ast.js";
3
+ //#region src/index.ts
5
4
  /**
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`.
5
+ * Parse SCSS directly into the canonical AST v2 document.
14
6
  *
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.
7
+ * Spans carry offsets only. For `startLine`/`startColumn` facts import `parse`
8
+ * from `@jesscss/scss-parser/positions` the same function bound to the
9
+ * line-aware compiled table. This entry never loads that table.
72
10
  */
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
11
  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));
12
+ return parseWith(scssGrammar, input);
107
13
  }
108
14
  //#endregion
109
- export { ScssParseError, parse };
15
+ export { ScssImportPostludeError, ScssParseError, parse };
@@ -0,0 +1,43 @@
1
+ /**
2
+ * The public SCSS parse failure lives in its own module so that both AST
3
+ * entries — `.` and `./positions` — can export it without either one reaching
4
+ * the other's compiled grammar table. A class declared in an entry cannot be
5
+ * re-exported by a sibling entry without dragging that entry's imports along.
6
+ */
7
+ /** Structured failure from the public direct SCSS parser. */
8
+ export declare class ScssParseError extends SyntaxError {
9
+ readonly code: "parse/syntax-error";
10
+ readonly offset: number;
11
+ readonly expected: readonly string[];
12
+ readonly line?: number;
13
+ readonly column?: number;
14
+ readonly endLine?: number;
15
+ readonly endColumn?: number;
16
+ readonly reason?: string;
17
+ readonly fix?: string;
18
+ constructor(offset: number, expected: readonly string[], options?: {
19
+ message?: string;
20
+ reason?: string;
21
+ fix?: string;
22
+ line?: number;
23
+ column?: number;
24
+ endLine?: number;
25
+ endColumn?: number;
26
+ });
27
+ }
28
+ /**
29
+ * A media/layer/supports postlude belongs to the plain CSS `@import` form only.
30
+ *
31
+ * Once the parser has decided an `@import` is compile-time — a Sass partial
32
+ * rather than a `.css` file or a URL — a trailing query has nothing left to
33
+ * describe: the partial's rules are spliced into this document, not linked as a
34
+ * separate CSS resource.
35
+ */
36
+ export declare class ScssImportPostludeError extends SyntaxError {
37
+ readonly code: "parse/import-postlude-on-compile-time-import";
38
+ readonly offset: number;
39
+ readonly endOffset: number;
40
+ readonly reason = "A media, layer, or supports query is only valid on a plain CSS @import.";
41
+ readonly fix = "Drop the query, or wrap the import in an explicit @media/@supports/@layer block.";
42
+ constructor(offset: number, endOffset: number);
43
+ }
@@ -0,0 +1,5 @@
1
+ import { type Stylesheet } from '@jesscss/core/ast';
2
+ import type { scssGrammar } from './grammar/ast.js';
3
+ /** The rule map both compiled SCSS AST variants expose. */
4
+ export type ScssAstGrammar = typeof scssGrammar;
5
+ export declare function parseWith(grammar: ScssAstGrammar, input: string): Stylesheet;
@@ -0,0 +1,12 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_parse_with = require("./chunks/parse-with.cjs");
3
+ let src_grammar_ast_positions_js = require("./grammar/ast/positions.cjs");
4
+ //#region src/positions.ts
5
+ /** Parse SCSS into the canonical AST v2 document with line/column facts. */
6
+ function parse(input) {
7
+ return require_parse_with.parseWith(src_grammar_ast_positions_js.scssPositionsGrammar, input);
8
+ }
9
+ //#endregion
10
+ exports.ScssImportPostludeError = require_parse_with.ScssImportPostludeError;
11
+ exports.ScssParseError = require_parse_with.ScssParseError;
12
+ exports.parse = parse;
@@ -0,0 +1,9 @@
1
+ import { n as ScssImportPostludeError, r as ScssParseError, t as parseWith } from "./chunks/parse-with.js";
2
+ import { scssPositionsGrammar } from "./grammar/ast/positions.js";
3
+ //#region src/positions.ts
4
+ /** Parse SCSS into the canonical AST v2 document with line/column facts. */
5
+ function parse(input) {
6
+ return parseWith(scssPositionsGrammar, input);
7
+ }
8
+ //#endregion
9
+ export { ScssImportPostludeError, ScssParseError, parse };
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Root-trivia label selection for the SCSS grammar.
3
+ *
4
+ * This is a plain fact about the grammar's trivia arm labels, so it lives in a
5
+ * leaf module with no imports. Reading it from the CST entry instead would make
6
+ * every consumer of the package entry load the compiled CST grammar tables:
7
+ * Node's ESM loader does not tree-shake, so an unused named import still
8
+ * executes the module it is taken from, and each table is multiple megabytes.
9
+ */
10
+ export declare const commentTriviaLabels: readonly ["comment"];
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "access": "public"
5
5
  },
6
6
  "description": "",
7
- "version": "2.0.0-alpha.11",
7
+ "version": "2.0.0-alpha.13",
8
8
  "engines": {
9
9
  "node": "^20.19.0 || >=22.12.0"
10
10
  },
@@ -16,15 +16,45 @@
16
16
  "import": "./lib/index.js",
17
17
  "require": "./lib/index.cjs"
18
18
  },
19
+ "./positions": {
20
+ "types": "./lib/positions.d.ts",
21
+ "import": "./lib/positions.js",
22
+ "require": "./lib/positions.cjs"
23
+ },
19
24
  "./cst": {
20
25
  "types": "./lib/cst.d.ts",
21
26
  "import": "./lib/cst.js",
22
27
  "require": "./lib/cst.cjs"
23
28
  },
29
+ "./cst/positions": {
30
+ "types": "./lib/cst/positions.d.ts",
31
+ "import": "./lib/cst/positions.js",
32
+ "require": "./lib/cst/positions.cjs"
33
+ },
24
34
  "./grammar": {
25
- "types": "./lib/grammar.d.ts",
26
- "import": "./lib/grammar.js",
27
- "require": "./lib/grammar.cjs"
35
+ "types": "./lib/grammar/ast.d.ts",
36
+ "import": "./lib/grammar/ast.js",
37
+ "require": "./lib/grammar/ast.cjs"
38
+ },
39
+ "./grammar/ast": {
40
+ "types": "./lib/grammar/ast.d.ts",
41
+ "import": "./lib/grammar/ast.js",
42
+ "require": "./lib/grammar/ast.cjs"
43
+ },
44
+ "./grammar/ast/positions": {
45
+ "types": "./lib/grammar/ast/positions.d.ts",
46
+ "import": "./lib/grammar/ast/positions.js",
47
+ "require": "./lib/grammar/ast/positions.cjs"
48
+ },
49
+ "./grammar/cst": {
50
+ "types": "./lib/grammar/cst.d.ts",
51
+ "import": "./lib/grammar/cst.js",
52
+ "require": "./lib/grammar/cst.cjs"
53
+ },
54
+ "./grammar/cst/positions": {
55
+ "types": "./lib/grammar/cst/positions.d.ts",
56
+ "import": "./lib/grammar/cst/positions.js",
57
+ "require": "./lib/grammar/cst/positions.cjs"
28
58
  },
29
59
  "./package.json": "./package.json"
30
60
  },
@@ -32,11 +62,11 @@
32
62
  "lib"
33
63
  ],
34
64
  "dependencies": {
35
- "@jesscss/css-parser": "2.0.0-alpha.11"
65
+ "@jesscss/css-parser": "2.0.0-alpha.13"
36
66
  },
37
67
  "peerDependencies": {
38
- "parseman": "^0.41.0",
39
- "@jesscss/core": "2.0.0-alpha.11"
68
+ "parseman": "^0.50.5",
69
+ "@jesscss/core": "2.0.0-alpha.13"
40
70
  },
41
71
  "peerDependenciesMeta": {
42
72
  "@jesscss/core": {
@@ -44,10 +74,10 @@
44
74
  }
45
75
  },
46
76
  "devDependencies": {
47
- "parseman": "^0.41.0",
77
+ "parseman": "^0.50.5",
48
78
  "sass-spec": "github:sass/sass-spec",
49
- "@jesscss/parser-shared": "0.0.0",
50
- "@jesscss/core": "2.0.0-alpha.11"
79
+ "@jesscss/core": "2.0.0-alpha.13",
80
+ "@jesscss/parser-shared": "2.0.0-alpha.13"
51
81
  },
52
82
  "author": "Matthew Dean <matthew-dean@users.noreply.github.com>",
53
83
  "license": "MIT",
@@ -61,7 +91,7 @@
61
91
  "postinstall": "node ./scripts/materialize-sass-spec-cache.cjs",
62
92
  "ci": "pnpm build && pnpm test",
63
93
  "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",
94
+ "compile": "node -e \"require('node:fs').rmSync('lib',{recursive:true,force:true})\" && tsdown --tsconfig tsconfig.build.json --no-dts && tsc -p tsconfig.build.json --emitDeclarationOnly --noCheck",
65
95
  "dev": "tsdown --tsconfig tsconfig.build.json --no-dts --watch",
66
96
  "sass-spec:cache": "node ./scripts/materialize-sass-spec-cache.cjs",
67
97
  "test": "vitest --run --passWithNoTests",
@@ -1,6 +0,0 @@
1
- import type { Stylesheet } from '@jesscss/core/ast';
2
- /**
3
- * Rewrite user-`@function` call sites in a parsed SCSS document to `$f(args)`
4
- * lambda invokes. Returns the document unchanged when it defines no user function.
5
- */
6
- export declare function lowerUserFunctionCalls(sheet: Stylesheet): Stylesheet;