@jesscss/less-parser 2.0.0-alpha.6 → 2.0.0-alpha.8

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.
Files changed (62) hide show
  1. package/README.md +149 -36
  2. package/lib/builders.d.ts +381 -0
  3. package/lib/builders.d.ts.map +1 -0
  4. package/lib/cst.cjs +14 -0
  5. package/lib/cst.d.ts +6 -0
  6. package/lib/cst.d.ts.map +1 -0
  7. package/lib/cst.js +12 -0
  8. package/lib/functional-parser.cjs +2654 -0
  9. package/lib/functional-parser.d.ts +18 -0
  10. package/lib/functional-parser.d.ts.map +1 -0
  11. package/lib/functional-parser.js +2589 -0
  12. package/lib/grammar.cjs +35057 -0
  13. package/lib/grammar.d.ts +2 -0
  14. package/lib/grammar.d.ts.map +1 -0
  15. package/lib/grammar.js +35056 -0
  16. package/lib/index.cjs +17 -4096
  17. package/lib/index.d.ts +9 -149
  18. package/lib/index.d.ts.map +1 -1
  19. package/lib/index.js +6 -4091
  20. package/lib/jess.cjs +3968 -0
  21. package/lib/jess.d.ts +7 -0
  22. package/lib/jess.d.ts.map +1 -0
  23. package/lib/jess.js +3962 -0
  24. package/lib/lessParser.d.ts +38 -0
  25. package/lib/lessParser.d.ts.map +1 -0
  26. package/lib/lessRecursiveParser.d.ts +99 -0
  27. package/lib/lessRecursiveParser.d.ts.map +1 -0
  28. package/lib/lessTokens.d.ts +23 -0
  29. package/lib/lessTokens.d.ts.map +1 -0
  30. package/lib/productions/guards.d.ts +113 -0
  31. package/lib/productions/guards.d.ts.map +1 -0
  32. package/lib/productions/index.d.ts +5 -0
  33. package/lib/productions/index.d.ts.map +1 -0
  34. package/lib/productions/root.d.ts +38 -0
  35. package/lib/productions/root.d.ts.map +1 -0
  36. package/lib/productions/selectors.d.ts +41 -0
  37. package/lib/productions/selectors.d.ts.map +1 -0
  38. package/lib/productions/values.d.ts +35 -0
  39. package/lib/productions/values.d.ts.map +1 -0
  40. package/lib/utils.d.ts +9 -0
  41. package/lib/utils.d.ts.map +1 -0
  42. package/package.json +40 -9
  43. package/src/__tests__/debug-log.ts +35 -0
  44. package/src/__tests__/wall5-parse.test.ts +67 -0
  45. package/src/builders.ts +3190 -0
  46. package/src/cst.ts +25 -0
  47. package/src/functional-parser.ts +162 -0
  48. package/src/grammar.ts +870 -0
  49. package/src/index.ts +19 -0
  50. package/src/jess.ts +6 -0
  51. package/src/lessParser.ts +120 -0
  52. package/src/lessRecursiveParser.ts +279 -0
  53. package/src/lessTokens.ts +350 -0
  54. package/src/productions/guards.ts +1066 -0
  55. package/src/productions/index.ts +29 -0
  56. package/src/productions/root.ts +1613 -0
  57. package/src/productions/selectors.ts +1309 -0
  58. package/src/productions/values.ts +1449 -0
  59. package/src/utils.ts +178 -0
  60. package/lib/index.d.cts +0 -150
  61. package/lib/index.d.cts.map +0 -1
  62. package/lib/index.js.map +0 -1
