Skip to content

feat(eslint-plugin): Regenerate recommended config #354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
module.exports = {
root: true,
plugins: ['eslint-plugin', '@typescript-eslint', 'jest'],
env: {
es6: true,
node: true,
},
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
rules: {
'comma-dangle': ['error', 'always-multiline'],
'no-mixed-operators': 'error',
'no-console': 'off',
'no-undef': 'off',

// TODO - enable these rules as we clean up the codebase
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-member-accessibility': 'off',
'@typescript-eslint/indent': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-object-literal-type-assertion': 'off',
'@typescript-eslint/no-parameter-properties': 'off',
'@typescript-eslint/no-use-before-define': 'off',
'@typescript-eslint/no-var-requires': 'off',
},
parserOptions: {
sourceType: 'module',
ecmaFeatures: {
jsx: false,
},
// TODO - when we enable rules with typechecking
// project: [
// './packages/eslint-plugin/tsconfig.json',
// './packages/eslint-plugin-tslint/tsconfig.json',
// './packages/parser/tsconfig.json',
// './packages/typescript-estree/tsconfig.json',
// ],
},
overrides: [
{
files: [
'packages/eslint-plugin-tslint/tests/**/*.ts',
'packages/eslint-plugin/tests/**/*.test.ts',
'packages/parser/tests/**/*.ts',
'packages/typescript-estree/tests/**/*.ts',
],
env: {
'jest/globals': true,
},
rules: {
'jest/no-disabled-tests': 'warn',
'jest/no-focused-tests': 'error',
'jest/no-alias-methods': 'error',
'jest/no-identical-title': 'error',
'jest/no-jasmine-globals': 'error',
'jest/no-jest-import': 'error',
'jest/no-test-prefixes': 'error',
'jest/no-test-callback': 'error',
'jest/no-test-return-statement': 'error',
'jest/prefer-to-have-length': 'warn',
'jest/prefer-spy-on': 'error',
'jest/valid-expect': 'error',
},
},
{
files: [
'packages/eslint-plugin/test/**/*.ts',
'packages/eslint-plugin-tslint/tests/**/*.spec.ts',
],
rules: {
'eslint-plugin/no-identical-tests': 'error',
},
},
],
};
66 changes: 0 additions & 66 deletions .eslintrc.json

This file was deleted.

