Skip to content

Commit

Permalink
Fix: '@typescript-eslint/indent': 'warn'. (opensearch-project#262)
Browse files Browse the repository at this point in the history
Signed-off-by: dblock <[email protected]>
  • Loading branch information
dblock authored Apr 22, 2024
1 parent 61a10f4 commit 20cdaa1
Show file tree
Hide file tree
Showing 35 changed files with 1,260 additions and 1,261 deletions.
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
Expand Up @@ -3,45 +3,45 @@ 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;
}
return root;
const paths = ref.replace('#/', '').split('/');
for(const p of paths) {
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

0 comments on commit 20cdaa1

Please sign in to comment.