Skip to content

Commit

Permalink
refactor(ast): followup changes for YAML 1.2 Nodes
Browse files Browse the repository at this point in the history
Refs #3481
  • Loading branch information
char0n committed Jan 17, 2024
1 parent 34b18cb commit 1247078
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 90 deletions.
15 changes: 10 additions & 5 deletions packages/apidom-ast/src/predicates.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
export const isNodeType = (type: string, node: any): boolean => node?.type === type;
import type Literal from './Literal';
import Position, { Point } from './Position';
import ParseResult from './ParseResult';

export const isLiteral = isNodeType.bind(undefined, 'literal');
export const isNodeType = (type: string, node: unknown): boolean =>
node != null && typeof node === 'object' && 'type' in node && node.type === type;

export const isPosition = isNodeType.bind(undefined, 'position');
export const isLiteral = (node: unknown): node is Literal => isNodeType('literal', node);
export const isPosition = (node: unknown): node is Position => isNodeType('position', node);

export const isPoint = isNodeType.bind(undefined, 'point');
export const isPoint = (node: unknown): node is Point => isNodeType('point', node);

export const isParseResult = isNodeType.bind(undefined, 'parseResult');
export const isParseResult = (node: unknown): node is ParseResult =>
isNodeType('parseResult', node);
6 changes: 3 additions & 3 deletions packages/apidom-ast/src/yaml/nodes/YamlAlias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import Node from '../../Node';
import type { NodeOptions } from '../../Node';

export interface YamlAliasOptions extends NodeOptions {
content?: string | null;
readonly content: string;
}

