Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(AST): introduce YAML AST #108

Merged
merged 1 commit into from
Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions apidom/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apidom/packages/apidom-ast/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"stampit": "=4.3.1"
},
"devDependencies": {
"tree-sitter": "=0.16.2",
"tree-sitter": "=0.17.0",
"tree-sitter-json": "=0.16.0"
}
}
24 changes: 24 additions & 0 deletions apidom/packages/apidom-ast/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,30 @@ export {
isString as isJsonString,
isTrue as isJsonTrue,
} from './nodes/json/predicates';
// YAML AST related exports
export { default as YamlAlias } from './nodes/yaml/YamlAlias';
export { default as YamlCollection } from './nodes/yaml/YamlCollection';
export { default as YamlComment } from './nodes/yaml/YamlComment';
export { default as YamlDirective } from './nodes/yaml/YamlDirective';
export { default as YamlDocument } from './nodes/yaml/YamlDocument';
export { default as YamlKeyValuePair } from './nodes/yaml/YamlKeyValuePair';
export { default as YamlMapping } from './nodes/yaml/YamlMapping';
export { default as YamlNode } from './nodes/yaml/YamlNode';
export { default as YamlScalar } from './nodes/yaml/YamlScalar';
export { default as YamlSequence } from './nodes/yaml/YamlSequence';
export { default as YamlStream } from './nodes/yaml/YamlStream';
export { default as YamlTag } from './nodes/yaml/YamlTag';
export {
isAlias as isYamlAlias,
isKeyValuePair as isYamlKeyValuePair,
isDirective as isYamlDirective,
isDocument as isYamlDocument,
isMapping as isYamlMapping,
isScalar as isYamlScalar,
isSequence as isYamlSequence,
isStream as isYamlStream,
isTag as isYamlTag,
} from './nodes/yaml/predicates';
// generic AST related exports
export { default as Literal } from './Literal';
export { Point, default as Position } from './Position';
Expand Down
2 changes: 1 addition & 1 deletion apidom/packages/apidom-ast/src/nodes/json/JsonArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { anyPass } from 'ramda';
import JsonNode from './JsonNode';
import { isFalse, isTrue, isNull, isNumber, isString, isArray, isObject } from './predicates';

type JsonArray = Node;
type JsonArray = JsonNode;

