Skip to content

Commit

Permalink
Improve eslint config (#2042)
Browse files Browse the repository at this point in the history
- Get `.ts` files covered 🎉
- Update eslint related dependencies
- Add typescript-eslint [recommended rules](https://github.com/typescript-eslint/typescript-eslint/blob/3e26ab684ae5642e9c50940bedd155e32e189900/packages/eslint-plugin/src/configs/recommended.json), but only enabled ones we already followed, commented ones causing errors.

Commented rules including:

```yaml
  # '@typescript-eslint/no-unused-vars': 1
  # '@typescript-eslint/indent': ['error', 2]
  # '@typescript-eslint/array-type': error
  # '@typescript-eslint/ban-types': error
  # '@typescript-eslint/explicit-member-accessibility': error
  # '@typescript-eslint/member-delimiter-style': error
  # '@typescript-eslint/no-angle-bracket-type-assertion': error
  # '@typescript-eslint/no-explicit-any': warn
  # '@typescript-eslint/no-object-literal-type-assertion': error
  # '@typescript-eslint/no-use-before-define': error
  # '@typescript-eslint/no-var-requires': error
  # '@typescript-eslint/prefer-interface': error
```

### TODO

Go through these commented rules one by one, enable & fix for it or confirm it's disabled. Since enabling some rules would cause massive code change (like `'@typescript-eslint/indent': ['error', 2]`), which might conflict with our ongoing development, we better do this in separated PRs.
  • Loading branch information
amio authored and leo committed Apr 11, 2019
1 parent 1d52502 commit 3cc3b17
Show file tree
Hide file tree
Showing 36 changed files with 578 additions and 562 deletions.
6 changes: 6 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@types
download
dist
test/fixtures
bin
link
86 changes: 86 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
module.exports = {
'extends': [
'airbnb',
'prettier'
],
'parser': '@typescript-eslint/parser',
'parserOptions': {
'ecmaVersion': 2018,
'sourceType': 'module',
'modules': true
},
'plugins': [
'@typescript-eslint'
],
'settings': {
'import/resolver': {
'typescript': {
}
}
},
'rules': {
'quotes': [
2,
'single',
{
'allowTemplateLiterals': true
}
],
'class-methods-use-this': 0,
'consistent-return': 0,
'func-names': 0,
'global-require': 0,
'guard-for-in': 0,
'import/no-duplicates': 0,
'import/no-dynamic-require': 0,
'import/no-extraneous-dependencies': 0,
'import/prefer-default-export': 0,
'lines-between-class-members': 0,
'no-await-in-loop': 0,
'no-bitwise': 0,
'no-console': 0,
'no-continue': 0,
'no-control-regex': 0,
'no-empty': 0,
'no-loop-func': 0,
'no-nested-ternary': 0,
'no-param-reassign': 0,
'no-plusplus': 0,
'no-restricted-globals': 0,
'no-restricted-syntax': 0,
'no-shadow': 0,
'no-underscore-dangle': 0,
'no-use-before-define': 0,
'prefer-const': 0,
'prefer-destructuring': 0,
'camelcase': 0,
'no-unused-vars': 0, // in favor of '@typescript-eslint/no-unused-vars'
// 'indent': 0 // in favor of '@typescript-eslint/indent'
// '@typescript-eslint/no-unused-vars': 'error', // this might conflict with a lot ongoing changes
// '@typescript-eslint/indent': ['error', 2] // this might conflict with a lot ongoing changes
'@typescript-eslint/no-array-constructor': 'error',
'@typescript-eslint/adjacent-overload-signatures': 'error',
'@typescript-eslint/class-name-casing': 'error',
'@typescript-eslint/interface-name-prefix': 'error',
'@typescript-eslint/no-empty-interface': 'error',
'@typescript-eslint/no-inferrable-types': 'error',
'@typescript-eslint/no-misused-new': 'error',
'@typescript-eslint/no-namespace': 'error',
'@typescript-eslint/no-non-null-assertion': 'error',
'@typescript-eslint/no-parameter-properties': 'error',
'@typescript-eslint/no-triple-slash-reference': 'error',
'@typescript-eslint/prefer-namespace-keyword': 'error',
'@typescript-eslint/type-annotation-spacing': 'error',
// '@typescript-eslint/array-type': 'error',
// '@typescript-eslint/ban-types': 'error',
// '@typescript-eslint/explicit-function-return-type': 'warn',
// '@typescript-eslint/explicit-member-accessibility': 'error',
// '@typescript-eslint/member-delimiter-style': 'error',
// '@typescript-eslint/no-angle-bracket-type-assertion': 'error',
// '@typescript-eslint/no-explicit-any': 'warn',
// '@typescript-eslint/no-object-literal-type-assertion': 'error',
// '@typescript-eslint/no-use-before-define': 'error',
// '@typescript-eslint/no-var-requires': 'error',
// '@typescript-eslint/prefer-interface': 'error'
}
}
68 changes: 7 additions & 61 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"test": "yarn test-lint",
"test-unit": "nyc ava test/*unit.js --fail-fast",
"test-integration": "ava test/integration.js --serial --fail-fast",
"test-lint": "eslint .",
"test-lint": "eslint . --ext .js,.ts",
"prepublishOnly": "yarn build-download && cp download/default.js download/dist/now",
"coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov",
"postinstall": "node download/install.js",
Expand Down Expand Up @@ -68,60 +68,6 @@
"node10-win-x64"
]
},
"eslintConfig": {
"extends": [
"airbnb",
"prettier"
],
"parser": "@typescript-eslint/parser",
"plugins": [
"typescript"
],
"settings": {
"import/resolver": {
"typescript": {}
}
},
"rules": {
"camelcase": 0,
"class-methods-use-this": 0,
"consistent-return": 0,
"func-names": 0,
"global-require": 0,
"guard-for-in": 0,
"import/no-duplicates": 0,
"import/no-dynamic-require": 0,
"import/no-extraneous-dependencies": 0,
"import/prefer-default-export": 0,
"lines-between-class-members": 0,
"no-await-in-loop": 0,
"no-bitwise": 0,
"no-console": 0,
"no-continue": 0,
"no-control-regex": 0,
"no-empty": 0,
"no-loop-func": 0,
"no-nested-ternary": 0,
"no-param-reassign": 0,
"no-plusplus": 0,
"no-restricted-globals": 0,
"no-restricted-syntax": 0,
"no-shadow": 0,
"no-underscore-dangle": 0,
"no-use-before-define": 0,
"prefer-const": 0,
"prefer-destructuring": 0,
"typescript/no-unused-vars": "error"
}
},
"eslintIgnore": [
"@types",
"download",
"dist",
"test/fixtures",
"bin",
"link"
],
"ava": {
"compileEnhancements": false,
"extensions": [
Expand Down Expand Up @@ -163,6 +109,7 @@
"@types/tar-fs": "1.16.1",
"@types/text-table": "0.2.0",
"@types/write-json-file": "2.2.1",
"@typescript-eslint/eslint-plugin": "1.6.0",
"@typescript-eslint/parser": "1.1.0",
"@zeit/dockerignore": "0.0.4",
"@zeit/fun": "0.6.0",
Expand Down Expand Up @@ -194,14 +141,13 @@
"email-prompt": "0.3.2",
"email-validator": "1.1.1",
"epipebomb": "1.0.0",
"eslint": "5.3.0",
"eslint": "5.16.0",
"eslint-config-airbnb": "17.1.0",
"eslint-config-prettier": "3.3.0",
"eslint-config-prettier": "4.1.0",
"eslint-import-resolver-typescript": "1.1.1",
"eslint-plugin-import": "2.14.0",
"eslint-plugin-jsx-a11y": "6.1.1",
"eslint-plugin-react": "7.11.0",
"eslint-plugin-typescript": "0.14.0",
"eslint-plugin-import": "2.16.0",
"eslint-plugin-jsx-a11y": "6.2.1",
"eslint-plugin-react": "7.12.4",
"esm": "3.0.84",
"execa": "0.9.0",
"fetch-h2": "2.0.3",
Expand Down
11 changes: 5 additions & 6 deletions src/commands/alias/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,12 @@ export default async function set(
);
if (handleResult === 1) {
return 1;
} else {
console.log(
`${chalk.cyan('> Success!')} ${chalk.bold(
`https://${handleResult.alias}`
)} now points to https://${deployment.url} ${setStamp()}`
);
}
console.log(
`${chalk.cyan('> Success!')} ${chalk.bold(
`https://${handleResult.alias}`
)} now points to https://${deployment.url} ${setStamp()}`
);
}