2 changes: 1 addition & 1 deletion packages/eslint-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"docs": "eslint-docs",
"docs:check": "eslint-docs check",
"test": "jest --coverage",
"recommended:update": "ts-node tools/update-recommended.ts",
"recommended:update": "ts-node --files tools/update-recommended.ts",
"prebuild": "npm run clean",
"build": "tsc -p tsconfig.build.json",
"clean": "rimraf dist/",
Expand Down
2 changes: 2 additions & 0 deletions packages/eslint-plugin/src/configs/recommended.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"rules": {
"@typescript-eslint/adjacent-overload-signatures": "error",
"@typescript-eslint/array-type": "error",
"@typescript-eslint/ban-ts-ignore": "error",
"@typescript-eslint/ban-types": "error",
"camelcase": "off",
"@typescript-eslint/camelcase": "error",
Expand All @@ -28,6 +29,7 @@
"@typescript-eslint/no-non-null-assertion": "error",
"@typescript-eslint/no-object-literal-type-assertion": "error",
"@typescript-eslint/no-parameter-properties": "error",
"@typescript-eslint/no-require-imports": "error",
"@typescript-eslint/no-triple-slash-reference": "error",
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": "warn",
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/rules/promise-function-async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default util.createRule<Options, MessageIds>({
'Requires any function or method that returns a Promise to be marked async.',
tslintName: 'promise-function-async',
category: 'Best Practices',
recommended: 'error',
recommended: false,
},
messages: {
missingAsync: 'Functions that return promises must be async.',
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin/src/util/createRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import RuleModule, {
import { applyDefault } from './applyDefault';

// note - cannot migrate this to an import statement because it will make TSC copy the package.json to the dist folder
// eslint-disable-next-line @typescript-eslint/no-require-imports
const version = require('../../package.json').version;

// Utility type to remove a list of properties from an object
Expand Down
4 changes: 3 additions & 1 deletion packages/eslint-plugin/src/util/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ export function getNameFromClassMember(
return getNameFromPropertyName(methodDefinition.key);
}

return sourceCode.text.slice(...methodDefinition.key.range);
return sourceCode.text.slice(
...(methodDefinition.key as TSESTree.Expression).range,
);
}

/**
Expand Down
34 changes: 17 additions & 17 deletions packages/eslint-plugin/tools/update-recommended.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import path from 'path';
import fs from 'fs';
import requireIndex from 'requireindex';
import RuleModule from 'ts-eslint';

const bannedRecommendedRules = new Set([
'camelcase',
'indent',
'no-array-constructor',
'no-unused-vars',
]);
const MAX_RULE_NAME_LENGTH = 32 + 'typescript/'.length;
const MAX_RULE_NAME_LENGTH = 40 + 'typescript/'.length;

function padEnd(str: string, length: number): string {
while (str.length < length) {
Expand All @@ -24,29 +25,28 @@ function padEnd(str: string, length: number): string {
*/
function generate(): void {
// replace this with Object.entries when node > 8
const allRules = requireIndex(path.resolve(__dirname, '../dist/lib/rules'));
const allRules: Record<
string,
{ default: RuleModule<any, any> }
> = requireIndex(path.resolve(__dirname, '../dist/rules'));

const rules = Object.keys(allRules)
.filter(key => !!allRules[key].meta.docs.recommended)
.reduce<Record<string, string>>((config, key) => {
.map(key => ({ ...allRules[key].default, name: key }))
.filter(rule => {
return !!rule.meta.docs.recommended;
})
.reduce<Record<string, string>>((config, rule) => {
// having this here is just for output niceness (the keys will be ordered)
if (bannedRecommendedRules.has(key)) {
console.log(padEnd(key, MAX_RULE_NAME_LENGTH), '= off');
config[key] = 'off';
if (bannedRecommendedRules.has(rule.name)) {
console.log(padEnd(rule.name, MAX_RULE_NAME_LENGTH), '= off');
config[rule.name] = 'off';
}

const ruleName = `@typescript-eslint/${key}`;
const setting = allRules[key].meta.docs.recommended;
const ruleName = `@typescript-eslint/${rule.name}`;
const setting = rule.meta.docs.recommended as string;

if (!['error', 'warn'].includes(setting)) {
console.log(`ERR! Invalid level for rule ${key}: "${setting}"`);
// Don't want to throw an error since ^ explains what happened.
// eslint-disable-next-line no-process-exit
process.exit(1);
}

console.log(padEnd(ruleName, MAX_RULE_NAME_LENGTH), '=', setting);
config[ruleName] = setting;
console.log(padEnd(ruleName, MAX_RULE_NAME_LENGTH), '=', setting);

return config;
}, {});
Expand Down
2 changes: 0 additions & 2 deletions packages/parser/src/analyze-scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ class Referencer extends OriginalReferencer<ScopeManager> {
visitor.visit(node);

if (options.processRightHandNodes) {
// @ts-ignore
visitor.rightHandNodes.forEach(this.visit, this);
}
}
Expand Down Expand Up @@ -151,7 +150,6 @@ class Referencer extends OriginalReferencer<ScopeManager> {
const def = defs[i];
if (
def.type === 'FunctionName' &&
// @ts-ignore
def.node.type === 'TSDeclareFunction'
) {
defs.splice(i, 1);
Expand Down
1 change: 1 addition & 0 deletions packages/parser/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ParserOptions } from './parser-options';
import { visitorKeys } from './visitor-keys';

// note - cannot migrate this to an import statement because it will make TSC copy the package.json to the dist folder
// eslint-disable-next-line @typescript-eslint/no-require-imports
const packageJSON = require('../package.json');

interface ParseForESLintResult {
Expand Down
2 changes: 2 additions & 0 deletions packages/typescript-estree/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ export interface ParseAndGenerateServicesResult<T extends ParserOptions> {
// Public
//------------------------------------------------------------------------------

// note - cannot migrate this to an import statement because it will make TSC copy the package.json to the dist folder
// eslint-disable-next-line @typescript-eslint/no-require-imports
export const version: string = require('../package.json').version;

export function parse<T extends ParserOptions = ParserOptions>(
Expand Down
1 change: 1 addition & 0 deletions packages/typescript-estree/tests/ast-alignment/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function createError(message: string, line: number, column: number) {
}

function parseWithBabelParser(text: string, jsx: boolean = true) {
// eslint-disable-next-line @typescript-eslint/no-require-imports
const babel = require('@babel/parser');
const plugins: ParserPlugin[] = [
'typescript',
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/utils/generate-package-json.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable @typescript-eslint/no-require-imports */

const fs = require('fs');
const rootPackageJSON = require('/usr/root-package.json');

Expand Down