Skip to content

Commit

Permalink
Add ESLint and Prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
mchudy committed Sep 15, 2020
1 parent ae5764b commit 03ff8e8
Show file tree
Hide file tree
Showing 16 changed files with 1,153 additions and 225 deletions.
40 changes: 40 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* eslint-env node */
module.exports = {
parser: "@typescript-eslint/parser",
plugins: ["@typescript-eslint", "prettier"],
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended",
"prettier/@typescript-eslint",
],
parserOptions: {
ecmaVersion: 2018,
sourceType: "module",
ecmaFeatures: {
jsx: true,
},
},
rules: {
"no-var": "off",
"no-console": ["warn", { allow: ["warn", "error", "assert"] }],
"max-params": ["error", { max: 4 }],

"prettier/prettier": "warn",

"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/prefer-interface": "off",
"@typescript-eslint/explicit-member-accessibility": "off",
"@typescript-eslint/no-parameter-properties": "off",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/no-use-before-define": "off",
"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/camelcase": "off",
"@typescript-eslint/no-empty-interface": "off",
"@typescript-eslint/no-inferrable-types": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/ban-types": "off",
},
};
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ jobs:
- uses: actions/checkout@v2
- name: Install modules
run: npm ci
- name: Run ESLint
run: npm run lint
- name: Run tests
run: npm test
- name: Build
Expand Down
18 changes: 18 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = {
printWidth: 120,
useTabs: false,
arrowParens: "avoid",
trailingComma: "all",
tabWidth: 4,
jsxBracketSameLine: true,
endOfLine: "lf",
overrides: [
{
files: ["*.json", ".prettierrc", ".eslintrc", ".stylelintrc", ".yml"],
options: {
tabWidth: 2,
},
},
],
proseWrap: "always",
};
92 changes: 46 additions & 46 deletions __tests__/routing.test.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
import { createRouting, number, segment } from '../src/index';

