From 534f9b6fb6e87497d6e416dd34014df7e1af4f5c Mon Sep 17 00:00:00 2001 From: ExE Boss <3889017+ExE-Boss@users.noreply.github.com> Date: Thu, 30 May 2019 23:31:15 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20Add=C2=A0`json-to-ast`=20(#35818)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- types/json-to-ast/.editorconfig | 3 + types/json-to-ast/index.d.ts | 74 +++++++++++++++++++ types/json-to-ast/test/json-to-ast.test.ts | 49 ++++++++++++ .../test/json-to-ast.umd-global.test.ts | 44 +++++++++++ types/json-to-ast/tsconfig.json | 20 +++++ types/json-to-ast/tslint.json | 6 ++ 6 files changed, 196 insertions(+) create mode 100644 types/json-to-ast/.editorconfig create mode 100644 types/json-to-ast/index.d.ts create mode 100644 types/json-to-ast/test/json-to-ast.test.ts create mode 100644 types/json-to-ast/test/json-to-ast.umd-global.test.ts create mode 100644 types/json-to-ast/tsconfig.json create mode 100644 types/json-to-ast/tslint.json diff --git a/types/json-to-ast/.editorconfig b/types/json-to-ast/.editorconfig new file mode 100644 index 0000000000..a0207fa43b --- /dev/null +++ b/types/json-to-ast/.editorconfig @@ -0,0 +1,3 @@ +# This package uses tabs +[*] +indent_style = tab diff --git a/types/json-to-ast/index.d.ts b/types/json-to-ast/index.d.ts new file mode 100644 index 0000000000..949b359973 --- /dev/null +++ b/types/json-to-ast/index.d.ts @@ -0,0 +1,74 @@ +// Type definitions for json-to-ast 2.1 +// Project: https://github.com/vtrushin/json-to-ast +// Definitions by: ExE Boss +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare namespace parse { + interface Options { + /** + * Appends location information. + * + * @default true + */ + loc?: boolean; + + /** + * Appends source information to node’s 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; diff --git a/types/json-to-ast/test/json-to-ast.test.ts b/types/json-to-ast/test/json-to-ast.test.ts new file mode 100644 index 0000000000..a8314eac86 --- /dev/null +++ b/types/json-to-ast/test/json-to-ast.test.ts @@ -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; + } +} diff --git a/types/json-to-ast/test/json-to-ast.umd-global.test.ts b/types/json-to-ast/test/json-to-ast.umd-global.test.ts new file mode 100644 index 0000000000..3b35c6f4dc --- /dev/null +++ b/types/json-to-ast/test/json-to-ast.umd-global.test.ts @@ -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; + } +} diff --git a/types/json-to-ast/tsconfig.json b/types/json-to-ast/tsconfig.json new file mode 100644 index 0000000000..aa1bc0633a --- /dev/null +++ b/types/json-to-ast/tsconfig.json @@ -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" + ] +} diff --git a/types/json-to-ast/tslint.json b/types/json-to-ast/tslint.json new file mode 100644 index 0000000000..ffaae9bd46 --- /dev/null +++ b/types/json-to-ast/tslint.json @@ -0,0 +1,6 @@ +{ + "extends": "dtslint/dt.json", + "rules": { + "indent": [true, "tabs"] + } +}