Skip to content

Commit

Permalink
feat(ast): make errors and missing nodes part of AST
Browse files Browse the repository at this point in the history
Refs #35
  • Loading branch information
char0n committed Aug 6, 2020
1 parent 5dae36e commit 6142fab
Show file tree
Hide file tree
Showing 28 changed files with 359 additions and 314 deletions.
23 changes: 23 additions & 0 deletions apidom/packages/apidom-ast/src/Error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import stampit from 'stampit';

import Node from './Node';

interface Error extends Node {
value: unknown;
}

const Error: stampit.Stamp<Error> = stampit(Node, {
statics: {
type: 'error',
},
props: {
value: null,
isUnexpected: false,
},
init({ value = null, isUnexpected = false } = {}) {
this.value = value;
this.isUnexpected = isUnexpected;
},
});

export default Error;
22 changes: 22 additions & 0 deletions apidom/packages/apidom-ast/src/Literal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import stampit from 'stampit';

import Node from './Node';

interface Literal extends Node {
type: 'literal';
value: unknown;
}

const Literal: stampit.Stamp<Literal> = stampit(Node, {
statics: {
type: 'literal',
},
props: {
value: null,
},
init({ value = null } = {}) {
this.value = value;
},
});

export default Literal;
19 changes: 0 additions & 19 deletions apidom/packages/apidom-ast/src/Missing.ts

This file was deleted.

24 changes: 24 additions & 0 deletions apidom/packages/apidom-ast/src/Node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import stampit from 'stampit';

interface Node {
type: string | null;
isMissing: boolean;
children: unknown[];
position: Position | null;
}

const Node: stampit.Stamp<Node> = stampit({
props: {
type: null,
position: null,
children: [],
},
init({ children = [], position = null, isMissing = false } = {}, { stamp }) {
this.type = stamp.type;
this.isMissing = isMissing;
this.children = children;
this.position = position;
},
});

export default Node;
24 changes: 14 additions & 10 deletions apidom/packages/apidom-ast/src/ParseResult.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
import stampit from 'stampit';
import { head } from 'ramda';

