Skip to content

Commit b7a1312

Browse files
committed
feat(eslint): add jest and unicorn plugin
- adjust code according to eslint issues
1 parent 15a5c27 commit b7a1312

File tree

6 files changed

+63
-38
lines changed

6 files changed

+63
-38
lines changed

.eslintrc.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

.eslintrc.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"root": true,
3+
"parser": "@typescript-eslint/parser",
4+
"plugins": ["prettier", "jest", "unicorn", "@typescript-eslint"],
5+
"extends": [
6+
"plugin:@typescript-eslint/recommended",
7+
"plugin:jest/recommended",
8+
"plugin:unicorn/recommended",
9+
"plugin:prettier/recommended",
10+
"prettier/@typescript-eslint"
11+
],
12+
"parserOptions": {
13+
"ecmaVersion": 2020,
14+
"sourceType": "module",
15+
"ecmaFeatures": {}
16+
},
17+
"env": {
18+
"es2020": true,
19+
"es6": true,
20+
"browser": false,
21+
"jest": true
22+
},
23+
"rules": {
24+
"@typescript-eslint/explicit-module-boundary-types": "off",
25+
"sort-imports": [
26+
"error",
27+
{
28+
"ignoreCase": false,
29+
"ignoreDeclarationSort": true,
30+
"ignoreMemberSort": false,
31+
"memberSyntaxSortOrder": ["none", "all", "multiple", "single"]
32+
}
33+
]
34+
},
35+
"settings": {}
36+
}

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"jest": {
3030
"collectCoverage": true,
3131
"coveragePathIgnorePatterns": [
32-
"testUtils.ts",
32+
"test-utils.ts",
3333
"/node_modules/"
3434
],
3535
"coverageReporters": [
@@ -68,16 +68,18 @@
6868
"devDependencies": {
6969
"@types/jest": "26.0.3",
7070
"@types/mime": "2.0.2",
71-
"@zeit/ncc": "0.22.3",
7271
"@typescript-eslint/eslint-plugin": "3.4.0",
7372
"@typescript-eslint/parser": "3.4.0",
73+
"@zeit/ncc": "0.22.3",
7474
"eslint": "7.3.1",
7575
"eslint-config-prettier": "6.11.0",
76+
"eslint-plugin-jest": "23.17.1",
7677
"eslint-plugin-prettier": "3.1.4",
78+
"eslint-plugin-unicorn": "20.1.0",
7779
"husky": "4.2.5",
7880
"jest": "26.1.0",
79-
"prettier": "2.0.5",
8081
"mime": "2.4.6",
82+
"prettier": "2.0.5",
8183
"rimraf": "3.0.2",
8284
"ts-jest": "26.1.1",
8385
"ts-node": "8.10.2",

src/action.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { clearTestEnvironment, uploadReleaseAssetMock, setInput, setOutputMock } from './testUtils';
1+
import { clearTestEnvironment, setInput, setOutputMock, uploadReleaseAssetMock } from './test-utils';
22

33
import { run as testSubject } from './action';
44

@@ -10,12 +10,12 @@ describe('nodejs-upload-asset', () => {
1010

1111
it('no id provided', async () => {
1212
setInput('path', 'bar');
13-
return expect(testSubject()).rejects.toStrictEqual(Error('Input required and not supplied: id'));
13+
return expect(testSubject()).rejects.toStrictEqual(new Error('Input required and not supplied: id'));
1414
});
1515

1616
it('no path provided', async () => {
1717
setInput('id', '1');
18-
return expect(testSubject()).rejects.toStrictEqual(Error('Input required and not supplied: path'));
18+
return expect(testSubject()).rejects.toStrictEqual(new Error('Input required and not supplied: path'));
1919
});
2020

2121
it('missing GITHUB_TOKEN', async () => {

src/action.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as core from '@actions/core';
22
import { context, getOctokit } from '@actions/github';
3-
import { RestEndpointMethodTypes } from '@octokit/plugin-rest-endpoint-methods/dist-types/';
3+
import { RestEndpointMethodTypes } from '@octokit/plugin-rest-endpoint-methods/dist-types';
44

55
import * as fs from 'fs';
66
import { getType } from 'mime';
@@ -9,9 +9,9 @@ import { basename, resolve } from 'path';
99
type GitHub = ReturnType<typeof getOctokit>;
1010
type ReposUploadReleaseAssetParams = RestEndpointMethodTypes['repos']['uploadReleaseAsset']['parameters'];
1111

12-
const uploadAsset = async (client: GitHub, params: ReposUploadReleaseAssetParams) => {
13-
core.startGroup(`Uploading asset ${params.name} to release ${params.url}`);
14-
const response = await client.repos.uploadReleaseAsset(params);
12+
const uploadAsset = async (client: GitHub, parameters: ReposUploadReleaseAssetParams) => {
13+
core.startGroup(`Uploading asset ${parameters.name} to release ${parameters.url}`);
14+
const response = await client.repos.uploadReleaseAsset(parameters);
1515
core.info(`Release asset ${response.data.name} created [id: ${response.data.id}]`);
1616
core.endGroup();
1717
return response.data;
@@ -24,7 +24,7 @@ const prepareHeaders = (fullPathChecked: string, mime: string) => {
2424
};
2525
};
2626

27-
const prepareParams = (
27+
const prepareParameters = (
2828
data: string,
2929
headers: { 'content-length': number; 'content-type': string },
3030
label: string,
@@ -38,7 +38,7 @@ const prepareParams = (
3838
headers,
3939
label,
4040
name,
41-
release_id: parseInt(release_id),
41+
release_id: Number.parseInt(release_id),
4242
owner,
4343
repo,
4444
};
@@ -54,7 +54,7 @@ export const run = async (): Promise<void> => {
5454

5555
try {
5656
if (!process.env.GITHUB_TOKEN) {
57-
throw Error('Missing GITHUB_TOKEN');
57+
throw new Error('Missing GITHUB_TOKEN');
5858
}
5959

6060
const github = getOctokit(process.env.GITHUB_TOKEN);
@@ -64,7 +64,7 @@ export const run = async (): Promise<void> => {
6464

6565
const asset = await uploadAsset(
6666
github,
67-
prepareParams(data, headers, label, name, id, context.repo.owner, context.repo.repo),
67+
prepareParameters(data, headers, label, name, id, context.repo.owner, context.repo.repo),
6868
);
6969

7070
core.setOutput('id', asset.id.toString());

src/testUtils.ts renamed to src/test-utils.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const actionsCoreMock = jest.mock('@actions/core', () => {
2525
options.required &&
2626
!Object.keys(process.env).find((key) => `INPUT_${name.toUpperCase()}` === key)
2727
) {
28-
throw Error(`Input required and not supplied: ${name}`);
28+
throw new Error(`Input required and not supplied: ${name}`);
2929
}
3030
return process.env[`INPUT_${name.toUpperCase()}`];
3131
},
@@ -74,19 +74,19 @@ export const clearTestEnvironment = () => {
7474
};
7575

7676
expect.extend({
77-
// tslint:disable-next-line: object-literal-shorthand space-before-function-paren
77+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
7878
toHaveBeenCalledNthWith: function (recieved: jest.Mock, times: number, match: any) {
79-
const passTimes = recieved.mock.calls.length && recieved.mock.calls.length === times ? true : false;
79+
const passTimes = recieved.mock.calls.length > 0 && recieved.mock.calls.length === times ? true : false;
8080
const options = {
8181
comment: 'Error.message equality',
8282
isNot: this.isNot,
8383
promise: this.promise,
8484
};
85-
const callParams = recieved.mock.calls[0][0];
85+
const callParameters = recieved.mock.calls[0][0];
8686
const expectedKeys = Object.keys(match);
8787
const matchData = Object.create({});
8888
expectedKeys.forEach((key) => {
89-
matchData[key] = callParams[key];
89+
matchData[key] = callParameters[key];
9090
});
9191
const passDiff = expectedKeys.find((key) => {
9292
if (matchData[key] !== match[key]) {
@@ -124,9 +124,9 @@ expect.extend({
124124
pass,
125125
};
126126
},
127-
toHaveCoreError(recieved: jest.Mock, msg: RegExp) {
128-
const error = setFailedMock.mock.calls.length ? (setFailedMock.mock.calls[0][0] as Error) : undefined;
129-
const pass = error && error.message.match(msg) ? true : false;
127+
toHaveCoreError(recieved: jest.Mock, message: RegExp) {
128+
const error = setFailedMock.mock.calls.length > 0 ? (setFailedMock.mock.calls[0][0] as Error) : undefined;
129+
const pass = error && error.message.match(message) ? true : false;
130130
const options = {
131131
comment: 'Error.message equality',
132132
isNot: this.isNot,
@@ -136,12 +136,12 @@ expect.extend({
136136
return {
137137
message: () => {
138138
if (pass) {
139-
return this.utils.matcherHint('toHaveCoreError', error?.message, `${msg}`, options);
139+
return this.utils.matcherHint('toHaveCoreError', error?.message, `${message}`, options);
140140
} else {
141-
const diff = this.utils.diff(msg, error?.message, {
141+
const diff = this.utils.diff(message, error?.message, {
142142
expand: this.expand,
143143
});
144-
return this.utils.matcherHint('toHaveCoreError', error?.message, `${msg}`, options) + `\n\n${diff}`;
144+
return this.utils.matcherHint('toHaveCoreError', error?.message, `${message}`, options) + `\n\n${diff}`;
145145
}
146146
},
147147
pass,

0 commit comments

Comments
 (0)