@aljazmc/syntax 0.7.1 → 0.7.3

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,12 +1,30 @@
1
- # > syntax
1
+ # @aljazmc/syntax
2
2
 
3
- Shared, dependency-free scanner and recursive descent parser for CSS,
4
- HTML, JavaScript, JSON, JSX, Markdown, TypeScript and YAML, 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.
3
+ [![CI/CD](https://github.com/aljazmc/aljazmc/actions/workflows/cicd.yml/badge.svg)](https://github.com/aljazmc/aljazmc/actions/workflows/cicd.yml)
4
+ [![npm](https://img.shields.io/npm/v/@aljazmc/syntax?logo=npm&color=cb3837)](https://www.npmjs.com/package/@aljazmc/syntax)
5
+ [![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
6
+ [![Dependencies: none](https://img.shields.io/badge/dependencies-none-brightgreen)](package.json)
7
+ [![ESTree](https://img.shields.io/badge/AST-ESTree-f7df1e?logo=javascript&logoColor=black)](https://github.com/estree/estree)
8
+ [![CommonMark + GFM](https://img.shields.io/badge/Markdown-CommonMark%20%2B%20GFM-000000?logo=markdown&logoColor=white)](https://github.github.com/gfm/)
9
+ [![Bundled](https://img.shields.io/badge/bundled%20into-formatero%20·%20lintero-6e7781)](../../README.md#packages)
10
+ [![Tests](https://img.shields.io/badge/tests-31%20passing-brightgreen)](#tests)
11
+ [![TypeScript 7](https://img.shields.io/badge/TypeScript-7%20strict-3178c6?logo=typescript&logoColor=white)](https://www.typescriptlang.org/)
8
12
 
9
- ## > Features
13
+ The scanner and recursive descent parsers behind formatero and lintero,
14
+ for CSS, HTML, JavaScript, JSON, JSX, Markdown, TypeScript and YAML, in
15
+ plain TypeScript. esbuild bundles this code into both packages; it is
16
+ published on npm only for its types.
17
+
18
+ ## Features
19
+
20
+ - **Eight languages, one package.** Every parser is hand-written, with no
21
+ generated grammars and no dependencies.
22
+ - **ESTree trees.** JavaScript, JSX, TypeScript and JSON parse to ESTree,
23
+ with Babel-style literals and TypeScript nodes.
24
+ - **Comments kept.** Every parser keeps the comments, and HTML its
25
+ whitespace, as a formatter needs them.
26
+ - **Precise errors.** Invalid sources throw a `ParseError` with the line
27
+ and column of the problem; CSS can recover and list them instead.
10
28
 
11
29
  | Language | Entry points | Tree |
12
30
  | --------------------------- | ----------------------------- | ---------------------------------------------------------------------------------- |
@@ -25,7 +43,7 @@ problem.
25
43
  | `tagOf` | The known tag of an element name, like `div` for `DIV`. |
26
44
  | `Walker` | Lists the child nodes of a node in source order. |
27
45
 
28
- ## > Usage
46
+ ## Usage
29
47
 
30
48
  ```ts
31
49
  import {
@@ -44,6 +62,21 @@ const doc = parseMarkdown("# Title\n\n- [x] done\n");
44
62
  const stream = parseYaml("a:\n - b: 1 # c\n");
45
63
  ```
46
64
 
47
- ## > License
65
+ ## Tests
66
+
67
+ ```sh
68
+ docker compose run --rm node yarn t:syntax
69
+ ```
70
+
71
+ 31 tests, run by [testero](../../packages/testero), each checking many
72
+ sources: JavaScript, JSX and TypeScript, Markdown blocks and inlines by
73
+ CommonMark, HTML, CSS and YAML, and regressions. formatero and lintero
74
+ test the parser further, through 1,898 cases of their own that compare
75
+ with Biome.
76
+
77
+ A change here is bundled into formatero and lintero, so run their tests
78
+ too: `yarn t:formatero` and `yarn t:lintero`.
79
+
80
+ ## License
48
81
 
49
82
  MIT, see [LICENSE](LICENSE).
package/bin/index.js CHANGED
@@ -3727,6 +3727,8 @@ var BaseParser = class {
3727
3727
  previous = 0;
3728
3728
  /** Whether `in` is a binary operator here (not in a for-in head). */
3729
3729
  allowIn = true;
3730
+ /** Lines and columns of the source, made by the first error. */
3731
+ #lines;
3730
3732
  constructor(source, options) {
3731
3733
  this.source = source;
3732
3734
  this.options = options;
@@ -3819,7 +3821,8 @@ var BaseParser = class {
3819
3821
  );
3820
3822
  }
3821
3823
  raise(message, position) {
3822
- const lines = new Lines(this.source);
3824
+ this.#lines ??= new Lines(this.source);
3825
+ const lines = this.#lines;
3823
3826
  throw new ParseError(
3824
3827
  `${message} (${lines.line(position)}:${lines.column(position)})`,
3825
3828
  position
@@ -7381,6 +7384,7 @@ var YamlParser = class {
7381
7384
  };
7382
7385
 
7383
7386
  // shared/syntax/src/walker.ts
7387
+ var compare = /* @__PURE__ */ __name((a, b) => a.start - b.start || b.end - a.end, "compare");
7384
7388
  var Walker = class _Walker {
7385
7389
  static {
7386
7390
  __name(this, "Walker");
@@ -7391,16 +7395,20 @@ var Walker = class _Walker {
7391
7395
  /** Child nodes by start, outer nodes before inner ones starting with them. */
7392
7396
  static children(node) {
7393
7397
  const children = [];
7394
- for (const [key, value] of Object.entries(node)) {
7398
+ const shorthand = node.type === "Property" && node.shorthand;
7399
+ for (const key of Object.keys(node)) {
7395
7400
  if (key === "comments") continue;
7396
- if (node.type === "Property" && node.shorthand && key === "key")
7397
- continue;
7401
+ if (shorthand && key === "key") continue;
7402
+ const value = node[key];
7398
7403
  if (Array.isArray(value)) {
7399
7404
  for (const item of value)
7400
7405
  if (_Walker.isNode(item)) children.push(item);
7401
7406
  } else if (_Walker.isNode(value)) children.push(value);
7402
7407
  }
7403
- return children.sort((a, b) => a.start - b.start || b.end - a.end);
7408
+ for (let index = 1; index < children.length; index++)
7409
+ if (compare(children[index - 1], children[index]) > 0)
7410
+ return children.sort(compare);
7411
+ return children;
7404
7412
  }
7405
7413
  };
7406
7414
 
@@ -11,6 +11,7 @@ interface Snapshot {
11
11
  }
12
12
  type Fields<T extends NodeType> = Omit<NodeOf<T>, "end" | "start" | "type">;
13
13
  export default abstract class BaseParser {
14
+ #private;
14
15
  protected readonly source: string;
15
16
  protected readonly scanner: Scanner;
16
17
  protected readonly options: Options;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aljazmc/syntax",
3
- "version": "0.7.1",
3
+ "version": "0.7.3",
4
4
  "description": "Shared, dependency-free parser for CSS, HTML, JavaScript, JSON, JSX, Markdown, TypeScript and YAML, bundled into @aljazmc/formatero and @aljazmc/lintero.",
5
5
  "keywords": [
6
6
  "parser",