package/src/utils.ts ADDED
@@ -0,0 +1,178 @@
1
+ import {
2
+ InterpolatedSelector,
3
+ Interpolated,
4
+ Quoted,
5
+ Reference,
6
+ INTERPOLATION_PLACEHOLDER,
7
+ isNode,
8
+ N,
9
+ type Selector
10
+ } from '@jesscss/core';
11
+
12
+ // Pre-compiled regex for @{variable} interpolation - more efficient than creating new instances
13
+ const INTERPOLATION_REGEX = /([$@])\{([^}]+)\}/g;
14
+
15
+ export const createInterpolatedReference = (
16
+ prefix: string,
17
+ varName: string,
18
+ location?: any,
19
+ context?: any
20
+ ): Reference => {
21
+ const isProperty = prefix === '$';
22
+ const key = isProperty
23
+ ? new Quoted(varName, { quote: '\'' }, location)
24
+ : varName;
25
+ return new Reference(
26
+ { key },
27
+ { type: isProperty ? 'index' : 'variable', role: 'ident' },
28
+ location
29
+ );
30
+ };
31
+
32
+ export const getInterpolatedNode = (
33
+ name: string,
34
+ location?: any,
35
+ context?: any
36
+ ): Interpolated => {
37
+ const replacements: any[] = [];
38
+ let source = name;
39
+ let result;
40
+
41
+ INTERPOLATION_REGEX.lastIndex = 0;
42
+ while ((result = INTERPOLATION_REGEX.exec(name)) !== null) {
43
+ const [match, prefix, varName] = result;
44
+ source = source.replace(match, INTERPOLATION_PLACEHOLDER);
45
+ replacements.push(createInterpolatedReference(prefix ?? '', varName ?? '', location, context));
46
+ }
47
+
48
+ return new Interpolated({ source, replacements }, { role: 'ident' }, location);
49
+ };
50
+
51
+ export const normalizeMixinReferenceKey = (selector: Selector): { key: string | string[]; rawKey: Selector } => {
52
+ if (isNode(selector, N.BasicSelector) || selector instanceof InterpolatedSelector) {
53
+ return { key: selector.valueOf(), rawKey: selector };
54
+ }
55
+
56
+ if (isNode(selector, N.CompoundSelector)) {
57
+ return {
58
+ key: selector.value.map(node => node.valueOf()),
59
+ rawKey: selector
60
+ };
61
+ }
62
+
63
+ if (isNode(selector, N.ComplexSelector)) {
64
+ const path: string[] = [];
65
+ let canUsePath = true;
66
+
67
+ for (const node of selector.value) {
68
+ if (isNode(node, N.BasicSelector) || node instanceof InterpolatedSelector) {
69
+ path.push(node.valueOf());
70
+ continue;
71
+ }
72
+ if (isNode(node, N.CompoundSelector)) {
73
+ path.push(...node.value.map(child => child.valueOf()));
74
+ continue;
75
+ }
76
+ if (isNode(node, N.Combinator) && (node.value === '>' || node.value === ' ')) {
77
+ continue;
78
+ }
79
+ canUsePath = false;
80
+ break;
81
+ }
82
+
83
+ if (canUsePath && path.length > 0) {
84
+ return { key: path, rawKey: selector };
85
+ }
86
+ }
87
+
88
+ return { key: selector.valueOf(), rawKey: selector };
89
+ };
90
+
91
+ /* Handle both @{variable} interpolation and @id-@num variable variables */
92
+ export const getInterpolatedOrString = (name: string, location?: any, context?: any): Interpolated | string => {
93
+ // First check for @{variable} interpolation syntax
94
+ const matches: Array<{ fullMatch: string; prefix: string; varName: string; index: number }> = [];
95
+
96
+ // Reset regex state and collect all matches
97
+ INTERPOLATION_REGEX.lastIndex = 0;
98
+ let result;
99
+ while ((result = INTERPOLATION_REGEX.exec(name)) !== null) {
100
+ const [fullMatch, prefix, varName] = result;
101
+ if (varName && prefix) {
102
+ matches.push({
103
+ fullMatch,
104
+ prefix,
105
+ varName,
106
+ index: result.index
107
+ });
108
+ }
109
+ }
110
+
111
+ if (matches.length > 0) {
112
+ // Build source string and replacements
113
+ let source = name;
114
+ const replacements: any[] = [];
115
+ let offset = 0; // Track how much the string has been modified
116
+
117
+ // Process matches in forward order to maintain correct indices
118
+ for (let i = 0; i < matches.length; i++) {
119
+ const match = matches[i]!;
120
+ const adjustedIndex = match.index - offset;
121
+ const beforeMatch = source.substring(0, adjustedIndex);
122
+ const afterMatch = source.substring(adjustedIndex + match.fullMatch.length);
123
+
124
+ source = beforeMatch + INTERPOLATION_PLACEHOLDER + afterMatch;
125
+ offset += match.fullMatch.length - INTERPOLATION_PLACEHOLDER.length;
126
+
127
+ const ref = createInterpolatedReference(match.prefix, match.varName, location, context);
128
+ replacements.push(ref); // Add to end to maintain order
129
+ }
130
+
131
+ return new Interpolated({ source, replacements }, { role: 'ident' }, location);
132
+ }
133
+
134
+ // If no interpolation found, check for @id-@num variable variables
135
+ const atPos = name.indexOf('@', 1);
136
+ const dollarPos = name.indexOf('$', 1);
137
+
138
+ if (atPos === -1 && dollarPos === -1) {
139
+ if (name.startsWith('@') || name.startsWith('$')) {
140
+ return name.slice(1);
141
+ } else {
142
+ return name;
143
+ }
144
+ }
145
+
146
+ const nextPos = atPos !== -1 ? atPos : dollarPos;
147
+ const start = name.slice(1, nextPos);
148
+ const end = name.slice(nextPos);
149
+ const type: 'variable' | 'index' = end.startsWith('@') ? 'variable' : 'index';
150
+ // For @id-@num variable variables, we need to create an Interpolated node
151
+ const endResult = getInterpolatedOrString(end, location, context);
152
+ if (typeof endResult === 'string') {
153
+ const endKey = type === 'index'
154
+ ? new Quoted(endResult, { quote: '\'' }, location)
155
+ : endResult;
156
+ return new Interpolated({
157
+ source: start + INTERPOLATION_PLACEHOLDER,
158
+ replacements: [
159
+ new Reference(
160
+ { key: endKey },
161
+ { type, role: 'ident' },
162
+ location
163
+ )
164
+ ]
165
+ }, { role: 'ident' });
166
+ } else {
167
+ /**
168
+ * endResult is already an Interpolated node, so we need to handle this
169
+ * differently.
170
+ *
171
+ * @todo - test deep nesting
172
+ */
173
+ return new Interpolated({
174
+ source: start + INTERPOLATION_PLACEHOLDER,
175
+ replacements: [type === 'index' ? new Quoted(endResult, { quote: '\'' }, location) : endResult]
176
+ }, { role: 'ident' });
177
+ }
178
+ };
package/lib/index.d.cts DELETED
@@ -1,150 +0,0 @@
1
- import { CssRecursiveParser, CssRecursiveParserConfig, CssTokenType, RawModeConfig, RuleContext as RuleContext$1, TokenNames } from "@jesscss/css-parser";
2
- import { IToken, Lexer, TokenType } from "chevrotain";
3
- import { ComplexSelector, Extend, IParseResult, MathMode, Node, Rules, Selector, TreeContext } from "@jesscss/core";
4
-
5
- //#region src/lessTokens.d.ts
6
- /**
7
- * Less-specific token names introduced by merges below
8
- *
9
- * @todo - Can't we infer this from the tokens?
10
- */
11
- type LessExtraTokenType = 'Ellipsis' | 'AtKeywordLessExtension' | 'Interpolated' | 'LineComment' | 'PlusAssign' | 'UnderscoreAssign' | 'AnonMixinStart' | 'GtEqAlias' | 'LtEqAlias' | 'Extend' | 'AmpersandExtend' | 'AmpersandLParen' | 'AmpersandTemplateContents' | 'AmpersandTemplateEnd' | 'AllFlag' | 'When' | 'WhenFunctionStart' | 'VarOrProp' | 'NestedReference' | 'PropertyReference' | 'Percent' | 'FormatFunction' | 'IfFunction' | 'BooleanFunction' | 'DefaultGuardIdent' | 'DefaultGuardFunc' | 'JavaScript' | 'InterpolatedIdent' | 'InterpolatedCustomProperty' | 'InterpolatedSelector';
12
- declare function $preBuildTokens(): {
13
- modes: any;
14
- defaultMode: "Default";
15
- };
16
- declare const Fragments: string[][];
17
- declare const Tokens: {
18
- modes: any;
19
- defaultMode: "Default";
20
- };
21
- type ReturnTokens = ReturnType<typeof $preBuildTokens>;
22
- type TokenModes = ReturnTokens['modes'];
23
- type LessTokenType = TokenNames<TokenModes[keyof TokenModes]>;
24
- declare const lessFragments: () => ReadonlyArray<Readonly<[string, string]>>;
25
- declare const lessTokens: () => RawModeConfig;
26
- //#endregion
27
- //#region src/lessRecursiveParser.d.ts
28
- type LessParserConfig = CssRecursiveParserConfig & {
29
- /**
30
- * Is less strict with certain CSS rules and Less syntax
31
- * that the old Less parser allowed.
32
- *
33
- * @note This will also enable CSS legacyMode unless
34
- * legacyMode is explicitly false.
35
- */
36
- looseMode?: boolean;
37
- /**
38
- * Controls whether mixins and detached rulesets "leak" their inner rules.
39
- * When true (default):
40
- * - Mixins: Mixin and VarDeclaration nodes are 'public' and 'optional' respectively
41
- * - Detached rulesets: Mixin and VarDeclaration nodes are 'public' and 'private' respectively
42
- * When false:
43
- * - Both mixins and detached rulesets: Mixin and VarDeclaration nodes are 'private'
44
- */
45
- leakyRules?: boolean;
46
- /**
47
- * Less math evaluation mode. Used during parsing to decide whether a given
48
- * `Operation` should be represented as an `Expression` for Less→Jess conversion.
49
- *
50
- * Mirrors runtime behavior in `Context.shouldOperate()`.
51
- *
52
- * @default 'parens-division'
53
- */
54
- mathMode?: MathMode;
55
- /**
56
- * When enabled (default), the parser will wrap the *outermost* Less math/value
57
- * expressions (math operations, variable references, and chained mixin/variable
58
- * calls) in an `Expression({ parens: true })`.
59
- *
60
- * This is purely a parse-time AST shape choice to support Less→Jess conversion.
61
- *
62
- * @default true
63
- */
64
- wrapOuterExpressions?: boolean;
65
- };
66
- type CombinedTokenMap = Record<CssTokenType, TokenType> & Record<LessExtraTokenType, TokenType>;
67
- type TokenMap = CombinedTokenMap;
68
- interface ExtendTarget {
69
- selector?: Selector;
70
- target: Selector;
71
- flag: IToken | undefined;
72
- }
73
- type RuleContext = RuleContext$1 & {
74
- selector?: Selector;
75
- hasDefault?: boolean;
76
- allExtended?: boolean;
77
- isDefinition?: boolean;
78
- allowAnonymousMixins?: boolean;
79
- requireAccessorsAfterMixinCall?: boolean;
80
- inValueList?: boolean;
81
- allowComma?: boolean;
82
- node?: Node;
83
- ruleIsFinished?: boolean;
84
- sequences?: Array<ComplexSelector | Extend>;
85
- asReference?: boolean;
86
- extendTargets?: ExtendTarget[];
87
- extendNodes?: Extend[];
88
- inExtend?: boolean;
89
- wrapInExpression?: boolean;
90
- parenFrames?: boolean[];
91
- calcFrames?: number;
92
- detachedRulesetUsage?: 'function-arg' | 'mixin-arg' | 'default-param';
93
- inFunctionArgs?: boolean;
94
- allowMixinCallWithoutAccessor?: boolean;
95
- startValue?: Node;
96
- };
97
- declare class LessRecursiveParser extends CssRecursiveParser {
98
- T: TokenMap;
99
- looseMode: boolean;
100
- leakyRules: boolean;
101
- /** Warnings collected during parsing */
102
- warnings: Array<{
103
- message: string;
104
- token?: IToken;
105
- deprecation?: string;
106
- }>;
107
- /** See `LessParserConfig.mathMode` */
108
- mathMode: MathMode;
109
- /** See `LessParserConfig.wrapOuterExpressions` */
110
- wrapOuterExpressions: boolean;
111
- constructor(T: TokenMap, config?: LessParserConfig);
112
- protected processValueToken(token: IToken, ctx?: RuleContext): Node;
113
- shouldTryQualifiedRuleInDeclarationList(): boolean;
114
- warnDeprecation(message: string, token?: IToken, deprecationId?: string): void;
115
- }
116
- //#endregion
117
- //#region src/lessParser.d.ts
118
- type LessRules = keyof { [K in keyof LessRecursiveParser as LessRecursiveParser[K] extends ((...args: any[]) => Node) ? K : never]: true };
119
- type SyntacticContentAssistSuggestion = {
120
- nextTokenType: string;
121
- nextTokenLabel?: string;
122
- ruleStack: string[];
123
- };
124
- /**
125
- * Less parser using the new recursive-descent engine.
126
- * Keeps Chevrotain's lexer, replaces the parser.
127
- */
128
- declare class LessParser {
129
- lexer: Lexer;
130
- parser: LessRecursiveParser;
131
- constructor(config?: LessParserConfig);
132
- parse(text: string): IParseResult<Rules>;
133
- parse(text: string, rule: 'stylesheet'): IParseResult<Rules>;
134
- parse(text: string, rule: 'stylesheet', options: {
135
- context?: TreeContext;
136
- }): IParseResult<Rules>;
137
- parse(text: string, rule?: LessRules, options?: {
138
- context?: TreeContext;
139
- }): IParseResult;
140
- /**
141
- * @todo Implement content assist for the new parser
142
- */
143
- suggest(text: string, init: {
144
- offset: number;
145
- rule?: LessRules;
146
- }): SyntacticContentAssistSuggestion[];
147
- }
148
- //#endregion
149
- export { CombinedTokenMap, ExtendTarget, Fragments, LessExtraTokenType, LessParser, LessParser as Parser, LessParserConfig, LessRecursiveParser, LessRules, LessTokenType, RuleContext, SyntacticContentAssistSuggestion, TokenMap, Tokens, lessFragments, lessTokens };
150
- //# sourceMappingURL=index.d.cts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.cts","names":[],"sources":["../src/lessTokens.ts","../src/lessRecursiveParser.ts","../src/lessParser.ts"],"mappings":";;;;;;;;;AAqBA;KAAY,kBAAA;AAAA,iBAwCH,eAAA,CAAA;EAAe,KAAA;EAAA,WAAA;AAAA;AAAA,cA4RX,SAAA;AAAA,cACA,MAAA;EAA2B,KAAA;EAAA,WAAA;AAAA;AAAA,KAEnC,YAAA,GAAe,UAAA,QAAkB,eAAA;AAAA,KACjC,UAAA,GAAa,YAAA;AAAA,KAEN,aAAA,GAAgB,UAAA,CAAW,UAAA,OAAiB,UAAA;AAAA,cAE3C,aAAA,QAA8C,aAAA,CAAc,QAAA;AAAA,cAC5D,UAAA,QAAwC,aAAA;;;KClUzC,gBAAA,GAAmB,wBAAA;EDXnB;;;;;AA8Be;;ECXzB,SAAA;EDqBsB;;AA4RxB;;;;;AACA;ECxSE,UAAA;;;;ADwSuC;;;;;EC9RvC,QAAA,GAAW,QAAA;EDiSE;;;;AAEf;;;;;ECxRE,oBAAA;AAAA;AAAA,KAIU,gBAAA,GAAmB,MAAA,CAAO,YAAA,EAAc,SAAA,IAAa,MAAA,CAAO,kBAAA,EAAoB,SAAA;AAAA,KAChF,QAAA,GAAW,gBAAA;AAAA,UAEN,YAAA;EACf,QAAA,GAAW,QAAA;EACX,MAAA,EAAQ,QAAA;EACR,IAAA,EAAM,MAAA;AAAA;AAAA,KAGI,WAAA,GAAc,aAAA;EAC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