From e11b8fe72db74ad376524fe004b6527293822290 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Thu, 17 Dec 2020 15:51:40 +0000 Subject: [PATCH] feat: update ts config and new linter (#697) - add "isolatedModules": true, - "importsNotUsedAsValues": "error", - "noImplicitAny": true, - add a must read section to the ts docs - self lint BREAKING CHANGE: tsconfig changed and linter has new rules for ts files. --- .github/workflows/main.yml | 9 +--- md/ts-jsdoc.md | 8 +++ package.json | 2 +- src/config/karma.conf.js | 5 +- src/config/tsconfig.aegir.json | 42 +++++++-------- src/config/webpack.config.js | 86 +++++++++++++++--------------- test/fixtures/js+ts/src/another.ts | 2 +- test/fixtures/js+ts/src/typed.ts | 2 +- tsconfig.json | 9 ++++ 9 files changed, 89 insertions(+), 76 deletions(-) create mode 100644 tsconfig.json diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 0241f6e2c..cf49108da 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -6,15 +6,10 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - - run: yarn - - name: lint - run: yarn lint + - run: npm install + - run: npm run lint - name: dependencies run: ./cli.js dep-check -- -i aegir - - uses: ./actions/bundle-size - name: size - with: - github_token: ${{ secrets.GITHUB_TOKEN }} test-node: needs: check runs-on: ${{ matrix.os }} diff --git a/md/ts-jsdoc.md b/md/ts-jsdoc.md index 57612cf61..8c4181587 100644 --- a/md/ts-jsdoc.md +++ b/md/ts-jsdoc.md @@ -48,6 +48,14 @@ When installing a dependency from a git url (ie. PRs depending on other PRs) the Typescript can infere lots of the types without any help, but you can improve your code types by using just JSDoc for that follow the official TS documentation https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html. +## Must read references +[10 Insights from Adopting TypeScript at Scale](https://www.techatbloomberg.com/blog/10-insights-adopting-typescript-at-scale/) +[Typescript official performance notes](https://github.com/microsoft/TypeScript/wiki/Performance) +[TypeScript: Don’t Export const enums](https://ncjamieson.com/dont-export-const-enums/) +[TypeScript: Prefer Interfaces](https://ncjamieson.com/prefer-interfaces/) + + + ### Rules for optimal type declarations and documentation This list is a WIP, more rules will be added as we identify them. diff --git a/package.json b/package.json index a4f059100..37269a553 100644 --- a/package.json +++ b/package.json @@ -76,7 +76,7 @@ "dirty-chai": "^2.0.1", "electron-mocha": "^10.0.0", "eslint": "^7.15.0", - "eslint-config-ipfs": "^0.1.0", + "eslint-config-ipfs": "^1.0.0", "execa": "^5.0.0", "extract-zip": "^2.0.1", "findup-sync": "^4.0.0", diff --git a/src/config/karma.conf.js b/src/config/karma.conf.js index f65697460..5f05ffdd1 100644 --- a/src/config/karma.conf.js +++ b/src/config/karma.conf.js @@ -2,6 +2,7 @@ const merge = require('webpack-merge') const webpack = require('webpack') +const path = require('path') const webpackConfig = require('./webpack.config') const { fromRoot, hasFile } = require('../utils') const userConfig = require('./user')() @@ -56,7 +57,7 @@ const karmaConfig = (config, argv) => { invert: argv.invert } - const karmaEntry = `${__dirname}/karma-entry.js` + const karmaEntry = path.join(__dirname, 'karma-entry.js') if (!files.length) { // only try to load *.spec.js if we aren't specifying custom files @@ -139,7 +140,7 @@ const karmaConfig = (config, argv) => { } module.exports = (config) => { - var argv = require('yargs-parser')(process.argv.slice(2), { + const argv = require('yargs-parser')(process.argv.slice(2), { array: ['files-custom'], boolean: ['progress', 'bail'], string: ['timeout'] diff --git a/src/config/tsconfig.aegir.json b/src/config/tsconfig.aegir.json index e67034b00..b22e5c9d9 100644 --- a/src/config/tsconfig.aegir.json +++ b/src/config/tsconfig.aegir.json @@ -1,37 +1,35 @@ { "compilerOptions": { + "strict": true, + // project options + "outDir": "dist", "allowJs": true, "checkJs": true, - "forceConsistentCasingInFileNames": true, - "noImplicitReturns": false, - "noImplicitAny": false, - "noImplicitThis": true, - "noFallthroughCasesInSwitch": true, - "noUnusedLocals": true, - "noUnusedParameters": false, - "strictFunctionTypes": true, - "strictNullChecks": true, - "strictPropertyInitialization": true, - "strictBindCallApply": true, - "strict": true, - "skipLibCheck": true, - "alwaysStrict": true, - "esModuleInterop": true, - "stripInternal": true, - "resolveJsonModule": true, - "preserveConstEnums": true, - "removeComments": false, "target": "ES2019", - "moduleResolution": "node", "lib": ["ES2019", "ES2020.Promise", "ES2020.String", "ES2020.BigInt", "DOM", "DOM.Iterable"], - "noEmitOnError": true, "noEmit": false, + "noEmitOnError": true, "emitDeclarationOnly": true, "declaration": true, "declarationMap": true, "incremental": true, "composite": true, - "outDir": "dist" + "isolatedModules": true, + "removeComments": false, + // module resolution + "esModuleInterop": true, + "moduleResolution": "node", + // linter checks + "noImplicitReturns": false, + "noFallthroughCasesInSwitch": true, + "noUnusedLocals": true, + "noUnusedParameters": false, + // advanced + "importsNotUsedAsValues": "error", + "forceConsistentCasingInFileNames": true, + "skipLibCheck": true, + "stripInternal": true, + "resolveJsonModule": true }, "include": ["src"] } diff --git a/src/config/webpack.config.js b/src/config/webpack.config.js index c3836ad58..64c161839 100644 --- a/src/config/webpack.config.js +++ b/src/config/webpack.config.js @@ -117,48 +117,50 @@ const base = (env, argv) => { }) ], target: 'web', - node: process.env.AEGIR_NODE === 'false' ? { - global: true, - __filename: 'mock', - __dirname: 'mock', - dgram: false, - fs: false, - net: false, - tls: false, - child_process: false, - console: false, - // TODO remove this once readable-stream is fixed probably on in v4 - // https://github.com/nodejs/readable-stream/pull/435 - process: true, - Buffer: false, - setImmediate: false, - os: false, - assert: false, - constants: false, - events: false, - http: false, - path: false, - querystring: false, - stream: false, - string_decoder: false, - timers: false, - url: false, - util: false, - crypto: false - } : { - dgram: 'empty', - fs: 'empty', - net: 'empty', - tls: 'empty', - child_process: 'empty', - console: false, - global: true, - process: true, - __filename: 'mock', - __dirname: 'mock', - Buffer: true, - setImmediate: true - }, + node: process.env.AEGIR_NODE === 'false' + ? { + global: true, + __filename: 'mock', + __dirname: 'mock', + dgram: false, + fs: false, + net: false, + tls: false, + child_process: false, + console: false, + // TODO remove this once readable-stream is fixed probably on in v4 + // https://github.com/nodejs/readable-stream/pull/435 + process: true, + Buffer: false, + setImmediate: false, + os: false, + assert: false, + constants: false, + events: false, + http: false, + path: false, + querystring: false, + stream: false, + string_decoder: false, + timers: false, + url: false, + util: false, + crypto: false + } + : { + dgram: 'empty', + fs: 'empty', + net: 'empty', + tls: 'empty', + child_process: 'empty', + console: false, + global: true, + process: true, + __filename: 'mock', + __dirname: 'mock', + Buffer: true, + setImmediate: true + }, performance: { hints: false }, diff --git a/test/fixtures/js+ts/src/another.ts b/test/fixtures/js+ts/src/another.ts index 71eac7af2..ac4b30ee6 100644 --- a/test/fixtures/js+ts/src/another.ts +++ b/test/fixtures/js+ts/src/another.ts @@ -1,5 +1,5 @@ import { hello } from './typed' -export const main = ():void => { +export const main = (): void => { hello('world') } diff --git a/test/fixtures/js+ts/src/typed.ts b/test/fixtures/js+ts/src/typed.ts index 88d36376c..0597ddce9 100644 --- a/test/fixtures/js+ts/src/typed.ts +++ b/test/fixtures/js+ts/src/typed.ts @@ -1,3 +1,3 @@ -export const hello = (name:string):string => { +export const hello = (name: string): string => { return `Hello ${name}` } diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 000000000..05efde215 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,9 @@ +{ + "extends": "./src/config/tsconfig.aegir.json", + "compilerOptions": { + "outDir": "dist" + }, + "include": [ + "src", "test" + ] +}