@jesscss/css-parser 1.0.8-alpha.6 → 2.0.0-alpha.2

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 (52) hide show
  1. package/lib/advancedActionsParser.d.ts +60 -0
  2. package/lib/advancedActionsParser.js +203 -0
  3. package/lib/advancedActionsParser.js.map +1 -0
  4. package/lib/cssActionsParser.d.ts +155 -0
  5. package/lib/cssActionsParser.js +376 -0
  6. package/lib/cssActionsParser.js.map +1 -0
  7. package/lib/cssErrorMessageProvider.d.ts +14 -0
  8. package/lib/cssErrorMessageProvider.js +40 -0
  9. package/lib/cssErrorMessageProvider.js.map +1 -0
  10. package/lib/cssParser.d.ts +36 -103
  11. package/lib/cssParser.js +75 -58
  12. package/lib/cssParser.js.map +1 -0
  13. package/lib/cssTokens.d.ts +539 -5
  14. package/lib/cssTokens.js +488 -232
  15. package/lib/cssTokens.js.map +1 -0
  16. package/lib/index.d.ts +8 -16
  17. package/lib/index.js +9 -41
  18. package/lib/index.js.map +1 -0
  19. package/lib/productions.d.ts +273 -0
  20. package/lib/productions.js +3499 -0
  21. package/lib/productions.js.map +1 -0
  22. package/lib/test/ast-serialize.test.d.ts +1 -0
  23. package/lib/test/ast-serialize.test.js +157 -0
  24. package/lib/test/ast-serialize.test.js.map +1 -0
  25. package/lib/test/container.test.d.ts +1 -0
  26. package/lib/test/container.test.js +369 -0
  27. package/lib/test/container.test.js.map +1 -0
  28. package/lib/test/css-files.test.d.ts +1 -0
  29. package/lib/test/css-files.test.js +21 -0
  30. package/lib/test/css-files.test.js.map +1 -0
  31. package/lib/test/less-output.test.d.ts +1 -0
  32. package/lib/test/less-output.test.js +52 -0
  33. package/lib/test/less-output.test.js.map +1 -0
  34. package/lib/util/cst.d.ts +7 -2
  35. package/lib/util/cst.js +5 -9
  36. package/lib/util/cst.js.map +1 -0
  37. package/lib/util/index.d.ts +19 -13
  38. package/lib/util/index.js +98 -87
  39. package/lib/util/index.js.map +1 -0
  40. package/package.json +43 -20
  41. package/lib/productions/atRules.d.ts +0 -2
  42. package/lib/productions/atRules.js +0 -196
  43. package/lib/productions/blocks.d.ts +0 -2
  44. package/lib/productions/blocks.js +0 -181
  45. package/lib/productions/declarations.d.ts +0 -14
  46. package/lib/productions/declarations.js +0 -59
  47. package/lib/productions/root.d.ts +0 -2
  48. package/lib/productions/root.js +0 -49
  49. package/lib/productions/selectors.d.ts +0 -2
  50. package/lib/productions/selectors.js +0 -231
  51. package/lib/productions/values.d.ts +0 -2
  52. package/lib/productions/values.js +0 -114
