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

[PSC-2029] Add lint rule to prevent trailing forward slashes #2154

Merged
merged 3 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion cli/src/commands/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export interface LintConfig {

const lintConfig: LintConfig = {
rules: {
"no-omittable-fields-within-response-bodies": "warn"
"no-omittable-fields-within-response-bodies": "warn",
"no-trailing-forward-slash": "warn"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the default configuration used by the lint command. It can be overriden via command-line arguments since #2153

}
};

Expand Down
4 changes: 3 additions & 1 deletion lib/src/linting/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { noInlineObjectsWithinUnions } from "./rules/no-inline-objects-within-un
import { noNullableArrays } from "./rules/no-nullable-arrays";
import { noNullableFieldsWithinRequestBodies } from "./rules/no-nullable-fields-within-request-bodies";
import { noOmittableFieldsWithinResponseBodies } from "./rules/no-omittable-fields-within-response-bodies";
import { noTrailingForwardSlash } from "./rules/no-trailing-forward-slash";

export const availableRules: LintingRules = {
"has-discriminator": hasDiscriminator,
Expand All @@ -18,7 +19,8 @@ export const availableRules: LintingRules = {
"no-nullable-fields-within-request-bodies":
noNullableFieldsWithinRequestBodies,
"no-omittable-fields-within-response-bodies":
noOmittableFieldsWithinResponseBodies
noOmittableFieldsWithinResponseBodies,
"no-trailing-forward-slash": noTrailingForwardSlash
};

interface LintingRules {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { api, body, endpoint, response, String } from "@airtasker/spot";

@api({ name: "contract" })
class Contract {}

@endpoint({
method: "GET",
path: "/users"
})
class Endpoint {
@response({ status: 200 })
successResponse(@body body: Body) {}
}

interface Body {
body: String;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { api, body, endpoint, response, String } from "@airtasker/spot";

@api({ name: "contract" })
class Contract {}

@endpoint({
method: "GET",
path: "/users/"
})
class Endpoint {
@response({ status: 200 })
successResponse(@body body: Body) {}
}

interface Body {
body: String;
}
25 changes: 25 additions & 0 deletions lib/src/linting/rules/no-trailing-forward-slash.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { parseContract } from "../../parsers/contract-parser";
import { createProjectFromExistingSourceFile } from "../../spec-helpers/helper";
import { noTrailingForwardSlash } from "./no-trailing-forward-slash";

describe("no-trailing-forward-slash linter rule", () => {
test("returns no violations for contract not containing a trailing forward slash", () => {
const file = createProjectFromExistingSourceFile(
`${__dirname}/__spec-examples__/no-trailing-forward-slash/no-trailing-forward-slash.ts`
).file;

const { contract } = parseContract(file).unwrapOrThrow();

expect(noTrailingForwardSlash(contract)).toHaveLength(0);
});

test("returns a violation for contract containing a trailing forward slash", () => {
const file = createProjectFromExistingSourceFile(
`${__dirname}/__spec-examples__/no-trailing-forward-slash/trailing-forward-slash.ts`
).file;

const { contract } = parseContract(file).unwrapOrThrow();

expect(noTrailingForwardSlash(contract)).toHaveLength(1);
});
});
25 changes: 25 additions & 0 deletions lib/src/linting/rules/no-trailing-forward-slash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Contract } from "../../definitions";
import { LintingRuleViolation } from "../rule";

/**
* Request types should always be object types
*
* @param contract a contract
*/
export function noTrailingForwardSlash(
contract: Contract
): LintingRuleViolation[] {
const violations: LintingRuleViolation[] = [];

contract.endpoints.forEach(endpoint => {
const { path } = endpoint;

if (path.match(/\/$/)) {
violations.push({
message: `Endpoint (${endpoint.name} ${path}) contains a trailing forward slash`
});
}
});

return violations;
}