@aljazmc/syntax 0.6.1 → 0.7.0

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/README.md CHANGED
@@ -1,68 +1,49 @@
1
1
  # > syntax
2
2
 
3
3
  Shared, dependency-free scanner and recursive descent parser for CSS,
4
- HTML, JavaScript, JSON, JSX, Markdown and TypeScript, bundled into `@aljazmc/formatero` and `@aljazmc/lintero`, and published for their
5
- types.
4
+ HTML, JavaScript, JSON, JSX, Markdown and TypeScript, bundled into
5
+ `@aljazmc/formatero` and `@aljazmc/lintero` and published for their
6
+ types. Invalid sources throw a `ParseError` with the position of the
7
+ problem.
8
+
9
+ ## > Features
10
+
11
+ | Language | Entry points | Tree |
12
+ | --------------------------- | ----------------------------- | ---------------------------------------------------------------------------------- |
13
+ | CSS | `isCss`, `parseCss` | Rules, at-rules, declarations, values and selectors, with comments. |
14
+ | HTML | `isHtml`, `parseHtml` | Elements, attributes and text, comments and whitespace as trivia. |
15
+ | JavaScript, JSX, TypeScript | `language`, `parse` | ESTree, with Babel-style literals and TypeScript nodes. |
16
+ | JSON | `language`, `parse` | ESTree expressions. |
17
+ | Markdown | `isMarkdown`, `parseMarkdown` | CommonMark blocks and inlines, with GitHub's tables, task lists and strikethrough. |
18
+ | YAML | `isYaml`, `parseYaml` | Documents of block and flow collections and scalars, with comments in place. |
19
+
20
+ | Helper | Description |
21
+ | -------------------------------- | ----------------------------------------------------------- |
22
+ | `Lines` | Turns offsets into 1-based lines and columns, for messages. |
23
+ | `parseCss(…, { recover: true })` | Skips items with errors and lists them in `errors`. |
24
+ | `ParseError` | Thrown for invalid sources, with the position. |
25
+ | `tagOf` | The known tag of an element name, like `div` for `DIV`. |
26
+ | `Walker` | Lists the child nodes of a node in source order. |
27
+
28
+ ## > Usage
6
29
 
7
30
  ```ts
8
- import { language, parse } from "@aljazmc/syntax";
9
-
10
- const options = language("a.tsx"); // { json: false, typescript: true, jsx: true }
11
- const { program, comments } = parse("let a = <b />;", options);
12
- ```
13
-
14
- Syntax trees follow ESTree, with Babel-style literals and TypeScript nodes.
15
- Invalid sources throw `ParseError` with the position of the problem.
16
-
17
- `Walker.children(node)` lists the child nodes of a node in source order, and
18
- `Lines` turns offsets into 1-based lines and columns, for messages.
19
-
20
- Markdown is parsed like CommonMark, with GitHub's tables, task list items
21
- and strikethrough:
22
-
23
- ```ts
24
- import { isMarkdown, parseMarkdown } from "@aljazmc/syntax";
25
-
26
- isMarkdown("README.md"); // true
27
- const root = parseMarkdown("# Title\n\n- [x] done\n");
28
- // root.children: a Heading and a List, with their offsets and lines
31
+ import {
32
+ language,
33
+ parse,
34
+ parseCss,
35
+ parseHtml,
36
+ parseMarkdown,
37
+ parseYaml,
38
+ } from "@aljazmc/syntax";
39
+
40
+ const { program, comments } = parse("let a = <b />;", language("a.tsx"));
41
+ const sheet = parseCss("a > .b:hover { color: red; }\n");
42
+ const page = parseHtml('<p class="a">Hi <b>there</b></p>\n');
43
+ const doc = parseMarkdown("# Title\n\n- [x] done\n");
44
+ const stream = parseYaml("a:\n - b: 1 # c\n");
29
45
  ```
30
46
 
31
- Blocks have offsets into the source and 1-based lines, and keep what a
32
- formatter needs to print them as written, like the indentation of each
33
- line. Inlines have offsets into the text of their block.
34
-
35
- HTML is parsed like Biome parses it: elements must be closed, only void
36
- elements may have no closing tag, and whitespace and comments are trivia
37
- between nodes, not children:
38
-
39
- ```ts
40
- import { isHtml, parseHtml } from "@aljazmc/syntax";
41
-
42
- isHtml("index.html"); // true
43
- const root = parseHtml('<p class="a">Hi <b>there</b></p>\n');
44
- // root.children: an Element p, with its attributes, a Text and an Element b
45
- // root.comments: every <!-- --> comment
46
- ```
47
-
48
- Element names keep their case, and `tag` is the known tag they match, like
49
- `div` for `DIV` and `feBlend` for `feBlend`. Scripts, styles and
50
- preformatted elements keep their content as one raw text.
51
-
52
- CSS is parsed into rules, at-rules, declarations and their component
53
- values, and the selectors of rules into compound selectors, combinators
54
- and simple selectors, like Biome reads them:
55
-
56
- ```ts
57
- import { isCss, parseCss } from "@aljazmc/syntax";
58
-
59
- isCss("styles.css"); // true
60
- const sheet = parseCss("a > .b:hover { color: red !important; }\n");
61
- // sheet.children: a Rule, its selectors and a block with a Declaration
62
- // sheet.comments: every /* */ comment
63
- ```
47
+ ## > License
64
48
 
65
- Nested rules, `@media` queries and other at-rule preludes keep their
66
- values, keyframes keep theirs instead of selectors. Errors throw a
67
- `ParseError`, or with `parseCss(source, { recover: true })` the items
68
- with errors are skipped and listed in `sheet.errors`.
49
+ MIT, see [LICENSE](LICENSE).