class YamlAlias extends Node {
public static readonly type: string = 'alias';

public content: string | null;
public readonly content: string;

constructor({ content = null, ...rest }: YamlAliasOptions = {}) {
constructor({ content, ...rest }: YamlAliasOptions) {
super({ ...rest });
this.content = content;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/apidom-ast/src/yaml/nodes/YamlAnchor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import Node from '../../Node';
import type { NodeOptions } from '../../Node';

export interface YamlAnchorOptions extends NodeOptions {
name?: string | null;
readonly name: string;
}

class YamlAnchor extends Node {
public static readonly type: string = 'anchor';

public name: string | null;
public readonly name: string;

constructor({ name = null, ...rest }: YamlAnchorOptions = {}) {
constructor({ name, ...rest }: YamlAnchorOptions) {
super({ ...rest });
this.name = name;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/apidom-ast/src/yaml/nodes/YamlComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import Node from '../../Node';
import type { NodeOptions } from '../../Node';

export interface YamlCommentOptions extends NodeOptions {
content?: string | null;
content: string;
}

class YamlComment extends Node {
public static readonly type: string = 'comment';

public content: string | null;
public readonly content: string;

constructor({ content = null, ...rest }: YamlCommentOptions = {}) {
constructor({ content, ...rest }: YamlCommentOptions) {
super({ ...rest });
this.content = content;
}
Expand Down
22 changes: 11 additions & 11 deletions packages/apidom-ast/src/yaml/nodes/YamlDirective.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,31 @@ import Node from '../../Node';
import type { NodeOptions } from '../../Node';

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

export interface YamlDirectiveOptions extends NodeOptions {
name?: string | null;
parameters?: YamlDirectiveParameters | object;
name?: string;
parameters: YamlDirectiveParameters;
}

class YamlDirective extends Node {
public static readonly type: string = 'directive';

public name: string | null;
public readonly name?: string;

public parameters: YamlDirectiveParameters | null;
public readonly parameters: YamlDirectiveParameters;

constructor({ name = null, parameters = {}, ...rest }: YamlDirectiveOptions = {}) {
constructor({ name, parameters, ...rest }: YamlDirectiveOptions) {
super({ ...rest });
this.name = name;
this.parameters = mergeRight(
{
version: null,
handle: null,
prefix: null,
version: undefined,
handle: undefined,
prefix: undefined,
},
parameters,
);
Expand Down
6 changes: 3 additions & 3 deletions packages/apidom-ast/src/yaml/nodes/YamlKeyValuePair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import type { YamlStyleGroup } from './YamlStyle';
import { isScalar, isMapping, isSequence, isAlias } from './predicates';

export interface YamlKeyValuePairOptions extends NodeOptions {
styleGroup?: YamlStyleGroup | null;
styleGroup: YamlStyleGroup;
}

class YamlKeyValuePair extends Node {
public static readonly type: string = 'keyValuePair';

public styleGroup: YamlStyleGroup | null;
public readonly styleGroup: YamlStyleGroup;

constructor({ styleGroup = null, ...rest }: YamlKeyValuePairOptions = {}) {
constructor({ styleGroup, ...rest }: YamlKeyValuePairOptions) {
super({ ...rest });
this.styleGroup = styleGroup;
}
Expand Down
24 changes: 9 additions & 15 deletions packages/apidom-ast/src/yaml/nodes/YamlNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,22 @@ import YamlAnchor from './YamlAnchor';
import { YamlStyle, YamlStyleGroup } from './YamlStyle';

export interface YamlNodeOptions extends NodeOptions {
anchor?: YamlAnchor | null;
tag?: YamlTag | null;
style?: YamlStyle | null;
styleGroup?: YamlStyleGroup | null;
anchor?: YamlAnchor;
tag?: YamlTag;
style: YamlStyle;
styleGroup: YamlStyleGroup;
}

class YamlNode extends Node {
public anchor: YamlAnchor | null;
public readonly anchor?: YamlAnchor;

public tag: YamlTag | null;
public readonly tag?: YamlTag;

public style: YamlStyle | null;
public readonly style: YamlStyle;

public styleGroup: YamlStyleGroup | null;
public readonly styleGroup: YamlStyleGroup;

constructor({
anchor = null,
tag = null,
style = null,
styleGroup = null,
...rest
}: YamlNodeOptions = {}) {
constructor({ anchor, tag, style, styleGroup, ...rest }: YamlNodeOptions) {
super({ ...rest });
this.anchor = anchor;
this.tag = tag;
Expand Down
10 changes: 3 additions & 7 deletions packages/apidom-ast/src/yaml/nodes/YamlScalar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@ import YamlNode from './YamlNode';
import type { YamlNodeOptions } from './YamlNode';

export interface YamlScalarOptions extends YamlNodeOptions {
content?: string | null;
content: string;
}

class YamlScalar extends YamlNode {
public static readonly type: string = 'scalar';

public readonly content: string | null;
public readonly content: string;

public format: string | null = null;

public text: string | null = null;

constructor({ content = '', ...rest }: YamlScalarOptions = {}) {
constructor({ content, ...rest }: YamlScalarOptions) {
super({ ...rest });
this.content = content;
}
Expand Down
10 changes: 5 additions & 5 deletions packages/apidom-ast/src/yaml/nodes/YamlTag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ export enum YamlNodeKind {
}

export interface YamlTagOptions extends NodeOptions {
explicitName?: string;
kind?: YamlNodeKind | null;
explicitName: string;
kind: YamlNodeKind;
}

class YamlTag extends Node {
public static readonly type: string = 'tag';

public explicitName: string;
public readonly explicitName: string;

public kind: YamlNodeKind | null;
public readonly kind: YamlNodeKind;

constructor({ explicitName = '', kind = null, ...rest }: YamlTagOptions = {}) {
constructor({ explicitName, kind, ...rest }: YamlTagOptions) {
super({ ...rest });
this.explicitName = explicitName;
this.kind = kind;
Expand Down
31 changes: 21 additions & 10 deletions packages/apidom-ast/src/yaml/nodes/predicates.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
import type YamlStream from './YamlStream';
import type YamlDocument from './YamlDocument';
import type YamlMapping from './YamlMapping';
import type YamlSequence from './YamlSequence';
import type YamlKeyValuePair from './YamlKeyValuePair';
import type YamlTag from './YamlTag';
import type YamlScalar from './YamlScalar';
import type YamlAlias from './YamlAlias';
import type YamlDirective from './YamlDirective';
import type YamlComment from './YamlComment';
import { isNodeType } from '../../predicates';

export const isStream = isNodeType.bind(undefined, 'stream');
export const isStream = (node: unknown): node is YamlStream => isNodeType('stream', node);

export const isDocument = isNodeType.bind(undefined, 'document');
export const isDocument = (node: unknown): node is YamlDocument => isNodeType('document', node);

export const isMapping = isNodeType.bind(undefined, 'mapping');
export const isMapping = (node: unknown): node is YamlMapping => isNodeType('mapping', node);

export const isSequence = isNodeType.bind(undefined, 'sequence');
export const isSequence = (node: unknown): node is YamlSequence => isNodeType('sequence', node);

export const isKeyValuePair = isNodeType.bind(undefined, 'keyValuePair');
export const isKeyValuePair = (node: unknown): node is YamlKeyValuePair =>
isNodeType('keyValuePair', node);

export const isTag = isNodeType.bind(undefined, 'tag');
export const isTag = (node: unknown): node is YamlTag => isNodeType('tag', node);

export const isScalar = isNodeType.bind(undefined, 'scalar');
export const isScalar = (node: unknown): node is YamlScalar => isNodeType('scalar', node);

export const isAlias = isNodeType.bind(undefined, 'alias');
export const isAlias = (node: unknown): node is YamlAlias => isNodeType('alias', node);

export const isDirective = isNodeType.bind(undefined, 'directive');
export const isDirective = (node: unknown): node is YamlDirective => isNodeType('directive', node);

export const isComment = isNodeType.bind(undefined, 'comment');
export const isComment = (node: unknown): node is YamlComment => isNodeType('comment', node);
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import {
Error,
isNode as isCSTNode,
Literal,
ParseResult,
Point,
Position,
YamlAnchor,
YamlComment,
YamlDirective,
YamlStream,
YamlDocument,
YamlSequence,
YamlMapping,
YamlKeyValuePair,
YamlTag,
YamlAnchor,
YamlMapping,
YamlNodeKind,
YamlScalar,
YamlComment,
YamlSequence,
YamlStream,
YamlStyle,
YamlStyleGroup,
YamlNodeKind,
ParseResult,
Position,
Point,
Literal,
Error,
isNode as isCSTNode,
YamlTag,
} from '@swagger-api/apidom-ast';

import TreeCursorSyntaxNode from '../../TreeCursorSyntaxNode';
Expand Down Expand Up @@ -95,10 +95,10 @@ class CstVisitor {
return new YamlTag({ explicitName, kind, position });
}

private static kindNodeToYamlAnchor(node: TreeCursorSyntaxNode): YamlAnchor | null {
private static kindNodeToYamlAnchor(node: TreeCursorSyntaxNode): YamlAnchor | undefined {
const { anchor: anchorNode } = node;

if (typeof anchorNode === 'undefined') return null;
if (typeof anchorNode === 'undefined') return undefined;

return new YamlAnchor({ name: anchorNode.text, position: CstVisitor.toPosition(anchorNode) });
}
Expand Down Expand Up @@ -127,7 +127,7 @@ class CstVisitor {
const anchor =
typeof anchorNode !== 'undefined'
? new YamlAnchor({ name: anchorNode.text, position: CstVisitor.toPosition(anchorNode) })
: null;
: undefined;

return new YamlScalar({
content: '',
Expand Down Expand Up @@ -163,7 +163,7 @@ class CstVisitor {
const anchor =
typeof anchorNode !== 'undefined'
? new YamlAnchor({ name: anchorNode.text, position: CstVisitor.toPosition(anchorNode) })
: null;
: undefined;

return new YamlScalar({
content: '',
Expand Down Expand Up @@ -196,7 +196,7 @@ class CstVisitor {
public readonly yaml_directive = {
enter: (node: TreeCursorSyntaxNode): YamlDirective => {
const position = CstVisitor.toPosition(node);
const version = node?.firstNamedChild?.text || null;
const version = node?.firstNamedChild?.text;

return new YamlDirective({
position,
Expand All @@ -217,8 +217,8 @@ class CstVisitor {
position,
name: '%TAG',
parameters: {
handle: tagHandleNode?.text || null,
prefix: tagPrefixNode?.text || null,
handle: tagHandleNode?.text,
prefix: tagPrefixNode?.text,
},
});

Expand All @@ -237,10 +237,10 @@ class CstVisitor {

return new YamlDirective({
position,
name: directiveNameNode?.text || null,
name: directiveNameNode?.text,
parameters: {
handle: directiveParameter1Node?.text || null,
prefix: directiveParameter2Node?.text || null,
handle: directiveParameter1Node?.text,
prefix: directiveParameter2Node?.text,
},
});
},
Expand Down Expand Up @@ -434,7 +434,6 @@ class CstVisitor {
});
const emptyScalarNode = new YamlScalar({
content: '',
anchor: null,
tag: new YamlTag({
explicitName: '?',
kind: YamlNodeKind.Scalar,
Expand Down Expand Up @@ -535,7 +534,7 @@ class CstVisitor {
? YamlStyle.Literal
: node.text.startsWith('>')
? YamlStyle.Folded
: null;
: YamlStyle.Plain;
const scalarNode = new YamlScalar({
content: node.text,
anchor,
Expand Down

0 comments on commit 1247078

Please sign in to comment.