@flint.fyi/json-language 0.16.3 → 0.18.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.
@@ -0,0 +1,7 @@
1
+ import { AnyNode } from "@humanwhocodes/momoa";
2
+ import { CharacterReportRange } from "@flint.fyi/core";
3
+
4
+ //#region src/getNodeRange.d.ts
5
+ declare function getNodeRange(node: AnyNode): CharacterReportRange;
6
+ //#endregion
7
+ export { getNodeRange };
@@ -0,0 +1,9 @@
1
+ //#region src/getNodeRange.ts
2
+ function getNodeRange(node) {
3
+ return {
4
+ begin: node.range[0],
5
+ end: node.range[1]
6
+ };
7
+ }
8
+ //#endregion
9
+ export { getNodeRange };
@@ -0,0 +1,6 @@
1
+ import { ValueNode } from "@humanwhocodes/momoa";
2
+
3
+ //#region src/getNodeText.d.ts
4
+ declare function getNodeText(node: ValueNode, sourceText: string): string;
5
+ //#endregion
6
+ export { getNodeText };
@@ -0,0 +1,6 @@
1
+ //#region src/getNodeText.ts
2
+ function getNodeText(node, sourceText) {
3
+ return sourceText.slice(node.range[0], node.range[1]);
4
+ }
5
+ //#endregion
6
+ export { getNodeText };
package/lib/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { JsonMinusNumericLiteral, JsonNode, JsonNodeName, JsonNodeVisitors, JsonNodesByName, JsonObjectExpression, JsonObjectExpressionStatement, JsonSourceFile } from "./nodes.js";
2
- import { getJsonNodeRange } from "./getJsonNodeRange.js";
3
- import { JsonFileServices, jsonLanguage } from "./language.js";
4
- export { JsonFileServices, JsonMinusNumericLiteral, JsonNode, JsonNodeName, JsonNodeVisitors, JsonNodesByName, JsonObjectExpression, JsonObjectExpressionStatement, JsonSourceFile, getJsonNodeRange, jsonLanguage };
1
+ import { getNodeRange } from "./getNodeRange.js";
2
+ import { getNodeText } from "./getNodeText.js";
3
+ import { JsonVisitorKey } from "./nodes.js";
4
+ import { jsonLanguage } from "./language.js";
5
+ export { type JsonVisitorKey, getNodeRange, getNodeText, jsonLanguage };
package/lib/index.js CHANGED
@@ -1,3 +1,4 @@
1
- import { getJsonNodeRange } from "./getJsonNodeRange.js";
1
+ import { getNodeRange } from "./getNodeRange.js";
2
+ import { getNodeText } from "./getNodeText.js";
2
3
  import { jsonLanguage } from "./language.js";
3
- export { getJsonNodeRange, jsonLanguage };
4
+ export { getNodeRange, getNodeText, jsonLanguage };
package/lib/language.d.ts CHANGED
@@ -1,11 +1,12 @@
1
- import { JsonNodeVisitors, JsonSourceFile } from "./nodes.js";
1
+ import { JsonNodeVisitors } from "./nodes.js";
2
+ import { DocumentNode } from "@humanwhocodes/momoa";
2
3
  import { Language } from "@flint.fyi/core";
3
4
 
4
5
  //#region src/language.d.ts
5
6
  interface JsonFileServices {
6
- sourceFile: JsonSourceFile;
7
+ root: DocumentNode;
8
+ sourceText: string;
7
9
  }
8
10
  declare const jsonLanguage: Language<JsonNodeVisitors, JsonFileServices>;
9
11
  //#endregion
10
- export { JsonFileServices, jsonLanguage };
11
- //# sourceMappingURL=language.d.ts.map
12
+ export { jsonLanguage };
package/lib/language.js CHANGED
@@ -1,14 +1,19 @@
1
+ import { parse, traverse } from "@humanwhocodes/momoa";
1
2
  import { createLanguage } from "@flint.fyi/core";
2
- import * as ts from "typescript";
3
3
  //#region src/language.ts
4
- const kindOverrides = new Map([[ts.SyntaxKind.SourceFile, "JsonSourceFile"]]);
5
4
  const jsonLanguage = createLanguage({
6
5
  about: { name: "JSON" },
7
6
  createFileFactory: () => {
8
7
  return { createFile: (data) => {
9
8
  return {
10
9
  about: data,
11
- services: { sourceFile: ts.parseJsonText(data.filePathAbsolute, data.sourceText) }
10
+ services: {
11
+ root: parse(data.sourceText, {
12
+ mode: "json",
13
+ ranges: true
14
+ }),
15
+ sourceText: data.sourceText
16
+ }
12
17
  };
13
18
  } };
14
19
  },
@@ -19,16 +24,11 @@ const jsonLanguage = createLanguage({
19
24
  options,
20
25
  ...file.services
21
26
  };
22
- const visit = (node) => {
23
- const key = kindOverrides.get(node.kind) ?? ts.SyntaxKind[node.kind];
24
- visitors[key]?.(node, visitorServices);
25
- node.forEachChild(visit);
26
- visitors[`${key}:exit`]?.(node, visitorServices);
27
- };
28
- visit(file.services.sourceFile);
27
+ traverse(file.services.root, {
28
+ enter: (node) => visitors[node.type]?.(node, visitorServices),
29
+ exit: (node) => visitors[`${node.type}:exit`]?.(node, visitorServices)
30
+ });
29
31
  }
30
32
  });
