-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from darraghoriordan/feat/fixes-and-add-new-swa…
…gger-rules feat: add some new rules and fixes
- Loading branch information
Showing
15 changed files
with
381 additions
and
23 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
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
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
33 changes: 33 additions & 0 deletions
33
...ules/apiMethodsShouldSpecifyApiOperation/apiMethodsShouldSpecifyApiOperation.test-data.ts
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
export const testCases = [ | ||
{ | ||
moduleCode: `class TestClass { | ||
@Get() | ||
@ApiOkResponse({ type: String, isArray: true }) | ||
@ApiBadRequestResponse({ description: "Bad Request" }) | ||
public getAll(): Promise<string[]> { | ||
return []; | ||
} | ||
}`, | ||
shouldUseDecorator: false, | ||
message: "all good", | ||
}, | ||
{ | ||
moduleCode: `class TestClass { | ||
@Get() | ||
public getAll(): Promise<string[]> { | ||
return []; | ||
} | ||
}`, | ||
shouldUseDecorator: true, | ||
message: "no api operation decorator", | ||
}, | ||
{ | ||
moduleCode: `class TestClass { | ||
public getAll(): Promise<string[]> { | ||
return []; | ||
} | ||
}`, | ||
shouldUseDecorator: false, | ||
message: "not an http method", | ||
}, | ||
]; |
33 changes: 33 additions & 0 deletions
33
src/rules/apiMethodsShouldSpecifyApiOperation/apiMethodsShouldSpecifyApiOperation.test.ts
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import {typedTokenHelpers} from "../../utils/typedTokenHelpers"; | ||
import { | ||
fakeContext, | ||
fakeFilePath, | ||
} from "../../utils/nestModules/nestProvidedInjectableMapper.test-date"; | ||
import {TSESTree} from "@typescript-eslint/types"; | ||
import {testCases} from "./apiMethodsShouldSpecifyApiOperation.test-data"; | ||
import {shouldUseApiOperationDecorator} from "./apiMethodsShouldSpecifyApiOperation"; | ||
|
||
// should probably be split up into multiple tests | ||
describe("apiMethodsShouldSpecifyApiOperation", () => { | ||
test.each(testCases)( | ||
"is an expected response for %#", | ||
(testCase: { | ||
moduleCode: string; | ||
shouldUseDecorator: boolean; | ||
message: string; | ||
}) => { | ||
const ast = typedTokenHelpers.parseStringToAst( | ||
testCase.moduleCode, | ||
fakeFilePath, | ||
fakeContext | ||
); | ||
|
||
const shouldUseOptional = shouldUseApiOperationDecorator( | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
(ast.body[0] as TSESTree.ClassDeclaration).body | ||
.body[0] as TSESTree.MethodDefinition | ||
); | ||
expect(shouldUseOptional).toEqual(testCase.shouldUseDecorator); | ||
} | ||
); | ||
}); |
85 changes: 85 additions & 0 deletions
85
src/rules/apiMethodsShouldSpecifyApiOperation/apiMethodsShouldSpecifyApiOperation.ts
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 |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Import { getParserServices } from "@typescript-eslint/experimental-utils/dist/eslint-utils"; | ||
// import * as tsutils from "tsutils"; | ||
// import { getParserServices } from "@typescript-eslint/experimental-utils/dist/eslint-utils"; | ||
import {TSESTree} from "@typescript-eslint/types"; | ||
import {createRule} from "../../utils/createRule"; | ||
import {typedTokenHelpers} from "../../utils/typedTokenHelpers"; | ||
|
||
export const shouldUseApiOperationDecorator = ( | ||
node: TSESTree.MethodDefinition | ||
): boolean => { | ||
const hasApiMethodDecorator = typedTokenHelpers.nodeHasDecoratorsNamed( | ||
node, | ||
["Get", "Post", "Put", "Delete", "Patch", "Options", "Head", "All"] | ||
); | ||
|
||
const hasApiOperationDecorator = typedTokenHelpers.nodeHasDecoratorsNamed( | ||
node, | ||
[ | ||
"ApiResponse", | ||
"ApiOkResponse", | ||
"ApiCreatedResponse", | ||
"ApiAcceptedResponse", | ||
"ApiNoContentResponse", | ||
"ApiMovedPermanentlyResponse", | ||
"ApiFoundResponse", | ||
"ApiBadRequestResponse", | ||
"ApiUnauthorizedResponse", | ||
"ApiTooManyRequestsResponse", | ||
"ApiNotFoundResponse", | ||
"ApiInternalServerErrorResponse", | ||
"ApiBadGatewayResponse", | ||
"ApiConflictResponse", | ||
"ApiForbiddenResponse", | ||
"ApiGatewayTimeoutResponse", | ||
"ApiGoneResponse", | ||
"ApiMethodNotAllowedResponse", | ||
"ApiNotAcceptableResponse", | ||
"ApiNotImplementedResponse", | ||
"ApiPreconditionFailedResponse", | ||
"ApiPayloadTooLargeResponse", | ||
"ApiRequestTimeoutResponse", | ||
"ApiServiceUnavailableResponse", | ||
"ApiUnprocessableEntityResponse", | ||
"ApiUnsupportedMediaTypeResponse", | ||
"ApiDefaultResponse", | ||
] | ||
); | ||
|
||
return hasApiMethodDecorator && !hasApiOperationDecorator; | ||
}; | ||
|
||
const rule = createRule({ | ||
name: "api-method-should-specify-api-operation", | ||
meta: { | ||
docs: { | ||
description: | ||
"Api methods should at least specify the expected OK response with @ApiOkResponse. But also add any error responses that might not be expected (e.g. 429)", | ||
category: "Best Practices", | ||
recommended: false, | ||
requiresTypeChecking: true, | ||
}, | ||
messages: { | ||
shouldSpecifyApiOperation: `A method decorated with @Get, @Post etc. should specify the expected ApiOperation e.g. @ApiOkResponse(type: MyType)`, | ||
}, | ||
schema: [], | ||
type: "suggestion", | ||
}, | ||
defaultOptions: [], | ||
|
||
create(context) { | ||
return { | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
MethodDefinition(node: TSESTree.MethodDefinition): void { | ||
if (shouldUseApiOperationDecorator(node)) { | ||
context.report({ | ||
node: node, | ||
messageId: "shouldSpecifyApiOperation", | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}); | ||
|
||
export default rule; |
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
26 changes: 26 additions & 0 deletions
26
src/rules/controllerDecoratedHasApiTags/controllerDecoratedHasApiTags.test-data.ts
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
export const testCases = [ | ||
{ | ||
moduleCode: ` | ||
@ApiTags("my-tag") | ||
@Controller("my-controller") | ||
class TestClass { | ||
}`, | ||
shouldUseApiTagsDecorator: false, | ||
message: "all good state", | ||
}, | ||
{ | ||
moduleCode: ` | ||
@Controller("my-controller") | ||
class TestClass { | ||
}`, | ||
shouldUseApiTagsDecorator: true, | ||
message: "missing tag", | ||
}, | ||
{ | ||
moduleCode: ` | ||
class TestClass { | ||
}`, | ||
shouldUseApiTagsDecorator: false, | ||
message: "not a controller", | ||
}, | ||
]; |
34 changes: 34 additions & 0 deletions
34
src/rules/controllerDecoratedHasApiTags/controllerDecoratedHasApiTags.test.ts
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import {typedTokenHelpers} from "../../utils/typedTokenHelpers"; | ||
import { | ||
fakeContext, | ||
fakeFilePath, | ||
} from "../../utils/nestModules/nestProvidedInjectableMapper.test-date"; | ||
import {TSESTree} from "@typescript-eslint/types"; | ||
import {testCases} from "./controllerDecoratedHasApiTags.test-data"; | ||
import {shouldUseApiTagDecorator} from "./controllerDecoratedHasApiTags"; | ||
|
||
// should probably be split up into multiple tests | ||
describe("controllerDecoratedHasApiTags", () => { | ||
test.each(testCases)( | ||
"is an expected response for %#", | ||
(testCase: { | ||
moduleCode: string; | ||
shouldUseApiTagsDecorator: boolean; | ||
message: string; | ||
}) => { | ||
const ast = typedTokenHelpers.parseStringToAst( | ||
testCase.moduleCode, | ||
fakeFilePath, | ||
fakeContext | ||
); | ||
|
||
const shouldUseOptional = shouldUseApiTagDecorator( | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
ast.body[0] as TSESTree.ClassDeclaration | ||
); | ||
expect(shouldUseOptional).toEqual( | ||
testCase.shouldUseApiTagsDecorator | ||
); | ||
} | ||
); | ||
}); |
Oops, something went wrong.