diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 0000000..cd41668 --- /dev/null +++ b/.eslintrc.js @@ -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: { + }, +}; \ No newline at end of file diff --git a/index.ts b/index.ts index 57c8c5f..f3b2755 100644 --- a/index.ts +++ b/index.ts @@ -62,7 +62,7 @@ export class GPIOAccess extends EventEmitter { /** * Unexport all exported GPIO ports. */ - async unexportAll() { + async unexportAll(): Promise { await Promise.all( [...this.ports.values()].map(port => port.exported ? port.unexport() : undefined @@ -121,7 +121,7 @@ export class GPIOPort extends EventEmitter { return this._exported; } - async export(direction: DirectionMode) { + async export(direction: DirectionMode): Promise { if (!/^(in|out)$/.test(direction)) { throw new InvalidAccessError(`Must be "in" or "out".`); } @@ -134,7 +134,7 @@ export class GPIOPort extends EventEmitter { } try { - clearInterval(this._timeout as any); + clearInterval(this._timeout as ReturnType); if (!this.exported) { await fs.writeFile( path.join(SysfsGPIOPath, "export"), @@ -147,6 +147,7 @@ export class GPIOPort extends EventEmitter { ); if (direction === "in") { this._timeout = setInterval( + // eslint-disable-next-line this.read.bind(this), this._pollingInterval ); @@ -159,8 +160,8 @@ export class GPIOPort extends EventEmitter { this._exported = true; } - async unexport() { - clearInterval(this._timeout as any); + async unexport(): Promise { + clearInterval(this._timeout as ReturnType); try { await fs.writeFile( @@ -174,7 +175,7 @@ export class GPIOPort extends EventEmitter { this._exported = false; } - async read() { + async read(): Promise { if (!(this.exported && this.direction === "in")) { throw new InvalidAccessError( `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 { if (!(this.exported && this.direction === "out")) { throw new InvalidAccessError( `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 { const ports = new GPIOPortMap( [...Array(GPIOPortMapSizeMax).keys()].map(portNumber => [ diff --git a/package.json b/package.json index 1ad2b38..c25c97d 100644 --- a/package.json +++ b/package.json @@ -15,10 +15,14 @@ "license": "MIT", "devDependencies": { "@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": { "build": "tsc", + "lint": "eslint index.ts", "prepare": "rm -rf dist && npm run build" }, "keywords": [ diff --git a/tsconfig.eslint.json b/tsconfig.eslint.json new file mode 100644 index 0000000..fb9dbd4 --- /dev/null +++ b/tsconfig.eslint.json @@ -0,0 +1,11 @@ +{ + "extends": "./tsconfig.json", + "include": [ + "*.ts", + ".eslintrc.js" + ], + "exclude": [ + "node_modules", + "dist" + ] +} \ No newline at end of file