Skip to content

Commit

Permalink
chore(core): tweak StringifiedRuleset and StringifiedRule (#2309)
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip authored Oct 14, 2022
1 parent d5ce09e commit 1724aba
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
8 changes: 5 additions & 3 deletions packages/core/src/ruleset/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { HumanReadableDiagnosticSeverity, IRuleThen, RuleDefinition, String
import { minimatch } from './utils/minimatch';
import { Formats } from './formats';
import { resolveAlias } from './alias';
import type { Stringified } from './types';

export interface IRule {
description: string | null;
Expand All @@ -25,13 +26,14 @@ export interface IRule {
given: string[];
}

export type StringifiedRule = Omit<IRule, 'formats' | 'then'> & {
type RuleJson = Omit<IRule, 'then'> & {
name: string;
formats: string[] | null;
then: (Pick<IRuleThen, 'field'> & { function: string; functionOptions?: string })[];
owner: number;
};

export type StringifiedRule = Stringified<RuleJson>;

export class Rule implements IRule {
public description: string | null;
public message: string | null;
Expand Down Expand Up @@ -164,7 +166,7 @@ export class Rule implements IRule {
return new Rule(this.name, this.definition, this.owner);
}

public toJSON(): Stringifable<StringifiedRule> {
public toJSON(): Stringifable<RuleJson> {
return {
name: this.name,
recommended: this.recommended,
Expand Down
9 changes: 6 additions & 3 deletions packages/core/src/ruleset/ruleset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { DEFAULT_PARSER_OPTIONS, getDiagnosticSeverity } from '..';
import { mergeRulesets } from './mergers/rulesets';
import { Formats } from './formats';
import { isSimpleAliasDefinition } from './utils/guards';
import type { Stringified } from './types';

const STACK_SYMBOL = Symbol('@stoplight/spectral/ruleset/#stack');
const DEFAULT_RULESET_FILE = /^\.?spectral\.(ya?ml|json|m?js)$/;
Expand All @@ -29,9 +30,9 @@ type RulesetContext = {

let SEED = 1;

export type StringifiedRuleset = {
type RulesetJson = {
id: number;
extends: StringifiedRuleset[] | null;
extends: RulesetJson[] | null;
source: string | null;
aliases: RulesetAliasesDefinition | null;
formats: Formats | null;
Expand All @@ -40,6 +41,8 @@ export type StringifiedRuleset = {
parserOptions: ParserOptions;
};

export type StringifiedRuleset = Stringified<RulesetJson>;

export class Ruleset {
public readonly id = SEED++;

Expand Down Expand Up @@ -309,7 +312,7 @@ export class Ruleset {
return DEFAULT_RULESET_FILE.test(uri);
}

public toJSON(): Stringifable<StringifiedRuleset> {
public toJSON(): Stringifable<RulesetJson> {
return {
id: this.id,
extends: this.extends,
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/ruleset/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,14 @@ export type RulesetDefinition = Readonly<
>
>;

// eslint-disable-next-line @typescript-eslint/ban-types
export type Stringifable<T> = T extends object
? {
[P in keyof T]: Stringifable<T[P]> | { toJSON?(): Stringifable<T[P]> };
}
: T;

export type Stringified<T> = T extends object
? {
[P in keyof T]: NonNullable<T[P]> extends { toJSON(): infer R } ? R : Stringified<T[P]>;
}
: T;

0 comments on commit 1724aba

Please sign in to comment.