Skip to content

Commit

Permalink
Fix obvious lints. (#265)
Browse files Browse the repository at this point in the history
* 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
dblock authored Apr 23, 2024
1 parent 9f4ed28 commit 3ed6aaf
Show file tree
Hide file tree
Showing 38 changed files with 1,122 additions and 1,155 deletions.
33 changes: 2 additions & 31 deletions tools/eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,55 +13,26 @@ export default [
...compat.extends('standard-with-typescript'),
{
files: ['**/*.{js,ts}'],
ignores: [
'**/eslint.config.mjs'
],
// to auto-fix disable all rules except the one you want to fix with '@rule': 'warn', then run `npm run lint -- --fix`
rules: {
'@typescript-eslint/array-type': 'warn',
'@typescript-eslint/block-spacing': 'warn',
'@typescript-eslint/comma-dangle': 'warn',
'@typescript-eslint/comma-spacing': 'warn',
'@typescript-eslint/consistent-indexed-object-style': 'warn',
'@typescript-eslint/consistent-type-assertions': 'warn',
'@typescript-eslint/consistent-type-imports': 'warn',
'@typescript-eslint/dot-notation': 'warn',
'@typescript-eslint/explicit-function-return-type': 'warn',
'@typescript-eslint/keyword-spacing': 'warn',
'@typescript-eslint/lines-between-class-members': 'warn',
'@typescript-eslint/member-delimiter-style': 'warn',
'@typescript-eslint/naming-convention': 'warn',
'@typescript-eslint/no-confusing-void-expression': 'warn',
'@typescript-eslint/no-dynamic-delete': 'warn',
'@typescript-eslint/no-invalid-void-type': 'warn',
'@typescript-eslint/no-non-null-assertion': 'warn',
'@typescript-eslint/no-unnecessary-type-assertion': 'warn',
'@typescript-eslint/no-unsafe-argument': 'warn',
'@typescript-eslint/no-unused-vars': 'warn',
'@typescript-eslint/object-curly-spacing': 'warn',
'@typescript-eslint/prefer-nullish-coalescing': 'warn',
'@typescript-eslint/quotes': 'warn',
'@typescript-eslint/require-array-sort-compare': 'warn',
'@typescript-eslint/semi': 'warn',
'@typescript-eslint/space-before-blocks': 'warn',
'@typescript-eslint/space-before-function-paren': 'warn',
'@typescript-eslint/space-infix-ops': 'warn',
'@typescript-eslint/strict-boolean-expressions': 'warn',
'@typescript-eslint/type-annotation-spacing': 'warn',
'array-bracket-spacing': 'warn',
'array-callback-return': 'warn',
curly: 'warn',
'eol-last': 'warn',
eqeqeq: 'warn',
'new-cap': 'warn',
'no-multi-spaces': 'warn',
'no-multiple-empty-lines': 'warn',
'no-return-assign': 'warn',
'no-useless-return': 'warn',
'object-curly-newline': 'warn',
'object-property-newline': 'warn',
'object-shorthand': 'warn',
'quote-props': 'warn',
'space-in-parens': 'warn'
'object-shorthand': 'warn'
}
}
]
66 changes: 33 additions & 33 deletions tools/helpers.ts
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>) {

Check warning on line 5 in tools/helpers.ts

View workflow job for this annotation

GitHub Actions / tools-tests

Missing return type on function
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[] = []) {

Check warning on line 14 in tools/helpers.ts

View workflow job for this annotation

GitHub Actions / tools-tests

Missing return type on function
const orders = _.fromPairs(priorities.map((k, i) => [k, i + 1]))
const sorted = _.entries(obj).sort((a, b) => {
const order_a = orders[a[0]]

Check warning on line 17 in tools/helpers.ts

View workflow job for this annotation

GitHub Actions / tools-tests

Variable name `order_a` must match one of the following formats: camelCase, PascalCase, UPPER_CASE
const order_b = orders[b[0]]

Check warning on line 18 in tools/helpers.ts

View workflow job for this annotation

GitHub Actions / tools-tests

Variable name `order_b` must match one of the following formats: camelCase, PascalCase, UPPER_CASE
if (order_a && order_b) return order_a - order_b

Check warning on line 19 in tools/helpers.ts

View workflow job for this annotation

GitHub Actions / tools-tests

Unexpected number value in conditional. An explicit zero/NaN check is required

Check warning on line 19 in tools/helpers.ts

View workflow job for this annotation

GitHub Actions / tools-tests

Unexpected number value in conditional. An explicit zero/NaN check is required
if (order_a) return 1

Check warning on line 20 in tools/helpers.ts

View workflow job for this annotation

GitHub Actions / tools-tests

Unexpected number value in conditional. An explicit zero/NaN check is required
if (order_b) return -1

Check warning on line 21 in tools/helpers.ts

View workflow job for this annotation

GitHub Actions / tools-tests

Unexpected number value in conditional. An explicit zero/NaN check is required
return a[0].localeCompare(b[0])
})
sorted.forEach(([k, v]) => {
delete obj[k];
obj[k] = v;
});
delete obj[k]

Check warning on line 25 in tools/helpers.ts

View workflow job for this annotation

GitHub Actions / tools-tests

Do not delete dynamically computed property keys
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 {

Check warning on line 30 in tools/helpers.ts

View workflow job for this annotation

GitHub Actions / tools-tests

Parameter name `file_path` must match one of the following formats: camelCase, PascalCase, UPPER_CASE
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))
}
108 changes: 58 additions & 50 deletions tools/linter/PathRefsValidator.ts
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[]
})
}
}
}
Loading

0 comments on commit 3ed6aaf

Please sign in to comment.