return 0;
Expand Down
2 changes: 1 addition & 1 deletion src/commands/deploy/legacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ async function sync({
// Read scale and fail if we have both regions and scale
if (regions.length > 0 && Object.keys(scaleFromConfig).length > 0) {
error(
"Can't set both `regions` and `scale` options simultaneously",
'Can\'t set both `regions` and `scale` options simultaneously',
'regions-and-scale-at-once'
);
await exit(1);
Expand Down
3 changes: 2 additions & 1 deletion src/commands/dev/lib/builder-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from '../../../util/errors-ts';

import * as _staticBuilder from './static-builder';

const staticBuilder: Builder = _staticBuilder;
staticBuilder[PACKAGE] = {
version: 'built-in'
Expand Down Expand Up @@ -96,7 +97,7 @@ export async function installBuilders(packages: string[]): Promise<void> {
for (const builderPkg of packages) {
const parsed = npa(builderPkg);
const name = parsed.name || builderPkg;
if (localBuilders.hasOwnProperty(name)) {
if (Object.hasOwnProperty.call(localBuilders, name)) {
continue;
}
const spec = parsed.rawSpec || parsed.fetchSpec || 'latest';
Expand Down
4 changes: 3 additions & 1 deletion src/commands/dev/lib/dev-builder.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* disable this rule _here_ to avoid conflict with ongoing changes */
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import bytes from 'bytes';
import chalk from 'chalk';
import { tmpdir } from 'os';
Expand Down Expand Up @@ -94,7 +96,7 @@ export async function executeBuild(
): Promise<void> {
const { buildConfig, buildEntry } = asset;
if (!buildConfig || !buildEntry) {
throw new Error("Asset has not been built yet, can't rebuild");
throw new Error('Asset has not been built yet, can\'t rebuild');
}
const { cwd, env } = devServer;
const entrypoint = relative(cwd, buildEntry.fsPath);
Expand Down
4 changes: 3 additions & 1 deletion src/commands/dev/lib/dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ export default class DevServer {
if (this.stopping) return;
this.stopping = true;
this.output.log(`Stopping ${chalk.bold('`now dev`')} server`);
/* eslint-disable-next-line array-callback-return */
const ops = Object.values(this.assets).map((asset: BuilderOutput) => {
if (asset.type === 'Lambda' && asset.fn) {
return asset.fn.destroy();
Expand Down Expand Up @@ -514,6 +515,7 @@ export default class DevServer {
}
}

/* eslint-disable no-case-declarations */
switch (asset.type) {
case 'FileFsRef':
this.setResponseHeaders(res, nowRequestId);
Expand Down Expand Up @@ -603,7 +605,7 @@ export default class DevServer {
'UNKNOWN_ASSET_TYPE',
`Don't know how to handle asset type: ${(asset as any).type}`
);
return;

}
};

Expand Down
5 changes: 3 additions & 2 deletions src/commands/dev/lib/glob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export async function glob(
? (val: string): boolean => ignore.ignores(val)
: () => false;
delete opts.ignore;
/* eslint-disable no-new */
// @ts-ignore
new GlobIgnore(
pattern,
Expand Down Expand Up @@ -154,14 +155,14 @@ export async function globBuilderInputs(

for (const relativePath of files) {
const fsPath = join(options.cwd || '/', relativePath);
let stat: Stats = options.statCache![fsPath] as Stats;
let stat: Stats = options.statCache[fsPath] as Stats;
if (!stat) {
throw new Error(
`statCache does not contain value for ${relativePath} (resolved to ${fsPath})`
);
}
if (stat.isFile()) {
const isSymlink = options.symlinks![fsPath];
const isSymlink = options.symlinks[fsPath];
if (isSymlink) {
stat = await lstat(fsPath);
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/dev/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import http from 'http';
import { Output } from '../../../util/output';
import { Lambda as FunLambda } from '@zeit/fun';
import { FileBlob, FileFsRef, Lambda } from '@now/build-utils';
import { Output } from '../../../util/output';

export const PACKAGE = Symbol('package.json');

Expand Down
2 changes: 1 addition & 1 deletion src/commands/domains/move.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export default async function move(
client,
context,
domainName,
matchId ? matchId : destination
matchId || destination
);
});
if (moveTokenResult instanceof ERRORS.DomainMoveConflict) {
Expand Down
4 changes: 1 addition & 3 deletions src/commands/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,13 @@ function prepareFolder(cwd: string, folder: string, force?: boolean) {
`Destination path "${chalk.bold(folder)}" already exists and is not an empty directory. You may use ${cmd('--force')} or ${cmd('--f')} to override it.`
);
}
} else {
if (dest !== cwd) {
} else if (dest !== cwd) {
try {
fs.mkdirSync(dest);
} catch (e) {
throw new Error(`Could not create directory "${chalk.bold(folder)}".`);
}
}
}

return dest;
}
Expand Down
2 changes: 1 addition & 1 deletion src/util/alias/deployment-should-downscale.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import deploymentIsAliased from './deployment-is-aliased';
import { Output } from '../output';
import Client from '../client';
import getScaleForDC from '../../util/scale/get-scale-for-dc';
import getScaleForDC from '../scale/get-scale-for-dc';
import { Deployment } from '../../types';

export default async function deploymentShouldDownscale(
Expand Down
4 changes: 2 additions & 2 deletions src/util/alias/get-deployment-for-alias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import chalk from 'chalk';

import getAppLastDeployment from '../deploy/get-app-last-deployment';
import getAppName from '../deploy/get-app-name';
import fetchDeploymentByIdOrHost from '../../util/deploy/get-deployment-by-id-or-host';
import wait from '../../util/output/wait';
import fetchDeploymentByIdOrHost from '../deploy/get-deployment-by-id-or-host';
import wait from '../output/wait';
import Client from '../client';
import { Output } from '../output';
import { User, Config } from '../../types';
Expand Down
4 changes: 2 additions & 2 deletions src/util/alias/get-inferred-targets.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Output } from '../output';
import * as ERRORS from '../../util/errors-ts';
import * as ERRORS from '../errors-ts';
import { Config } from '../../types';
import cmd from '../../util/output/cmd';
import cmd from '../output/cmd';

export default async function getInferredTargets(
output: Output,
Expand Down
6 changes: 3 additions & 3 deletions src/util/alias/get-rules-from-file.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'path';
import * as ERRORS from '../../util/errors-ts';
import humanizePath from '../../util/humanize-path';
import readJSONFile from '../../util/read-json-file';
import * as ERRORS from '../errors-ts';
import humanizePath from '../humanize-path';
import readJSONFile from '../read-json-file';
import validatePathAliasRules from './validate-path-alias-rules';
import { PathRule } from '../../types';

Expand Down
2 changes: 1 addition & 1 deletion src/util/alias/get-targets-for-alias.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NowError } from '../now-error';
import { Output } from '../output';
import getInferredTargets from './get-inferred-targets';
import toHost from '../../util/to-host';
import toHost from '../to-host';
import { Config } from '../../types';

export default async function getTargetsForAlias(
Expand Down
2 changes: 1 addition & 1 deletion src/util/alias/validate-path-alias-rules.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RulesFileValidationError } from '../../util/errors-ts';
import { RulesFileValidationError } from '../errors-ts';
import { PathRule } from '../../types';

export default function validatePathAliasRules(
Expand Down

0 comments on commit 3cc3b17

Please sign in to comment.