feat: Add json-to-ast (#35818)

* feat: Add `json-to-ast`

* test(json‑to‑ast): Fix UMD global test

* fix(json‑to‑ast): Remove `strict` compiler option

* test(json‑to‑ast): Move type assertions at the end of the line

* refactor(json‑to‑ast): Apply Prettier formatting
This commit is contained in:
ExE Boss 2019-05-30 23:31:15 +02:00 committed by Sheetal Nandi
parent 8eaac71fb2
commit 534f9b6fb6
6 changed files with 196 additions and 0 deletions

View File

@ -0,0 +1,3 @@
# This package uses tabs
[*]
indent_style = tab

74
types/json-to-ast/index.d.ts vendored Normal file
View File

@ -0,0 +1,74 @@
// Type definitions for json-to-ast 2.1
// Project: https://github.com/vtrushin/json-to-ast
// Definitions by: ExE Boss <https://github.com/ExE-Boss>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare namespace parse {
interface Options {
/**
* Appends location information.
*
* @default true
*/
loc?: boolean;
/**
* Appends source information to nodes location.
*
* @default null
*/
source?: string;
}
type ValueNode = ObjectNode | ArrayNode | LiteralNode;
interface Position {
line: number;
column: number;
offset: number;
}
interface Location {
start: Position;
end: Position;
source: string | null;
}
interface ASTNode {
type: string;
loc?: Location;
}
interface ObjectNode extends ASTNode {
type: "Object";
children: PropertyNode[];
}
interface PropertyNode extends ASTNode {
type: "Property";
key: IdentifierNode;
value: ValueNode;
}
interface IdentifierNode extends ASTNode {
type: "Identifier";
value: string;
raw: string;
}
interface ArrayNode extends ASTNode {
type: "Array";
children: ValueNode[];
}
interface LiteralNode extends ASTNode {
type: "Literal";
value: string | number | boolean | null;
raw: string;
}
}
declare function parse(json: string, settings?: parse.Options): parse.ValueNode;
export = parse;
export as namespace jsonToAst;

View File

@ -0,0 +1,49 @@
import parse = require("json-to-ast");
// $ExpectError
jsonToAst;
// $ExpectType ValueNode
const ast = parse('{"a": 1}');
processValueNode(ast);
function logPos(node: parse.ASTNode) {
const location = node.loc; // $ExpectType Location | undefined
if (location != null) {
location.start; // $ExpectType Position
location.end; // $ExpectType Position
location.source; // $ExpectType string | null
}
}
function processValueNode(ast: parse.ValueNode) {
logPos(ast);
switch (ast.type) {
case "Object":
ast; // $ExpectType ObjectNode
ast.children.forEach(child => {
child; // $ExpectType PropertyNode
child.key; // $ExpectType IdentifierNode
processValueNode(child.value);
});
break;
case "Array":
ast; // $ExpectType ArrayNode
ast.children.forEach(child => {
child; // $ExpectType ValueNode
processValueNode(child);
});
break;
case "Literal":
ast; // $ExpectType LiteralNode
ast.value; // $ExpectType string | number | boolean | null
ast.raw; // $ExpectType string
break;
}
}

View File

@ -0,0 +1,44 @@
// $ExpectType ValueNode
const ast = jsonToAst('{"a": 1}');
processValueNode(ast);
function logPos(node: jsonToAst.ASTNode) {
const location = node.loc; // $ExpectType Location | undefined
if (location != null) {
location.start; // $ExpectType Position
location.end; // $ExpectType Position
location.source; // $ExpectType string | null
}
}
function processValueNode(ast: jsonToAst.ValueNode) {
logPos(ast);
switch (ast.type) {
case "Object":
ast; // $ExpectType ObjectNode
ast.children.forEach(child => {
child; // $ExpectType PropertyNode
child.key; // $ExpectType IdentifierNode
processValueNode(child.value);
});
break;
case "Array":
ast; // $ExpectType ArrayNode
ast.children.forEach(child => {
child; // $ExpectType ValueNode
processValueNode(child);
});
break;
case "Literal":
ast; // $ExpectType LiteralNode
ast.value; // $ExpectType string | number | boolean | null
ast.raw; // $ExpectType string
break;
}
}

View File

@ -0,0 +1,20 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es5"],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": ["../"],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"test/json-to-ast.test.ts",
"test/json-to-ast.umd-global.test.ts"
]
}

View File

@ -0,0 +1,6 @@
{
"extends": "dtslint/dt.json",
"rules": {
"indent": [true, "tabs"]
}
}