const JsonArray: stampit.Stamp<JsonArray> = stampit(JsonNode, {
statics: {
Expand Down
2 changes: 1 addition & 1 deletion apidom/packages/apidom-ast/src/nodes/json/JsonDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { head } from 'ramda';

import JsonNode from './JsonNode';

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

Expand Down
3 changes: 2 additions & 1 deletion apidom/packages/apidom-ast/src/nodes/json/JsonObject.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import stampit from 'stampit';

import JsonNode from './JsonNode';
import JsonProperty from './JsonProperty';
import { isProperty } from './predicates';

type JsonObject = JsonNode;
Expand All @@ -10,7 +11,7 @@ const JsonObject: stampit.Stamp<JsonObject> = stampit(JsonNode, {
type: 'object',
},
methods: {
get properties(): unknown[] {
get properties(): Array<JsonProperty> {
// @ts-ignore
return this.children.filter(isProperty);
},
Expand Down
2 changes: 1 addition & 1 deletion apidom/packages/apidom-ast/src/nodes/json/JsonProperty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
isTrue,
} from './predicates';

interface JsonProperty extends Node {
interface JsonProperty extends JsonNode {
key: JsonKey;
value: unknown;
}
Expand Down
2 changes: 1 addition & 1 deletion apidom/packages/apidom-ast/src/nodes/json/JsonString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import JsonStringContent from './JsonStringContent';
import JsonEscapeSequence from './JsonEscapeSequence';
import { isEscapeSequence, isStringContent } from './predicates';

interface JsonString extends Node {
interface JsonString extends JsonNode {
value: string;
}

Expand Down
22 changes: 22 additions & 0 deletions apidom/packages/apidom-ast/src/nodes/yaml/YamlAlias.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import stampit from 'stampit';

import Node from '../../Node';

interface YamlAlias extends Node {
type: 'alias';
content: string | null;
}

const YamlAlias: stampit.Stamp<YamlAlias> = stampit(Node, {
statics: {
type: 'alias',
},
props: {
content: null,
},
init({ content = null } = {}) {
this.content = content;
},
});

export default YamlAlias;
11 changes: 11 additions & 0 deletions apidom/packages/apidom-ast/src/nodes/yaml/YamlCollection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import stampit from 'stampit';

import YamlNode from './YamlNode';

interface YamlCollection extends YamlNode {
readonly children: Array<unknown>;
}

const YamlCollection: stampit.Stamp<YamlCollection> = stampit(YamlNode, {});

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

import Node from '../../Node';

interface YamlComment extends Node {
type: 'comment';
content: string | null;
}

const YamlComment: stampit.Stamp<YamlComment> = stampit(Node, {
statics: {
type: 'comment',
},
props: {
content: null,
},
init({ content = null } = {}) {
this.content = content;
},
});

export default YamlComment;
39 changes: 39 additions & 0 deletions apidom/packages/apidom-ast/src/nodes/yaml/YamlDirective.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import stampit from 'stampit';
import { mergeRight } from 'ramda';

import Node from '../../Node';

interface YamlDirectiveParameters {
version: string | null;
handle: string | null;
prefix: string | null;
}

interface YamlDirective extends Node {
type: 'directive';
name: string | null;
parameters: YamlDirectiveParameters;
}

const YamlDirective: stampit.Stamp<YamlDirective> = stampit(Node, {
statics: {
type: 'directive',
},
props: {
name: null,
parameters: null,
},
init({ name = null, parameters = {} } = {}) {
this.name = name;
this.parameters = mergeRight(
{
version: null,
handle: null,
prefix: null,
},
parameters,
);
},
});

export default YamlDirective;
15 changes: 15 additions & 0 deletions apidom/packages/apidom-ast/src/nodes/yaml/YamlDocument.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import stampit from 'stampit';

import Node from '../../Node';

interface YamlDocument extends Node {
type: 'document';
}

const YamlDocument: stampit.Stamp<YamlDocument> = stampit(Node, {
statics: {
type: 'document',
},
});

export default YamlDocument;
14 changes: 14 additions & 0 deletions apidom/packages/apidom-ast/src/nodes/yaml/YamlKeyValuePair.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import stampit from 'stampit';
import YamlNode from './YamlNode';

interface YamlKeyValuePair extends YamlNode {
type: 'keyValuePair';
}

const YamlKeyValuePair: stampit.Stamp<YamlKeyValuePair> = stampit(YamlNode, {
statics: {
type: 'keyValuePair',
},
});

export default YamlKeyValuePair;
24 changes: 24 additions & 0 deletions apidom/packages/apidom-ast/src/nodes/yaml/YamlMapping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import stampit from 'stampit';
import { isArray } from 'ramda-adjunct';

import YamlCollection from './YamlCollection';
import { isKeyValuePair } from './predicates';
import YamlKeyValuePair from './YamlKeyValuePair';

interface YamlMapping extends YamlCollection {
type: 'mapping';
readonly content: Array<YamlKeyValuePair>;
}

const YamlMapping: stampit.Stamp<YamlMapping> = stampit(YamlCollection, {
statics: {
type: 'mapping',
},
methods: {
get content(): Array<YamlKeyValuePair> {
return isArray(this.children) ? this.children.filter(isKeyValuePair) : [];
},
},
});

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

import Node from '../../Node';

interface YamlNode extends Node {
content: unknown | null;
anchor: unknown | null;
tag: unknown | null;
}

const YamlNode: stampit.Stamp<YamlNode> = stampit(Node, {
props: {
content: null,
anchor: null,
tag: null,
},
init({ content = null, anchor = null, tag = null } = {}) {
this.content = content;
this.anchor = anchor;
this.tag = tag;
},
});

export default YamlNode;
16 changes: 16 additions & 0 deletions apidom/packages/apidom-ast/src/nodes/yaml/YamlScalar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import stampit from 'stampit';

import YamlNode from './YamlNode';

interface YamlScalar extends YamlNode {
type: 'scalar';
content: string | null;
}

const YamlScalar: stampit.Stamp<YamlScalar> = stampit(YamlNode, {
statics: {
type: 'scalar',
},
});

export default YamlScalar;
29 changes: 29 additions & 0 deletions apidom/packages/apidom-ast/src/nodes/yaml/YamlSequence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import stampit from 'stampit';
import { anyPass } from 'ramda';
import { isArray } from 'ramda-adjunct';

import YamlCollection from './YamlCollection';
import YamlMapping from './YamlMapping';
import YamlScalar from './YamlScalar';
import YamlAlias from './YamlAlias';
import { isMapping, isScalar, isSequence, isAlias } from './predicates';

interface YamlSequence extends YamlCollection {
type: 'sequence';
readonly content: Array<YamlSequence | YamlMapping | YamlScalar | YamlAlias>;
}

const YamlSequence: stampit.Stamp<YamlSequence> = stampit(YamlCollection, {
statics: {
type: 'sequence',
},
methods: {
get content(): Array<YamlSequence | YamlMapping | YamlScalar | YamlAlias> {
return isArray(this.children)
? this.children.filter(anyPass([isSequence, isMapping, isScalar, isAlias]))
: [];
},
},
});

export default YamlSequence;
17 changes: 17 additions & 0 deletions apidom/packages/apidom-ast/src/nodes/yaml/YamlStream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import stampit from 'stampit';

import Node from '../../Node';
import YamlDocument from './YamlDocument';

interface YamlStream extends Node {
type: 'stream';
children: Array<YamlDocument>;
}

const YamlStream: stampit.Stamp<YamlStream> = stampit(Node, {
statics: {
type: 'stream',
},
});

export default YamlStream;
31 changes: 31 additions & 0 deletions apidom/packages/apidom-ast/src/nodes/yaml/YamlTag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import stampit from 'stampit';

import Node from '../../Node';

enum YamlNodeKind {
Scalar = 'Scalar',
Sequence = 'Sequence',
Mapping = 'Mapping',
}

interface YamlTag {
type: 'tag';
name: string | null;
kind: YamlNodeKind | null;
}

const YamlTag: stampit.Stamp<YamlTag> = stampit(Node, {
statics: {
type: 'tag',
},
props: {
name: null,
kind: null,
},
init({ name = null, kind = null } = {}) {
this.name = name;
this.kind = kind;
},
});

export default YamlTag;
Loading