interface ParseResult {
import Node from './Node';

interface ParseResult extends Node {
type: 'parseResult';
errors: unknown[];
annotations: unknown[];
rootNode: unknown;
}

const ParseResult: stampit.Stamp<ParseResult> = stampit({
props: {
const ParseResult: stampit.Stamp<ParseResult> = stampit(Node, {
statics: {
type: 'parseResult',
errors: [],
annotations: [],
rootNode: null,
},
init({ rootNode = null, errors = [], annotations = [] } = {}) {
this.rootNode = rootNode;
this.errors = errors;
this.annotations = annotations;
methods: {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
get rootNode(): unknown {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return head(this.children);
},
},
});

Expand Down
13 changes: 10 additions & 3 deletions apidom/packages/apidom-ast/src/Position.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import stampit from 'stampit';

interface Point {
type: 'point';
row: number | null;
column: number | null;
char: number | null;
}

interface Position {
interface Position extends Node {
type: 'position';
startPosition: Point | null;
endPosition: Point | null;
start: Point | null;
end: Point | null;
}

export const Point: stampit.Stamp<Point> = stampit({
statics: {
type: 'point',
},
props: {
type: 'point',
row: null,
Expand All @@ -27,6 +31,9 @@ export const Point: stampit.Stamp<Point> = stampit({
});

const Position: stampit.Stamp<Position> = stampit({
statics: {
type: 'position',
},
props: {
type: 'position',
start: null,
Expand Down
7 changes: 3 additions & 4 deletions apidom/packages/apidom-ast/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ export { default as JsonProperty } from './nodes/json/JsonProperty';
export { default as JsonArray } from './nodes/json/JsonArray';
export { default as JsonValue } from './nodes/json/JsonValue';
export { default as JsonKey } from './nodes/json/JsonKey';
export { default as JsonComment } from './nodes/json/JsonComment';
export { default as JsonString } from './nodes/json/JsonString';
export { default as JsonStringContent } from './nodes/json/JsonStringContent';
export { default as JsonEscapeSequence } from './nodes/json/JsonEscapeSequence';
export { default as JsonNumber } from './nodes/json/JsonNumber';
export { default as JsonTrue } from './nodes/json/JsonTrue';
export { default as JsonFalse } from './nodes/json/JsonFalse';
export { default as JsonNull } from './nodes/json/JsonNull';
export { default as Position } from './Position';
export { Point } from './Position';
export { default as Missing } from './Missing';
export { default as Literal } from './Literal';
export { Point, default as Position } from './Position';
export { default as Error } from './Error';
export { default as ParseResult } from './ParseResult';
33 changes: 8 additions & 25 deletions apidom/packages/apidom-ast/src/nodes/json/JsonArray.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,21 @@
import stampit from 'stampit';
import { anyPass, propEq } from 'ramda';
import { anyPass } from 'ramda';

import JsonNode from './traits/JsonNode';
import JsonComments from './traits/JsonComments';
import NodeType from './node-type';
import Node from '../../Node';
import { isFalse, isTrue, isNull, isNumber, isString, isArray, isObject } from './predicates';

interface JsonArray extends JsonNode, JsonComments {
children: unknown[];
}
type JsonArray = Node;

const JsonArray: stampit.Stamp<JsonArray> = stampit(JsonNode, JsonComments, {
props: {
children: [],
},
init({ children = [], position = null } = {}) {
this.type = NodeType.Array;
this.children = children;
this.position = position;
const JsonArray: stampit.Stamp<JsonArray> = stampit(Node, {
statics: {
type: 'array',
},
methods: {
get items(): unknown[] {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return this.children.filter(
anyPass([
propEq('type', NodeType.Comment),
propEq('type', NodeType.False),
propEq('type', NodeType.True),
propEq('type', NodeType.Null),
propEq('type', NodeType.Number),
propEq('type', NodeType.String),
propEq('type', NodeType.Array),
propEq('type', NodeType.Object),
]),
anyPass([isFalse, isTrue, isNull, isNumber, isString, isArray, isObject]),
);
},
},
Expand Down
14 changes: 0 additions & 14 deletions apidom/packages/apidom-ast/src/nodes/json/JsonComment.ts

This file was deleted.

27 changes: 15 additions & 12 deletions apidom/packages/apidom-ast/src/nodes/json/JsonDocument.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import stampit from 'stampit';
import { head } from 'ramda';

import JsonNode from './traits/JsonNode';
import JsonComments from './traits/JsonComments';
import NodeType from './node-type';
import Node from '../../Node';

interface JsonDocument extends JsonNode, JsonComments {
child: JsonNode | null;
interface JsonDocument extends Node {
child: unknown | null;
}

const JsonDocument: stampit.Stamp<JsonDocument> = stampit(JsonNode, JsonComments, {
props: {
child: null,
const JsonDocument: stampit.Stamp<JsonDocument> = stampit(Node, {
statics: {
type: 'document',
},
init({ child = null, position = null } = {}) {
this.type = NodeType.Document;
this.child = child;
this.position = position;
methods: {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
get child(): unknown {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return head(this.children);
},
},
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import stampit from 'stampit';

import JsonValue from './JsonValue';
import NodeType from './node-type';

type JsonEscapeSequence = JsonValue;

const JsonEscapeSequence: stampit.Stamp<JsonEscapeSequence> = stampit(JsonValue, {
init() {
this.type = NodeType.EscapeSequence;
statics: {
type: 'escapeSequence',
},
});

Expand Down
5 changes: 2 additions & 3 deletions apidom/packages/apidom-ast/src/nodes/json/JsonFalse.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import stampit from 'stampit';

import JsonValue from './JsonValue';
import NodeType from './node-type';

type JsonFalse = JsonValue;

const JsonFalse: stampit.Stamp<JsonFalse> = stampit(JsonValue, {
init() {
this.type = NodeType.False;
statics: {
type: 'false',
},
});

Expand Down
5 changes: 2 additions & 3 deletions apidom/packages/apidom-ast/src/nodes/json/JsonKey.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import stampit from 'stampit';

import JsonValue from './JsonValue';
import NodeType from './node-type';

type JsonKey = JsonValue;

const JsonKey: stampit.Stamp<JsonKey> = stampit(JsonValue, {
init() {
this.type = NodeType.Key;
statics: {
type: 'key',
},
});

Expand Down
5 changes: 2 additions & 3 deletions apidom/packages/apidom-ast/src/nodes/json/JsonNull.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import stampit from 'stampit';

import JsonValue from './JsonValue';
import NodeType from './node-type';

type JsonNull = JsonValue;

const JsonNull: stampit.Stamp<JsonNull> = stampit(JsonValue, {
init() {
this.type = NodeType.Null;
statics: {
type: 'null',
},
});

Expand Down
5 changes: 2 additions & 3 deletions apidom/packages/apidom-ast/src/nodes/json/JsonNumber.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import stampit from 'stampit';

import JsonValue from './JsonValue';
import NodeType from './node-type';

type JsonNumber = JsonValue;

const JsonNumber: stampit.Stamp<JsonNumber> = stampit(JsonValue, {
init() {
this.type = NodeType.Number;
statics: {
type: 'number',
},
});

Expand Down
Loading

0 comments on commit 6142fab

Please sign in to comment.