diff --git a/packages/apidom-ast/src/yaml/schemas/ScalarTag.ts b/packages/apidom-ast/src/yaml/schemas/ScalarTag.ts index 835529d4f..c2f0fd233 100644 --- a/packages/apidom-ast/src/yaml/schemas/ScalarTag.ts +++ b/packages/apidom-ast/src/yaml/schemas/ScalarTag.ts @@ -38,5 +38,6 @@ class ScalarTag { return node; } } +/* eslint-enable class-methods-use-this */ export default ScalarTag; diff --git a/packages/apidom-ast/src/yaml/schemas/Tag.ts b/packages/apidom-ast/src/yaml/schemas/Tag.ts index 3fb028f3b..95eda76cc 100644 --- a/packages/apidom-ast/src/yaml/schemas/Tag.ts +++ b/packages/apidom-ast/src/yaml/schemas/Tag.ts @@ -1,7 +1,22 @@ -import ScalarTag from './ScalarTag'; +/* eslint-disable class-methods-use-this */ +class Tag { + public static readonly uri: string = ''; -class Tag extends ScalarTag { - public tag: string = ''; + public readonly tag: string = ''; + + constructor() { + this.tag = (this.constructor as typeof Tag).uri; + } + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + public test(node: any): boolean { + return true; + } + + public resolve(node: any): any { + return node; + } } +/* eslint-enable class-methods-use-this */ export default Tag; diff --git a/packages/apidom-ast/src/yaml/schemas/failsafe/GenericMapping.ts b/packages/apidom-ast/src/yaml/schemas/failsafe/GenericMapping.ts index edb5a6485..eb486977f 100644 --- a/packages/apidom-ast/src/yaml/schemas/failsafe/GenericMapping.ts +++ b/packages/apidom-ast/src/yaml/schemas/failsafe/GenericMapping.ts @@ -1,21 +1,14 @@ import Tag from '../Tag'; import { YamlNodeKind } from '../../nodes/YamlTag'; +/* eslint-disable class-methods-use-this */ class GenericMapping extends Tag { - public static uri: string = 'tag:yaml.org,2002:map'; + public static readonly uri: string = 'tag:yaml.org,2002:map'; - constructor() { - super(); - this.tag = GenericMapping.uri; - } - - public static test(node: any): boolean { + public test(node: any): boolean { return node.tag.kind === YamlNodeKind.Mapping; } - - public static resolve(node: any): any { - return node; - } } +/* eslint-enable class-methods-use-this */ export default GenericMapping; diff --git a/packages/apidom-ast/src/yaml/schemas/failsafe/GenericSequence.ts b/packages/apidom-ast/src/yaml/schemas/failsafe/GenericSequence.ts index 22c7bd9bd..9aa1cf799 100644 --- a/packages/apidom-ast/src/yaml/schemas/failsafe/GenericSequence.ts +++ b/packages/apidom-ast/src/yaml/schemas/failsafe/GenericSequence.ts @@ -1,21 +1,14 @@ import Tag from '../Tag'; import { YamlNodeKind } from '../../nodes/YamlTag'; +/* eslint-disable class-methods-use-this */ class GenericSequence extends Tag { - public static uri: string = 'tag:yaml.org,2002:seq'; + public static readonly uri: string = 'tag:yaml.org,2002:seq'; - constructor() { - super(); - this.tag = GenericSequence.uri; - } - - public static test(node: any): boolean { + public test(node: any): boolean { return node.tag.kind === YamlNodeKind.Sequence; } - - public static resolve(node: any): any { - return node; - } } +/* eslint-enable class-methods-use-this */ export default GenericSequence; diff --git a/packages/apidom-ast/src/yaml/schemas/failsafe/GenericString.ts b/packages/apidom-ast/src/yaml/schemas/failsafe/GenericString.ts index b89f7fa3b..1ce3e5427 100644 --- a/packages/apidom-ast/src/yaml/schemas/failsafe/GenericString.ts +++ b/packages/apidom-ast/src/yaml/schemas/failsafe/GenericString.ts @@ -1,16 +1,7 @@ import Tag from '../Tag'; class GenericString extends Tag { - public static uri: string = 'tag:yaml.org,2002:str'; - - constructor() { - super(); - this.tag = GenericString.uri; - } - - public static resolve(node: any): any { - return node; - } + public static readonly uri: string = 'tag:yaml.org,2002:str'; } export default GenericString; diff --git a/packages/apidom-ast/src/yaml/schemas/failsafe/index.ts b/packages/apidom-ast/src/yaml/schemas/failsafe/index.ts index d9ce43725..5e8c1b651 100644 --- a/packages/apidom-ast/src/yaml/schemas/failsafe/index.ts +++ b/packages/apidom-ast/src/yaml/schemas/failsafe/index.ts @@ -3,9 +3,9 @@ import { clone } from 'ramda'; import YamlTagError from '../../errors/YamlTagError'; import YamlDirective from '../../nodes/YamlDirective'; import { YamlNodeKind } from '../../nodes/YamlTag'; -import GenericMapping from './GenericMapping'; -import GenericSequence from './GenericSequence'; -import GenericString from './GenericString'; +import GenericMappingTag from './GenericMapping'; +import GenericSequenceTag from './GenericSequence'; +import GenericStringTag from './GenericString'; import ScalarTag from '../ScalarTag'; class FailsafeSchema { @@ -16,9 +16,9 @@ class FailsafeSchema { constructor() { this.tags = []; this.tagDirectives = []; - this.registerTag(new GenericMapping()); - this.registerTag(new GenericSequence()); - this.registerTag(new GenericString()); + this.registerTag(new GenericMappingTag()); + this.registerTag(new GenericSequenceTag()); + this.registerTag(new GenericStringTag()); } // eslint-disable-next-line class-methods-use-this @@ -28,11 +28,11 @@ class FailsafeSchema { if (node.tag.explicitName === '!') { // non-specific tag; we assume tag by kind if (node.tag.kind === YamlNodeKind.Scalar) { - specificTagName = GenericString.uri; + specificTagName = GenericStringTag.uri; } else if (node.tag.kind === YamlNodeKind.Sequence) { - specificTagName = GenericSequence.uri; + specificTagName = GenericSequenceTag.uri; } else if (node.tag.kind === YamlNodeKind.Mapping) { - specificTagName = GenericMapping.uri; + specificTagName = GenericMappingTag.uri; } } else if (node.tag.explicitName.startsWith('!<')) { // verbatim form @@ -80,7 +80,7 @@ class FailsafeSchema { // turn scalar nodes into canonical format before resolving let canonicalNode = node; - if (node.tag.kind === YamlNodeKind.Scalar) { + if (ScalarTag.test(node)) { canonicalNode = ScalarTag.canonicalFormat(node); } @@ -98,7 +98,7 @@ class FailsafeSchema { } // node content is not compatible with resolving mechanism (tag implementation) - if (!tag.constructor.test(canonicalNode)) { + if (!tag.test(canonicalNode)) { throw new YamlTagError(`Node couldn't be resolved against the tag "${specificTagName}"`, { specificTagName, explicitTagName: node.tag.explicitName, @@ -109,7 +109,7 @@ class FailsafeSchema { }); } - return tag.constructor.resolve(canonicalNode); + return tag.resolve(canonicalNode); } } diff --git a/packages/apidom-ast/src/yaml/schemas/json/Boolean.ts b/packages/apidom-ast/src/yaml/schemas/json/Boolean.ts index 12598d299..4acc46521 100644 --- a/packages/apidom-ast/src/yaml/schemas/json/Boolean.ts +++ b/packages/apidom-ast/src/yaml/schemas/json/Boolean.ts @@ -1,18 +1,14 @@ import Tag from '../Tag'; +/* eslint-disable class-methods-use-this */ class Boolean extends Tag { - public static uri: string = 'tag:yaml.org,2002:bool'; + public static readonly uri: string = 'tag:yaml.org,2002:bool'; - constructor() { - super(); - this.tag = Boolean.uri; - } - - public static test(node: any): boolean { + public test(node: any): boolean { return /^(true|false)$/.test(node.content); } - public static resolve(node: any): any { + public resolve(node: any): any { const content = node.content === 'true'; const nodeClone = node.clone(); @@ -21,5 +17,6 @@ class Boolean extends Tag { return nodeClone; } } +/* eslint-enable class-methods-use-this */ export default Boolean; diff --git a/packages/apidom-ast/src/yaml/schemas/json/FloatingPoint.ts b/packages/apidom-ast/src/yaml/schemas/json/FloatingPoint.ts index c92e97523..5b094fa82 100644 --- a/packages/apidom-ast/src/yaml/schemas/json/FloatingPoint.ts +++ b/packages/apidom-ast/src/yaml/schemas/json/FloatingPoint.ts @@ -1,18 +1,14 @@ import Tag from '../Tag'; +/* eslint-disable class-methods-use-this */ class FloatingPoint extends Tag { - public static uri: string = 'tag:yaml.org,2002:float'; + public static readonly uri: string = 'tag:yaml.org,2002:float'; - constructor() { - super(); - this.tag = FloatingPoint.uri; - } - - public static test(node: any): boolean { + public test(node: any): boolean { return /^-?(0|[1-9][0-9]*)(\.[0-9]*)?([eE][-+]?[0-9]+)?$/.test(node.content); } - public static resolve(node: any): any { + public resolve(node: any): any { const content = parseFloat(node.content); const nodeClone = node.clone(); @@ -21,5 +17,6 @@ class FloatingPoint extends Tag { return nodeClone; } } +/* eslint-enable class-methods-use-this */ export default FloatingPoint; diff --git a/packages/apidom-ast/src/yaml/schemas/json/Integer.ts b/packages/apidom-ast/src/yaml/schemas/json/Integer.ts index bbee4dc7b..723d2264d 100644 --- a/packages/apidom-ast/src/yaml/schemas/json/Integer.ts +++ b/packages/apidom-ast/src/yaml/schemas/json/Integer.ts @@ -1,18 +1,14 @@ import Tag from '../Tag'; +/* eslint-disable class-methods-use-this */ class Integer extends Tag { - public static uri: string = 'tag:yaml.org,2002:int'; + public static readonly uri: string = 'tag:yaml.org,2002:int'; - constructor() { - super(); - this.tag = Integer.uri; - } - - public static test(node: any): boolean { + public test(node: any): boolean { return /^-?(0|[1-9][0-9]*)$/.test(node.content); } - public static resolve(node: any): any { + public resolve(node: any): any { const content = parseInt(node.content, 10); const nodeClone = node.clone(); @@ -21,5 +17,6 @@ class Integer extends Tag { return nodeClone; } } +/* eslint-enable class-methods-use-this */ export default Integer; diff --git a/packages/apidom-ast/src/yaml/schemas/json/Null.ts b/packages/apidom-ast/src/yaml/schemas/json/Null.ts index b56db0a84..1cc1a00b5 100644 --- a/packages/apidom-ast/src/yaml/schemas/json/Null.ts +++ b/packages/apidom-ast/src/yaml/schemas/json/Null.ts @@ -1,18 +1,14 @@ import Tag from '../Tag'; +/* eslint-disable class-methods-use-this */ class Null extends Tag { - public static uri: string = 'tag:yaml.org,2002:null'; + public static readonly uri: string = 'tag:yaml.org,2002:null'; - constructor() { - super(); - this.tag = Null.uri; - } - - public static test(node: any): boolean { + public test(node: any): boolean { return /^null$/.test(node.content); } - public static resolve(node: any): any { + public resolve(node: any): any { const nodeClone = node.clone(); nodeClone.content = null; @@ -20,5 +16,6 @@ class Null extends Tag { return nodeClone; } } +/* eslint-enable class-methods-use-this */ export default Null; diff --git a/packages/apidom-ast/src/yaml/schemas/json/index.ts b/packages/apidom-ast/src/yaml/schemas/json/index.ts index ef53bf619..dde637f07 100644 --- a/packages/apidom-ast/src/yaml/schemas/json/index.ts +++ b/packages/apidom-ast/src/yaml/schemas/json/index.ts @@ -1,8 +1,8 @@ import FailsafeSchema from '../failsafe/index'; -import Boolean from './Boolean'; -import FloatingPoint from './FloatingPoint'; -import Integer from './Integer'; -import Null from './Null'; +import BooleanTag from './Boolean'; +import FloatingPointTag from './FloatingPoint'; +import IntegerTag from './Integer'; +import NullTag from './Null'; import { YamlNodeKind } from '../../nodes/YamlTag'; import GenericSequence from '../failsafe/GenericSequence'; import GenericMapping from '../failsafe/GenericMapping'; @@ -13,10 +13,10 @@ class JsonSchema extends FailsafeSchema { /** * We're registering more specific tags before more generic ones from Failsafe schema. */ - this.registerTag(new Boolean(), true); - this.registerTag(new FloatingPoint(), true); - this.registerTag(new Integer(), true); - this.registerTag(new Null(), true); + this.registerTag(new BooleanTag(), true); + this.registerTag(new FloatingPointTag(), true); + this.registerTag(new IntegerTag(), true); + this.registerTag(new NullTag(), true); } public toSpecificTagName(node: any): any { @@ -28,7 +28,7 @@ class JsonSchema extends FailsafeSchema { } else if (node.tag.kind === YamlNodeKind.Mapping) { specificTagName = GenericMapping.uri; } else if (node.tag.kind === YamlNodeKind.Scalar) { - const foundTag = this.tags.find((tag) => tag.constructor.test(node)); + const foundTag = this.tags.find((tag) => tag.test(node)); specificTagName = foundTag?.tag || '?'; } }