describe('createRouting', () => {
it('creates a simple route', () => {
const routes = createRouting({
products: segment`/products`,
} as const);

const route = routes.products();

expect(route).toEqual('/products');
});

it('creates nested routes', () => {
const routes = createRouting({
products: {
...segment`/products`,
children: {
create: segment`/create`,
},
},
} as const);

const mainRoute = routes.products();
const nestedRoute = routes.products.create();

expect(mainRoute).toEqual('/products');
expect(nestedRoute).toEqual('/products/create');
});

it('creates nested routes with params', () => {
const routes = createRouting({
products: {
...segment`/products/${number('productId')}`,
children: {
edit: segment`/edit`,
},
},
} as const);

const mainRoute = routes.products({ productId: '2' });
const nestedRoute = routes.products.edit({ productId: '2' });

expect(mainRoute).toEqual('/products/2');
expect(nestedRoute).toEqual('/products/2/edit');
});
import { createRouting, number, segment } from "../src/index";

describe("createRouting", () => {
it("creates a simple route", () => {
const routes = createRouting({
products: segment`/products`,
} as const);

const route = routes.products();

expect(route).toEqual("/products");
});

it("creates nested routes", () => {
const routes = createRouting({
products: {
...segment`/products`,
children: {
create: segment`/create`,
},
},
} as const);

const mainRoute = routes.products();
const nestedRoute = routes.products.create();

expect(mainRoute).toEqual("/products");
expect(nestedRoute).toEqual("/products/create");
});

it("creates nested routes with params", () => {
const routes = createRouting({
products: {
...segment`/products/${number("productId")}`,
children: {
edit: segment`/edit`,
},
},
} as const);

const mainRoute = routes.products({ productId: "2" });
const nestedRoute = routes.products.edit({ productId: "2" });

expect(mainRoute).toEqual("/products/2");
expect(nestedRoute).toEqual("/products/2/edit");
});
});
70 changes: 35 additions & 35 deletions __tests__/segments/arg.test.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
import { arg, createRouting, segment } from '../../src';
import { arg, createRouting, segment } from "../../src";

describe('arg segment', () => {
it('creates route with an arg segment', () => {
const routes = createRouting({
product: segment`/product/${arg('productId')}`,
} as const);
describe("arg segment", () => {
it("creates route with an arg segment", () => {
const routes = createRouting({
product: segment`/product/${arg("productId")}`,
} as const);

const route = routes.product({ productId: 'id' });
const route = routes.product({ productId: "id" });

expect(route).toEqual('/product/id');
});
expect(route).toEqual("/product/id");
});

it('creates route with an optional arg segment', () => {
const routes = createRouting({
product: segment`/product/${arg('productId', {
optional: true,
})}`,
} as const);
it("creates route with an optional arg segment", () => {
const routes = createRouting({
product: segment`/product/${arg("productId", {
optional: true,
})}`,
} as const);

const route = routes.product();
const route = routes.product();

expect(route).toEqual('/product');
});
expect(route).toEqual("/product");
});

it('creates route with a custom pattern param', () => {
const routes = createRouting({
product: segment`/product/${arg('productId', {
pattern: /[0-9]{2}/.source,
})}`,
} as const);
it("creates route with a custom pattern param", () => {
const routes = createRouting({
product: segment`/product/${arg("productId", {
pattern: /[0-9]{2}/.source,
})}`,
} as const);

const route = routes.product({ productId: '23' });
const route = routes.product({ productId: "23" });

expect(route).toEqual('/product/23');
});
expect(route).toEqual("/product/23");
});

it('route with a custom pattern param gets correctly validated', () => {
const routes = createRouting({
product: segment`/product/${arg('productId', {
pattern: /[0-9]{2}/.source,
})}`,
} as const);
it("route with a custom pattern param gets correctly validated", () => {
const routes = createRouting({
product: segment`/product/${arg("productId", {
pattern: /[0-9]{2}/.source,
})}`,
} as const);

expect(() => routes.product({ productId: '123' })).toThrow();
});
expect(() => routes.product({ productId: "123" })).toThrow();
});
});
44 changes: 22 additions & 22 deletions __tests__/segments/number.test.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import { createRouting, number, segment } from '../../src';
import { createRouting, number, segment } from "../../src";

describe('number segment', () => {
it('creates route with an optional number param', () => {
const routes = createRouting({
product: segment`/product/${number('productId', true)}`,
} as const);
describe("number segment", () => {
it("creates route with an optional number param", () => {
const routes = createRouting({
product: segment`/product/${number("productId", true)}`,
} as const);

const route = routes.product();
const route = routes.product();

expect(route).toEqual('/product');
});
expect(route).toEqual("/product");
});

it('creates route with a required number param', () => {
const routes = createRouting({
product: segment`/product/${number('productId')}`,
} as const);
it("creates route with a required number param", () => {
const routes = createRouting({
product: segment`/product/${number("productId")}`,
} as const);

const route = routes.product({ productId: '1' });
const route = routes.product({ productId: "1" });

expect(route).toEqual('/product/1');
});
expect(route).toEqual("/product/1");
});

it('throws when given an invalid number param', () => {
const routes = createRouting({
product: segment`/product/${number('productId')}`,
} as const);
it("throws when given an invalid number param", () => {
const routes = createRouting({
product: segment`/product/${number("productId")}`,
} as const);

expect(() => routes.product({ productId: 'aaa' })).toThrow();
});
expect(() => routes.product({ productId: "aaa" })).toThrow();
});
});
52 changes: 26 additions & 26 deletions __tests__/segments/query.test.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
import { createRouting, number, query, segment } from '../../src';
import { createRouting, query, segment } from "../../src";

describe('query segment', () => {
it('creates route with an optional query param', () => {
const routes = createRouting({
product: segment`/product${query({ productId: false })}`,
} as const);
describe("query segment", () => {
it("creates route with an optional query param", () => {
const routes = createRouting({
product: segment`/product${query({ productId: false })}`,
} as const);

const route = routes.product();
const route = routes.product();

expect(route).toEqual('/product');
});
expect(route).toEqual("/product");
});

it('creates route with a required query param', () => {
const routes = createRouting({
product: segment`/product${query({ productId: true })}`,
} as const);
it("creates route with a required query param", () => {
const routes = createRouting({
product: segment`/product${query({ productId: true })}`,
} as const);

const route = routes.product({}, { productId: '2' });
const route = routes.product({}, { productId: "2" });

expect(route).toEqual('/product?productId=2');
});
expect(route).toEqual("/product?productId=2");
});

it('creates route with multiple query params and they are sorted in alphabetical order', () => {
const routes = createRouting({
product: segment`/product${query({
productId: true,
details: true,
})}`,
} as const);
it("creates route with multiple query params and they are sorted in alphabetical order", () => {
const routes = createRouting({
product: segment`/product${query({
productId: true,
details: true,
})}`,
} as const);

const route = routes.product({}, { productId: '2', details: 'false' });
const route = routes.product({}, { productId: "2", details: "false" });

expect(route).toEqual(`/product?details=false&productId=2`);
});
expect(route).toEqual(`/product?details=false&productId=2`);
});
});
Loading

0 comments on commit 03ff8e8

Please sign in to comment.