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

Fix obvious lints. #262

Merged
merged 1 commit into from
Apr 22, 2024
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
1 change: 0 additions & 1 deletion tools/eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export default [
'@typescript-eslint/consistent-type-imports': 'warn',
'@typescript-eslint/dot-notation': 'warn',
'@typescript-eslint/explicit-function-return-type': 'warn',
'@typescript-eslint/indent': 'warn',
'@typescript-eslint/keyword-spacing': 'warn',
'@typescript-eslint/lines-between-class-members': 'warn',
'@typescript-eslint/member-delimiter-style': 'warn',
Expand Down
58 changes: 29 additions & 29 deletions tools/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
import fs from "fs";

Check warning on line 1 in tools/helpers.ts

View workflow job for this annotation

GitHub Actions / tools-tests

Strings must use singlequote

Check warning on line 1 in tools/helpers.ts

View workflow job for this annotation

GitHub Actions / tools-tests

Extra semicolon
import YAML from "yaml";

Check warning on line 2 in tools/helpers.ts

View workflow job for this annotation

GitHub Actions / tools-tests

Strings must use singlequote

Check warning on line 2 in tools/helpers.ts

View workflow job for this annotation

GitHub Actions / tools-tests

Extra semicolon
import _ from "lodash";

Check warning on line 3 in tools/helpers.ts

View workflow job for this annotation

GitHub Actions / tools-tests

Strings must use singlequote

Check warning on line 3 in tools/helpers.ts

View workflow job for this annotation

GitHub Actions / tools-tests

Extra semicolon

export function resolve(ref: string, root: Record<string, any>) {

Check warning on line 5 in tools/helpers.ts

View workflow job for this annotation

GitHub Actions / tools-tests

Missing return type on function

Check warning on line 5 in tools/helpers.ts

View workflow job for this annotation

GitHub Actions / tools-tests

Missing space before function parentheses
const paths = ref.replace('#/', '').split('/');
for(const p of paths) {
root = root[p];
if(root === undefined) break;
}
return root;
const paths = ref.replace('#/', '').split('/');

Check warning on line 6 in tools/helpers.ts

View workflow job for this annotation

GitHub Actions / tools-tests

Extra semicolon
for(const p of paths) {

Check warning on line 7 in tools/helpers.ts

View workflow job for this annotation

GitHub Actions / tools-tests

Expected space(s) after "for"
root = root[p];
if(root === undefined) break;
}
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]);
});
sorted.forEach(([k, v]) => {
delete obj[k];
obj[k] = v;
});
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]);
});
sorted.forEach(([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})));
fs.writeFileSync(file_path, quoteRefs(YAML.stringify(removeAnchors(content), {lineWidth: 0, singleQuote: true})));
}

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}'`;
}
return line
}).join('\n');
return str.split('\n').map((line) => {
if(line.includes('$ref')) {
const [key, value] = line.split(': ');
if(!value.startsWith("'")) line = `${key}: '${value}'`;
}
return line
}).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));
const replacer = (key: string, value: any) => key === '$anchor' ? undefined : value;
return JSON.parse(JSON.stringify(content, replacer));
}
120 changes: 60 additions & 60 deletions tools/linter/PathRefsValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,74 @@ import RootFile from "./components/RootFile";
import 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() {
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);
}
#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);
}
}

#build_available_paths() {
for (const file of this.namespaces_folder.files) {
this.available_paths[file.file] = new Set(Object.keys(file.spec().paths || {}));
}
#build_available_paths() {
for (const file of this.namespaces_folder.files) {
this.available_paths[file.file] = new Set(Object.keys(file.spec().paths || {}));
}
}

validate(): ValidationError[] {
return [
...this.validate_unresolved_refs(),
...this.validate_unreferenced_paths(),
];
}
validate(): ValidationError[] {
return [
...this.validate_unresolved_refs(),
...this.validate_unreferenced_paths(),
];
}

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.`,
};
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.`,
};

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[];
});
}
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[];
});
}

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 {
file: ns_file,
location: `Path: ${path}`,
message: `Unreferenced path: Path ${path} is not referenced in the root file.`,
};
}).filter((e) => e) as 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 {
file: ns_file,
location: `Path: ${path}`,
message: `Unreferenced path: Path ${path} is not referenced in the root file.`,
};
}).filter((e) => e) as ValidationError[];
});
}
}
Loading
Loading