Skip to content

Commit

Permalink
Replace tslint with eslint (airtasker#744)
Browse files Browse the repository at this point in the history
  • Loading branch information
lfportal authored Mar 23, 2020
1 parent 392ef03 commit 47bd3ae
Show file tree
Hide file tree
Showing 37 changed files with 469 additions and 171 deletions.
5 changes: 5 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
build
jest.config.js
jest.ci.config.js
webpack.config.js
51 changes: 51 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module.exports = {
root: true,
parser: "@typescript-eslint/parser",
plugins: ["@typescript-eslint"],
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
rules: {
"@typescript-eslint/camelcase": ["error", { properties: "never" }],
"@typescript-eslint/no-inferrable-types": "off",
"@typescript-eslint/no-use-before-define": "off"
},
overrides: [
{
files: [
"**/__spec-examples__/**/*.ts",
"lib/src/validation-server/spots/**/*.ts"
],
rules: {
"@typescript-eslint/ban-types": "off",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/no-unused-vars": "off",
"@typescript-eslint/no-explicit-any": "off"
}
},
{
files: ["lib/src/syntax/**/*.ts"],
rules: {
"@typescript-eslint/no-unused-vars": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/no-empty-function": "off"
}
},
{
files: ["*.tsx"],
rules: {
"@typescript-eslint/no-unused-vars": "off"
}
},
{
files: ["*.spec.ts"],
rules: {
"@typescript-eslint/explicit-function-return-type": "off"
}
}
]
};
2 changes: 1 addition & 1 deletion cli/src/commands/checksum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default class Checksum extends Command {
help: flags.help({ char: "h" })
};

async run() {
async run(): Promise<void> {
const { args } = this.parse(Checksum);
try {
const contract = parse(args[ARG_API]);
Expand Down
4 changes: 2 additions & 2 deletions cli/src/commands/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default class Docs extends Command {
})
};

async run() {
async run(): Promise<void> {
const { args, flags } = this.parse(Docs);
const { port } = flags;

Expand All @@ -49,7 +49,7 @@ export default class Docs extends Command {
res.send(openApiObj);
});

const start = async () => {
const start = async (): Promise<void> => {
try {
this.log(`Documentation server started on port ${port}`);
this.log(`Open http://localhost:${port} to view documentation`);
Expand Down
25 changes: 15 additions & 10 deletions cli/src/commands/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ export default class Generate extends Command {
})
};

async run() {
async run(): Promise<void> {
const { flags } = this.parse(Generate);
// tslint:disable-next-line:prefer-const
let { contract: contractPath, language, generator, out: outDir } = flags;
const { contract: contractPath } = flags;
let { language, generator, out: outDir } = flags;
const contractFilename = path.basename(contractPath, ".ts");

if (!generator) {
Expand Down Expand Up @@ -116,11 +116,11 @@ export default class Generate extends Command {
}
}

function availableGenerators() {
function availableGenerators(): string[] {
return Object.keys(generators).sort((a, b) => (a > b ? 1 : -1));
}

function availableFormats(generator: string) {
function availableFormats(generator: string): string[] {
return Object.keys(generators[generator].formats).sort((a, b) =>
a > b ? 1 : -1
);
Expand All @@ -131,31 +131,36 @@ interface Generators {
}

interface Generator {
transformer: (contract: Contract) => Object;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
transformer: (contract: Contract) => Record<string, any>;
formats: {
[name: string]: Format;
};
}

interface Format {
formatter: (generatedObject: Object) => string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
formatter: (generatedObject: Record<string, any>) => string;
extension: string;
}

const jsonFormat: Format = {
formatter: (obj: Object) => JSON.stringify(obj, null, 2),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
formatter: (obj: Record<string, any>) => JSON.stringify(obj, null, 2),
extension: "json"
};

const yamlFormat: Format = {
formatter: (obj: Object) =>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
formatter: (obj: Record<string, any>) =>
YAML.safeDump(obj, { skipInvalid: true /* for undefined */ }),
extension: "yml"
};

const generators: Generators = {
raw: {
transformer: (contract: Contract) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
transformer: (contract: Contract): Record<string, any> => {
return contract;
},
formats: {
Expand Down
3 changes: 1 addition & 2 deletions cli/src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ Generated the following files:
help: flags.help({ char: "h" })
};

async run() {
async run(): Promise<void> {
if (fs.existsSync("api.ts")) {
this.error(`There is already an API here!`);
this.exit(1);
}
outputFile(
".",
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default class Lint extends Command {
help: flags.help({ char: "h" })
};

async run() {
async run(): Promise<void> {
const { args } = this.parse(Lint);
const contractPath = args[ARG_API];
const contract = parse(contractPath);
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default class Mock extends Command {
})
};

async run() {
async run(): Promise<void> {
const {
args,
flags: { port, pathPrefix, proxyBaseUrl = "" }
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default class Validate extends Command {
help: flags.help({ char: "h" })
};

async run() {
async run(): Promise<void> {
const { args } = this.parse(Validate);
try {
parse(args[ARG_API]);
Expand Down
4 changes: 2 additions & 2 deletions cli/src/commands/validation-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default class ValidationServer extends Command {
})
};

async run() {
async run(): Promise<void> {
const { args, flags } = this.parse(ValidationServer);
const contractPath = args[ARG_API];
const { port } = flags;
Expand All @@ -40,7 +40,7 @@ export default class ValidationServer extends Command {
const contract = parse(contractPath);

this.log("Starting validation server...");
await runValidationServer(port, contract, this).defer();
await runValidationServer(port, contract).defer();
this.log(`Validation server running on port ${port}`);
} catch (e) {
this.error(e, { exit: 1 });
Expand Down
2 changes: 1 addition & 1 deletion docs/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from "react";
import { RedocStandalone } from "redoc";

class App extends React.Component {
render() {
render(): JSX.Element {
return (
<div>
<RedocStandalone specUrl="/contract-openapi3" />
Expand Down
3 changes: 2 additions & 1 deletion lib/src/generators/json-schema/json-schema-type-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export function typeToJsonSchemaType(
objectAdditionalProperties
)
};
case TypeKind.UNION:
case TypeKind.UNION: {
const elements = type.types;
if (elements.length === 0) throw new Error("Union type has no elements");
if (elements.length === 1) return typeToJsonSchemaType(elements[0]);
Expand Down Expand Up @@ -148,6 +148,7 @@ export function typeToJsonSchemaType(
oneOf: oneOfElements
};
}
}
case TypeKind.REFERENCE:
return {
$ref: `#/definitions/${type.name}`
Expand Down
2 changes: 1 addition & 1 deletion lib/src/generators/openapi2/openapi2-parameter-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ const queryParamArrayTypeToParameterArrayTypeObject = (
type: ArrayType,
typeTable: TypeTable,
config: Config
) =>
): ArrayMultiParameterObjectType =>
arrayTypeToParameterArrayMultiTypeObject(
type,
typeTable,
Expand Down
10 changes: 5 additions & 5 deletions lib/src/generators/openapi2/openapi2-specification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ export interface ArrayMultiParameterObjectType
interface ArrayParameterObjectTypeBase {
type: "array";
items: ItemsObject;
default?: any[];
default?: any[]; // eslint-disable-line @typescript-eslint/no-explicit-any
maxItems?: number;
minItems?: number;
uniqueItems?: boolean;
Expand Down Expand Up @@ -368,7 +368,7 @@ export interface ExternalDocumentationObject {

// https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#exampleObject
export interface ExampleObject {
[mimeType: string]: any;
[mimeType: string]: any; // eslint-disable-line @typescript-eslint/no-explicit-any
}

// https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#schemaObject
Expand All @@ -393,7 +393,7 @@ interface SchemaObjectBase {
"x-nullable"?: boolean;
title?: string;
description?: string;
example?: any;
example?: any; // eslint-disable-line @typescript-eslint/no-explicit-any
externalDocs?: ExternalDocumentationObject;
}

Expand Down Expand Up @@ -443,7 +443,7 @@ export interface BooleanSchemaObject extends SchemaObjectBase {

export interface ArraySchemaObject extends SchemaObjectBase {
type: "array";
default?: any[];
default?: any[]; // eslint-disable-line @typescript-eslint/no-explicit-any
maxItems?: number;
minItems?: number;
uniqueItems?: boolean;
Expand All @@ -452,7 +452,7 @@ export interface ArraySchemaObject extends SchemaObjectBase {

export interface ObjectSchemaObject extends SchemaObjectBase {
type: "object";
default?: any;
default?: any; // eslint-disable-line @typescript-eslint/no-explicit-any
required?: string[];
maxProperties?: number;
minProperties?: number;
Expand Down
16 changes: 8 additions & 8 deletions lib/src/generators/openapi3/openapi3-specification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ interface SchemaObjectBase {
not?: SchemaObject | ReferenceObject;
title?: string;
description?: string;
example?: any;
example?: any; // eslint-disable-line @typescript-eslint/no-explicit-any
externalDocs?: ExternalDocumentationObject;
deprecated?: boolean;
}
Expand Down Expand Up @@ -207,7 +207,7 @@ export interface ArraySchemaObject extends SchemaObjectBase {
minItems?: number;
maxItems?: number;
uniqueItems?: boolean;
default?: any[];
default?: any[]; // eslint-disable-line @typescript-eslint/no-explicit-any
}

export interface ObjectSchemaObject extends SchemaObjectBase {
Expand All @@ -217,7 +217,7 @@ export interface ObjectSchemaObject extends SchemaObjectBase {
additionalProperties?: SchemaObject | ReferenceObject | boolean;
maxProperties?: number;
minProperties?: number;
default?: any;
default?: any; // eslint-disable-line @typescript-eslint/no-explicit-any
}

export interface ObjectPropertiesSchemaObject {
Expand Down Expand Up @@ -362,13 +362,13 @@ export interface XmlObject {
export type ExampleObject = {
summary?: string;
description?: string;
value?: any;
value?: any; // eslint-disable-line @typescript-eslint/no-explicit-any
externalValue?: string;
} & MutuallyExclusiveExampleObjectValue;

type MutuallyExclusiveExampleObjectValue =
| {
value: any;
value: any; // eslint-disable-line @typescript-eslint/no-explicit-any
externalValue?: never;
}
| {
Expand Down Expand Up @@ -459,8 +459,8 @@ export interface CallbackObject {

// https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#linkObject
type LinkObject = {
parameters?: { [name: string]: any };
requestBody?: any;
parameters?: { [name: string]: any }; // eslint-disable-line @typescript-eslint/no-explicit-any
requestBody?: any; // eslint-disable-line @typescript-eslint/no-explicit-any
description?: string;
server?: ServerObject;
} & MutuallyExclusiveLinkObjectOperation;
Expand Down Expand Up @@ -492,7 +492,7 @@ export interface ExternalDocumentationObject {

type MutuallyExclusiveExample =
| {
example?: any;
example?: any; // eslint-disable-line @typescript-eslint/no-explicit-any
examples?: never;
}
| {
Expand Down
3 changes: 2 additions & 1 deletion lib/src/linting/rules/has-discriminator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ function findDisriminatorViolations(
typeTable,
typePath.concat("[]")
);
case TypeKind.UNION:
case TypeKind.UNION: {
const violationsInUnionTypes = type.types.reduce<string[]>((acc, t) => {
return acc.concat(
findDisriminatorViolations(t, typeTable, typePath.concat())
Expand All @@ -170,6 +170,7 @@ function findDisriminatorViolations(
return type.discriminator === undefined
? violationsInUnionTypes.concat(typePath.join("/"))
: violationsInUnionTypes;
}
case TypeKind.REFERENCE:
return findDisriminatorViolations(
dereferenceType(type, typeTable),
Expand Down
3 changes: 2 additions & 1 deletion lib/src/linting/rules/no-inline-objects-within-unions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ function findInlineObjectInUnionViolations(
typeTable,
typePath.concat("[]")
);
case TypeKind.UNION:
case TypeKind.UNION: {
const violationsInUnionTypes = type.types.reduce<string[]>((acc, t) => {
return acc.concat(
findInlineObjectInUnionViolations(t, typeTable, typePath.concat())
Expand All @@ -175,6 +175,7 @@ function findInlineObjectInUnionViolations(
return type.types.some(isObjectType)
? violationsInUnionTypes.concat(typePath.join("/"))
: violationsInUnionTypes;
}
case TypeKind.REFERENCE:
return findInlineObjectInUnionViolations(
dereferenceType(type, typeTable),
Expand Down
Loading

0 comments on commit 47bd3ae

Please sign in to comment.