31
33
  //#endregion
32
34
  export { jsonLanguage };
33
-
34
- //# sourceMappingURL=language.js.map
package/lib/nodes.d.ts CHANGED
@@ -1,33 +1,9 @@
1
+ import { AnyNode } from "@humanwhocodes/momoa";
1
2
  import { WithExitKeys } from "@flint.fyi/core";
2
- import * as ts from "typescript";
3
- import { AST } from "@flint.fyi/typescript-language";
4
3
 
5
4
  //#region src/nodes.d.ts
6
- type JsonMinusNumericLiteral = Omit<AST.PrefixUnaryExpression, "operand" | "operator"> & {
7
- readonly operand: AST.NumericLiteral;
8
- readonly operator: ts.SyntaxKind.MinusToken;
9
- };
10
- type JsonNode = JsonNodesByName[JsonNodeName];
11
- type JsonNodeName = keyof JsonNodesByName;
12
- interface JsonNodesByName {
13
- ArrayLiteralExpression: AST.ArrayLiteralExpression;
14
- BooleanLiteral: AST.BooleanLiteral;
15
- JsonMinusNumericLiteral: JsonMinusNumericLiteral;
16
- JsonObjectExpressionStatement: JsonObjectExpressionStatement;
17
- JsonSourceFile: JsonSourceFile;
18
- NullLiteral: AST.NullLiteral;
19
- NumericLiteral: AST.NumericLiteral;
20
- ObjectLiteralExpression: AST.ObjectLiteralExpression;
21
- StringLiteral: AST.StringLiteral;
22
- }
5
+ type JsonNodesByName = { [Node in AnyNode as Node["type"]]: Node };
23
6
  type JsonNodeVisitors = WithExitKeys<JsonNodesByName>;
24
- type JsonObjectExpression = AST.ArrayLiteralExpression | AST.BooleanLiteral | AST.NullLiteral | AST.NumericLiteral | AST.ObjectLiteralExpression | AST.StringLiteral | JsonMinusNumericLiteral;
25
- type JsonObjectExpressionStatement = Omit<AST.ExpressionStatement, "expression"> & {
26
- readonly expression: JsonObjectExpression;
27
- };
28
- type JsonSourceFile = Omit<AST.SourceFile, "statements"> & {
29
- readonly statements: ts.NodeArray<JsonObjectExpressionStatement>;
30
- };
7
+ type JsonVisitorKey = AnyNode["type"];
31
8
  //#endregion
32
- export { JsonMinusNumericLiteral, JsonNode, JsonNodeName, JsonNodeVisitors, JsonNodesByName, JsonObjectExpression, JsonObjectExpressionStatement, JsonSourceFile };
33
- //# sourceMappingURL=nodes.d.ts.map
9
+ export { JsonNodeVisitors, JsonVisitorKey };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flint.fyi/json-language",
3
- "version": "0.16.3",
3
+ "version": "0.18.0",
4
4
  "description": "[Experimental] JSON language for Flint.",
5
5
  "homepage": "https://flint.fyi",
6
6
  "repository": {
@@ -19,16 +19,14 @@
19
19
  ".": "./lib/index.js"
20
20
  },
21
21
  "files": [
22
- "lib/",
23
- "!lib/**/*.map"
22
+ "lib/"
24
23
  ],
25
24
  "dependencies": {
26
- "typescript": "^5.9.0 || ^6.0.0",
27
- "@flint.fyi/core": "^0.22.0",
28
- "@flint.fyi/typescript-language": "^0.18.2"
25
+ "@humanwhocodes/momoa": "^3.3.10",
26
+ "@flint.fyi/core": "^0.23.1"
29
27
  },
30
28
  "devDependencies": {
31
- "tsdown": "0.21.0",
29
+ "tsdown": "0.22.2",
32
30
  "vitest": "4.1.0"
33
31
  },
34
32
  "engines": {
@@ -1,8 +0,0 @@
1
- import { JsonNode, JsonSourceFile } from "./nodes.js";
2
- import { CharacterReportRange } from "@flint.fyi/core";
3
-
4
- //#region src/getJsonNodeRange.d.ts
5
- declare function getJsonNodeRange(node: JsonNode, sourceFile: JsonSourceFile): CharacterReportRange;
6
- //#endregion
7
- export { getJsonNodeRange };
8
- //# sourceMappingURL=getJsonNodeRange.d.ts.map
@@ -1,11 +0,0 @@
1
- //#region src/getJsonNodeRange.ts
2
- function getJsonNodeRange(node, sourceFile) {
3
- return {
4
- begin: node.getStart(sourceFile),
5
- end: node.getEnd()
6
- };
7
- }
8
- //#endregion
9
- export { getJsonNodeRange };
10
-
11
- //# sourceMappingURL=getJsonNodeRange.js.map