Feature/lint (#22)

* eslint package install

* scrupt add .

* warning resolved

* warning resolced

* error  Async function 'requestGPIOAccess' has no 'await' expression resolved

* Promise returned in function argument where a void return was expected resolved

* package-lock.json 削除

* script improve

* temporary commit

* L234 disabled eslint rules

* L159 lint disabled

* レビュー指摘事項反映
This commit is contained in:
Akihiko.KIgure 2021-03-28 17:31:23 +09:00 committed by GitHub
parent 80411187ea
commit 13b6bc57b1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 8 deletions

24
.eslintrc.js Normal file
View file

@ -0,0 +1,24 @@
module.exports = {
root: true,
env: {
es6: true,
node: true,
},
parser: '@typescript-eslint/parser',
parserOptions: {
sourceType: 'module',
ecmaVersion: 2019, // Node.js 12の場合は2019、他のバージョンのNode.jsを利用している場合は場合は適宜変更する
tsconfigRootDir: __dirname,
project: ['./tsconfig.eslint.json']
},
plugins: [
'@typescript-eslint',
],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/recommended-requiring-type-checking',
],
rules: {
},
};

View file

@ -62,7 +62,7 @@ export class GPIOAccess extends EventEmitter {
/** /**
* Unexport all exported GPIO ports. * Unexport all exported GPIO ports.
*/ */
async unexportAll() { async unexportAll(): Promise<void> {
await Promise.all( await Promise.all(
[...this.ports.values()].map(port => [...this.ports.values()].map(port =>
port.exported ? port.unexport() : undefined port.exported ? port.unexport() : undefined
@ -121,7 +121,7 @@ export class GPIOPort extends EventEmitter {
return this._exported; return this._exported;
} }
async export(direction: DirectionMode) { async export(direction: DirectionMode): Promise<void> {
if (!/^(in|out)$/.test(direction)) { if (!/^(in|out)$/.test(direction)) {
throw new InvalidAccessError(`Must be "in" or "out".`); throw new InvalidAccessError(`Must be "in" or "out".`);
} }
@ -134,7 +134,7 @@ export class GPIOPort extends EventEmitter {
} }
try { try {
clearInterval(this._timeout as any); clearInterval(this._timeout as ReturnType<typeof setInterval>);
if (!this.exported) { if (!this.exported) {
await fs.writeFile( await fs.writeFile(
path.join(SysfsGPIOPath, "export"), path.join(SysfsGPIOPath, "export"),
@ -147,6 +147,7 @@ export class GPIOPort extends EventEmitter {
); );
if (direction === "in") { if (direction === "in") {
this._timeout = setInterval( this._timeout = setInterval(
// eslint-disable-next-line
this.read.bind(this), this.read.bind(this),
this._pollingInterval this._pollingInterval
); );
@ -159,8 +160,8 @@ export class GPIOPort extends EventEmitter {
this._exported = true; this._exported = true;
} }
async unexport() { async unexport(): Promise<void> {
clearInterval(this._timeout as any); clearInterval(this._timeout as ReturnType<typeof setInterval>);
try { try {
await fs.writeFile( await fs.writeFile(
@ -174,7 +175,7 @@ export class GPIOPort extends EventEmitter {
this._exported = false; this._exported = false;
} }
async read() { async read(): Promise<GPIOValue> {
if (!(this.exported && this.direction === "in")) { if (!(this.exported && this.direction === "in")) {
throw new InvalidAccessError( throw new InvalidAccessError(
`The exported must be true and value of direction must be "in".` `The exported must be true and value of direction must be "in".`
@ -199,7 +200,7 @@ export class GPIOPort extends EventEmitter {
} }
} }
async write(value: GPIOValue) { async write(value: GPIOValue): Promise<void> {
if (!(this.exported && this.direction === "out")) { if (!(this.exported && this.direction === "out")) {
throw new InvalidAccessError( throw new InvalidAccessError(
`The exported must be true and value of direction must be "out".` `The exported must be true and value of direction must be "out".`
@ -231,6 +232,8 @@ export class OperationError extends Error {
} }
} }
// Web GPIOの仕様に基づく意図的なasync関数の使用なので、ルールを無効化
// eslint-disable-next-line
export async function requestGPIOAccess(): Promise<GPIOAccess> { export async function requestGPIOAccess(): Promise<GPIOAccess> {
const ports = new GPIOPortMap( const ports = new GPIOPortMap(
[...Array(GPIOPortMapSizeMax).keys()].map(portNumber => [ [...Array(GPIOPortMapSizeMax).keys()].map(portNumber => [

View file

@ -15,10 +15,14 @@
"license": "MIT", "license": "MIT",
"devDependencies": { "devDependencies": {
"@types/node": "^14.14.25", "@types/node": "^14.14.25",
"typescript": "^4.1.3" "@typescript-eslint/eslint-plugin": "^4.15.0",
"@typescript-eslint/parser": "^4.15.0",
"eslint": "^7.20.0",
"typescript": "^4.1.5"
}, },
"scripts": { "scripts": {
"build": "tsc", "build": "tsc",
"lint": "eslint index.ts",
"prepare": "rm -rf dist && npm run build" "prepare": "rm -rf dist && npm run build"
}, },
"keywords": [ "keywords": [

11
tsconfig.eslint.json Normal file
View file

@ -0,0 +1,11 @@
{
"extends": "./tsconfig.json",
"include": [
"*.ts",
".eslintrc.js"
],
"exclude": [
"node_modules",
"dist"
]
}