package/lib/cssParser.js CHANGED
@@ -1,62 +1,79 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.CssParser = void 0;
7
- const chevrotain_1 = require("chevrotain");
8
- const root_1 = __importDefault(require("./productions/root"));
9
- const atRules_1 = __importDefault(require("./productions/atRules"));
10
- const blocks_1 = __importDefault(require("./productions/blocks"));
11
- const selectors_1 = __importDefault(require("./productions/selectors"));
12
- const declarations_1 = __importDefault(require("./productions/declarations"));
13
- const values_1 = __importDefault(require("./productions/values"));
14
- class CssParser extends chevrotain_1.EmbeddedActionsParser {
15
- constructor(tokens, T, config = {
16
- maxLookahead: 1,
17
- recoveryEnabled: true
18
- }) {
19
- super(tokens, config);
20
- const $ = this;
21
- $.T = T;
22
- root_1.default.call($, $);
23
- atRules_1.default.call($, $);
24
- blocks_1.default.call($, $);
25
- selectors_1.default.call($, $);
26
- declarations_1.default.call($, $);
27
- values_1.default.call($, $);
28
- /** If this is extended, don't perform self-analysis twice */
29
- if ($.constructor === CssParser) {
30
- $.performSelfAnalysis();
31
- }
32
- }
33
- /** Capture location information for CST nodes */
34
- CAPTURE(func) {
35
- if (!this.RECORDING_PHASE) {
36
- const startIndex = this.currIdx + 1;
37
- const result = func();
38
- const endIndex = this.currIdx;
39
- if (result && result.name) {
40
- const startToken = this.input[startIndex];
41
- const endToken = this.input[endIndex];
42
- if (startToken && endToken) {
43
- const { startOffset, startColumn, startLine } = startToken;
44
- const { endOffset, endColumn, endLine } = endToken;
45
- result.location = {
46
- startOffset, startColumn, startLine,
47
- endOffset, endColumn, endLine
48
- };
49
- }
50
- }
51
- return result;
52
- }
53
- return func();
1
+ import { Lexer } from 'chevrotain';
2
+ import { cssLexer } from './cssTokens.js';
3
+ import { CssActionsParser } from './cssActionsParser.js';
4
+ import { CssErrorMessageProvider } from './cssErrorMessageProvider.js';
5
+ const errorMessageProvider = new CssErrorMessageProvider();
6
+ /**
7
+ * If we're not extending the CSS parser,
8
+ * this is the friendlier interface for returning
9
+ * a CST, as it assigns tokens to the parser automatically.
10
+ */
11
+ export class CssParser {
12
+ lexer;
13
+ parser;
14
+ /**
15
+ * @note `recoveryEnabled` should be set to true for
16
+ * linting and language services.
17
+ */
18
+ constructor(config = {}) {
19
+ config = {
20
+ errorMessageProvider,
21
+ /**
22
+ * Override this if you want to omit legacy IE syntax
23
+ * and ancient CSS hacks.
24
+ * @todo Allow overriding when parsing a single rule.
25
+ */
26
+ legacyMode: true,
27
+ skipValidations: process.env.TEST !== 'true',
28
+ ...config
29
+ };
30
+ const { lexer, T } = cssLexer;
31
+ this.lexer = new Lexer(lexer, {
32
+ ensureOptimizations: true,
33
+ // Always run the validations during testing (dev flows).
34
+ // And avoid validation during productive flows to reduce the Lexer's startup time.
35
+ skipValidations: process.env.TEST !== 'true'
36
+ });
37
+ this.parser = new CssActionsParser(lexer, T, config);
54
38
  }
55
- RULE(name, impl, config) {
56
- return super.RULE(name, (...args) => this.CAPTURE(() => impl(...args)), config);
39
+ parse(text, rule = 'stylesheet') {
40
+ const parser = this.parser;
41
+ const lexerResult = this.lexer.tokenize(text);
42
+ const lexedTokens = lexerResult.tokens;
43
+ // removed diagnostics
44
+ parser.input = lexedTokens;
45
+ const tree = parser[rule]();
46
+ return {
47
+ tree,
48
+ lexerResult,
49
+ errors: parser.errors,
50
+ warnings: [] // CSS parser doesn't produce deprecation warnings
51
+ };
57
52
  }
58
- OVERRIDE_RULE(name, impl, config) {
59
- return super.OVERRIDE_RULE(name, (...args) => this.CAPTURE(() => impl(...args)), config);
53
+ /**
54
+ * IDE helper: suggest next possible token types at `offset` using Chevrotain's
55
+ * syntactic content assist. This is syntactic-only (not semantic completion).
56
+ *
57
+ * Note: content assist is significantly slower than normal parsing, so it
58
+ * should be called on-demand (e.g. near the cursor).
59
+ */
60
+ suggest(text, init) {
61
+ const { offset, rule = 'stylesheet' } = init;
62
+ const prefix = text.slice(0, Math.max(0, offset));
63
+ const lexerResult = this.lexer.tokenize(prefix);
64
+ const tokens = lexerResult.tokens;
65
+ try {
66
+ const paths = this.parser.computeContentAssist(rule, tokens);
67
+ return paths.map(p => ({
68
+ nextTokenType: p.nextTokenType.name,
69
+ nextTokenLabel: p.nextTokenType.LABEL,
70
+ ruleStack: p.ruleStack,
71
+ occurrenceStack: p.occurrenceStack
72
+ }));
73
+ }
74
+ catch {
75
+ return [];
76
+ }
60
77
  }
61
78
  }
62
- exports.CssParser = CssParser;
79
+ //# sourceMappingURL=cssParser.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cssParser.js","sourceRoot":"","sources":["../src/cssParser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAuC,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAC9F,OAAO,EAAE,uBAAuB,EAAE,MAAM,8BAA8B,CAAC;AAKvE,MAAM,oBAAoB,GAAG,IAAI,uBAAuB,EAAE,CAAC;AAW3D;;;;GAIG;AACH,MAAM,OAAO,SAAS;IACpB,KAAK,CAAQ;IACb,MAAM,CAAmB;IAEzB;;;OAGG;IACH,YACE,SAA0B,EAAE;QAE5B,MAAM,GAAG;YACP,oBAAoB;YACpB;;;;eAIG;YAC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