mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2025-10-16 12:05:41 +00:00
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:
parent
8eaac71fb2
commit
534f9b6fb6
3
types/json-to-ast/.editorconfig
Normal file
3
types/json-to-ast/.editorconfig
Normal file
@ -0,0 +1,3 @@
|
||||
# This package uses tabs
|
||||
[*]
|
||||
indent_style = tab
|
||||
74
types/json-to-ast/index.d.ts
vendored
Normal file
74
types/json-to-ast/index.d.ts
vendored
Normal 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 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;
|
||||
49
types/json-to-ast/test/json-to-ast.test.ts
Normal file
49
types/json-to-ast/test/json-to-ast.test.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
44
types/json-to-ast/test/json-to-ast.umd-global.test.ts
Normal file
44
types/json-to-ast/test/json-to-ast.umd-global.test.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
20
types/json-to-ast/tsconfig.json
Normal file
20
types/json-to-ast/tsconfig.json
Normal 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"
|
||||
]
|
||||
}
|
||||
6
types/json-to-ast/tslint.json
Normal file
6
types/json-to-ast/tslint.json
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"extends": "dtslint/dt.json",
|
||||
"rules": {
|
||||
"indent": [true, "tabs"]
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user