-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix cosmetic autoformat lints. * Fixed @typescript-eslint/no-unused-vars. * Fixed eqeqeq. * Fixed @typescript-eslint/consistent-type-imports. * Fixed no-useless-return. * Fixed @typescript-eslint/array-type. * Rebased with changes on main. Signed-off-by: dblock <[email protected]>
- Loading branch information
Showing
38 changed files
with
1,122 additions
and
1,155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,47 @@ | ||
import fs from "fs"; | ||
import YAML from "yaml"; | ||
import _ from "lodash"; | ||
import fs from 'fs' | ||
import YAML from 'yaml' | ||
import _ from 'lodash' | ||
|
||
export function resolve(ref: string, root: Record<string, any>) { | ||
const paths = ref.replace('#/', '').split('/'); | ||
for(const p of paths) { | ||
root = root[p]; | ||
if(root === undefined) break; | ||
export function resolve (ref: string, root: Record<string, any>) { | ||
const paths = ref.replace('#/', '').split('/') | ||
for (const p of paths) { | ||
root = root[p] | ||
if (root === undefined) break | ||
} | ||
return root; | ||
return root | ||
} | ||
|
||
export function sortByKey(obj: Record<string, any>, priorities: string[] = []) { | ||
const orders = _.fromPairs(priorities.map((k, i) => [k, i+1])); | ||
const sorted = _.entries(obj).sort((a,b) => { | ||
const order_a = orders[a[0]]; | ||
const order_b = orders[b[0]]; | ||
if(order_a && order_b) return order_a - order_b; | ||
if(order_a) return 1; | ||
if(order_b) return -1; | ||
return a[0].localeCompare(b[0]); | ||
}); | ||
export function sortByKey (obj: Record<string, any>, priorities: string[] = []) { | ||
const orders = _.fromPairs(priorities.map((k, i) => [k, i + 1])) | ||
const sorted = _.entries(obj).sort((a, b) => { | ||
const order_a = orders[a[0]] | ||
const order_b = orders[b[0]] | ||
if (order_a && order_b) return order_a - order_b | ||
Check warning on line 19 in tools/helpers.ts GitHub Actions / tools-tests
|
||
if (order_a) return 1 | ||
if (order_b) return -1 | ||
return a[0].localeCompare(b[0]) | ||
}) | ||
sorted.forEach(([k, v]) => { | ||
delete obj[k]; | ||
obj[k] = v; | ||
}); | ||
delete obj[k] | ||
obj[k] = v | ||
}) | ||
} | ||
|
||
export function write2file(file_path: string, content: Record<string, any>): void { | ||
fs.writeFileSync(file_path, quoteRefs(YAML.stringify(removeAnchors(content), {lineWidth: 0, singleQuote: true}))); | ||
export function write2file (file_path: string, content: Record<string, any>): void { | ||
fs.writeFileSync(file_path, quoteRefs(YAML.stringify(removeAnchors(content), { lineWidth: 0, singleQuote: true }))) | ||
} | ||
|
||
function quoteRefs(str: string): string { | ||
function quoteRefs (str: string): string { | ||
return str.split('\n').map((line) => { | ||
if(line.includes('$ref')) { | ||
const [key, value] = line.split(': '); | ||
if(!value.startsWith("'")) line = `${key}: '${value}'`; | ||
if (line.includes('$ref')) { | ||
const [key, value] = line.split(': ') | ||
if (!value.startsWith("'")) line = `${key}: '${value}'` | ||
} | ||
return line | ||
}).join('\n'); | ||
}).join('\n') | ||
} | ||
|
||
function removeAnchors(content: Record<string, any>): Record<string, any> { | ||
const replacer = (key: string, value: any) => key === '$anchor' ? undefined : value; | ||
return JSON.parse(JSON.stringify(content, replacer)); | ||
} | ||
function removeAnchors (content: Record<string, any>): Record<string, any> { | ||
const replacer = (key: string, value: any) => key === '$anchor' ? undefined : value | ||
return JSON.parse(JSON.stringify(content, replacer)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,84 @@ | ||
import {ValidationError} from "../types"; | ||
import RootFile from "./components/RootFile"; | ||
import NamespacesFolder from "./components/NamespacesFolder"; | ||
import { type ValidationError } from '../types' | ||
import type RootFile from './components/RootFile' | ||
import type NamespacesFolder from './components/NamespacesFolder' | ||
|
||
export default class PathRefsValidator { | ||
root_file: RootFile; | ||
namespaces_folder: NamespacesFolder; | ||
root_file: RootFile | ||
namespaces_folder: NamespacesFolder | ||
|
||
referenced_paths: Record<string, Set<string>> = {}; // file -> paths | ||
available_paths: Record<string, Set<string>> = {}; // file -> paths | ||
referenced_paths: Record<string, Set<string>> = {} // file -> paths | ||
available_paths: Record<string, Set<string>> = {} // file -> paths | ||
|
||
constructor(root_file: RootFile, namespaces_folder: NamespacesFolder) { | ||
this.root_file = root_file; | ||
this.namespaces_folder = namespaces_folder; | ||
this.#build_referenced_paths(); | ||
this.#build_available_paths(); | ||
constructor (root_file: RootFile, namespaces_folder: NamespacesFolder) { | ||
this.root_file = root_file | ||
this.namespaces_folder = namespaces_folder | ||
this.#build_referenced_paths() | ||
this.#build_available_paths() | ||
} | ||
|
||
#build_referenced_paths() { | ||
#build_referenced_paths () { | ||
for (const [path, spec] of Object.entries(this.root_file.spec().paths)) { | ||
const ref = spec!.$ref!; | ||
const file = ref.split('#')[0]; | ||
if(!this.referenced_paths[file]) this.referenced_paths[file] = new Set(); | ||
this.referenced_paths[file].add(path); | ||
const ref = spec!.$ref! | ||
const file = ref.split('#')[0] | ||
if (!this.referenced_paths[file]) this.referenced_paths[file] = new Set() | ||
this.referenced_paths[file].add(path) | ||
} | ||
} | ||
|
||
#build_available_paths() { | ||
#build_available_paths () { | ||
for (const file of this.namespaces_folder.files) { | ||
this.available_paths[file.file] = new Set(Object.keys(file.spec().paths || {})); | ||
this.available_paths[file.file] = new Set(Object.keys(file.spec().paths || {})) | ||
} | ||
} | ||
|
||
validate(): ValidationError[] { | ||
validate (): ValidationError[] { | ||
return [ | ||
...this.validate_unresolved_refs(), | ||
...this.validate_unreferenced_paths(), | ||
]; | ||
...this.validate_unreferenced_paths() | ||
] | ||
} | ||
|
||
validate_unresolved_refs(): ValidationError[] { | ||
validate_unresolved_refs (): ValidationError[] { | ||
return Object.entries(this.referenced_paths).flatMap(([ref_file, ref_paths]) => { | ||
const available = this.available_paths[ref_file]; | ||
if(!available) return { | ||
file: this.root_file.file, | ||
location: `Paths: ${[...ref_paths].join(' , ')}`, | ||
message: `Unresolved path reference: Namespace file ${ref_file} does not exist.`, | ||
}; | ||
const available = this.available_paths[ref_file] | ||
if (!available) { | ||
return { | ||
file: this.root_file.file, | ||
location: `Paths: ${[...ref_paths].join(' , ')}`, | ||
message: `Unresolved path reference: Namespace file ${ref_file} does not exist.` | ||
} | ||
} | ||
|
||
return Array.from(ref_paths).map((path) => { | ||
if(!available.has(path)) return { | ||
file: this.root_file.file, | ||
location: `Path: ${path}`, | ||
message: `Unresolved path reference: Path ${path} does not exist in namespace file ${ref_file}.`, | ||
}; | ||
}).filter((e) => e) as ValidationError[]; | ||
}); | ||
if (!available.has(path)) { | ||
return { | ||
file: this.root_file.file, | ||
location: `Path: ${path}`, | ||
message: `Unresolved path reference: Path ${path} does not exist in namespace file ${ref_file}.` | ||
} | ||
} | ||
}).filter((e) => e) as ValidationError[] | ||
}) | ||
} | ||
|
||
validate_unreferenced_paths(): ValidationError[] { | ||
validate_unreferenced_paths (): ValidationError[] { | ||
return Object.entries(this.available_paths).flatMap(([ns_file, ns_paths]) => { | ||
const referenced = this.referenced_paths[ns_file]; | ||
if(!referenced) return { | ||
file: ns_file, | ||
message: `Unreferenced paths: No paths are referenced in the root file.`, | ||
}; | ||
return Array.from(ns_paths).map((path) => { | ||
if(!referenced || !referenced.has(path)) return { | ||
const referenced = this.referenced_paths[ns_file] | ||
if (!referenced) { | ||
return { | ||
file: ns_file, | ||
location: `Path: ${path}`, | ||
message: `Unreferenced path: Path ${path} is not referenced in the root file.`, | ||
}; | ||
}).filter((e) => e) as ValidationError[]; | ||
}); | ||
message: 'Unreferenced paths: No paths are referenced in the root file.' | ||
} | ||
} | ||
return Array.from(ns_paths).map((path) => { | ||
if (!referenced || !referenced.has(path)) { | ||
return { | ||
file: ns_file, | ||
location: `Path: ${path}`, | ||
message: `Unreferenced path: Path ${path} is not referenced in the root file.` | ||
} | ||
} | ||
}).filter((e) => e) as ValidationError[] | ||
}) | ||
} | ||
} | ||
} |
Oops, something went wrong.