Skip to content

Commit

Permalink
[eslint-config-universe] Add more lint rules for TypeScript (#6847)
Browse files Browse the repository at this point in the history
* [eslint-config-universe] Add more lint rules for TypeScript

* expotools check-packages --no-build --no-test --fix-lint

* expotools check-packages
  • Loading branch information
wschurman authored and EvanBacon committed Feb 13, 2020
1 parent ac77c9e commit 70918b0
Show file tree
Hide file tree
Showing 101 changed files with 679 additions and 189 deletions.
31 changes: 31 additions & 0 deletions packages/eslint-config-universe/README.md
Expand Up @@ -74,6 +74,37 @@ You also can extend multiple configs, which is useful for projects that span sev
}
```

## Optional Additional TypeScript Typed Linting

Universe also provides optional additional config for typescript-eslint rules that make use of the parsed type information. Note that this may increase the time it takes to run lint for large projects. More information can be found at https://github.com/typescript-eslint/typescript-eslint/blob/master/docs/getting-started/linting/TYPED_LINTING.md

To enable the additional config, the following changes to your config are required:

### .eslintrc.js

```diff
module.exports = {
extends: [
'universe',
+ 'universe/shared/typescript-parsed-linting',
],
+ overrides: [
+ {
+ files: [
+ '*.ts',
+ '*.tsx',
+ '*.d.ts'
+ ],
+ parserOptions: {
+ project: './tsconfig.json'
+ },
+ },
+ ],
};
```

More info on parserOptions.project can be found at https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/parser#parseroptionsproject

## Philosophy

This config is designed to mark severe problems (ex: syntax errors) as errors and stylistic issues as warnings. This lets your team apply policies like, "make sure a commit has no errors but ignore warnings if the commit didn't introduce them."
Expand Down
Expand Up @@ -143,3 +143,58 @@ function tsUnused2() {
"warningCount": 5,
}
`;
exports[`lints with the default config: fixtures/all-04.ts 1`] = `
Object {
"errorCount": 0,
"fixableErrorCount": 0,
"fixableWarningCount": 0,
"messages": Array [],
"output": "export const blah = (): string[] => {
return ['1'];
};
",
"warningCount": 0,
}
`;
exports[`lints with the default config: fixtures/all-05.ts 1`] = `
Object {
"errorCount": 0,
"fixableErrorCount": 0,
"fixableWarningCount": 0,
"messages": Array [
Object {
"column": 21,
"endColumn": 23,
"endLine": 1,
"line": 1,
"message": "Forbidden extra non-null assertion.",
"messageId": "noExtraNonNullAssertion",
"nodeType": "TSNonNullExpression",
"ruleId": "@typescript-eslint/no-extra-non-null-assertion",
"severity": 1,
},
],
"output": "export const blah = 3!!;
",
"warningCount": 1,
}
`;
exports[`lints with the default config: fixtures/all-06.ts 1`] = `
Object {
"errorCount": 0,
"fixableErrorCount": 0,
"fixableWarningCount": 0,
"messages": Array [],
"output": "export const numberTest: number | null = null;
export const booleanTest: boolean | null = null;
export const symbolTest: symbol | null = null;
export const objectTest: object | null = null;
export const stringTest: string | null = null;
export const objectShorthandTest: object | null = null;
",
"warningCount": 0,
}
`;
Expand Up @@ -144,6 +144,61 @@ function tsUnused2() {
}
`;
exports[`lints with the React Native config: fixtures/all-04.ts 1`] = `
Object {
"errorCount": 0,
"fixableErrorCount": 0,
"fixableWarningCount": 0,
"messages": Array [],
"output": "export const blah = (): string[] => {
return ['1'];
};
",
"warningCount": 0,
}
`;
exports[`lints with the React Native config: fixtures/all-05.ts 1`] = `
Object {
"errorCount": 0,
"fixableErrorCount": 0,
"fixableWarningCount": 0,
"messages": Array [
Object {
"column": 21,
"endColumn": 23,
"endLine": 1,
"line": 1,
"message": "Forbidden extra non-null assertion.",
"messageId": "noExtraNonNullAssertion",
"nodeType": "TSNonNullExpression",
"ruleId": "@typescript-eslint/no-extra-non-null-assertion",
"severity": 1,
},
],
"output": "export const blah = 3!!;
",
"warningCount": 1,
}
`;
exports[`lints with the React Native config: fixtures/all-06.ts 1`] = `
Object {
"errorCount": 0,
"fixableErrorCount": 0,
"fixableWarningCount": 0,
"messages": Array [],
"output": "export const numberTest: number | null = null;
export const booleanTest: boolean | null = null;
export const symbolTest: symbol | null = null;
export const objectTest: object | null = null;
export const stringTest: string | null = null;
export const objectShorthandTest: object | null = null;
",
"warningCount": 0,
}
`;
exports[`lints with the React Native config: fixtures/web-native-00.js 1`] = `
Object {
"errorCount": 0,
Expand Down
Expand Up @@ -144,6 +144,61 @@ function tsUnused2() {
}
`;
exports[`lints with the Node config: fixtures/all-04.ts 1`] = `
Object {
"errorCount": 0,
"fixableErrorCount": 0,
"fixableWarningCount": 0,
"messages": Array [],
"output": "export const blah = (): string[] => {
return ['1'];
};
",
"warningCount": 0,
}
`;
exports[`lints with the Node config: fixtures/all-05.ts 1`] = `
Object {
"errorCount": 0,
"fixableErrorCount": 0,
"fixableWarningCount": 0,
"messages": Array [
Object {
"column": 21,
"endColumn": 23,
"endLine": 1,
"line": 1,
"message": "Forbidden extra non-null assertion.",
"messageId": "noExtraNonNullAssertion",
"nodeType": "TSNonNullExpression",
"ruleId": "@typescript-eslint/no-extra-non-null-assertion",
"severity": 1,
},
],
"output": "export const blah = 3!!;
",
"warningCount": 1,
}
`;
exports[`lints with the Node config: fixtures/all-06.ts 1`] = `
Object {
"errorCount": 0,
"fixableErrorCount": 0,
"fixableWarningCount": 0,
"messages": Array [],
"output": "export const numberTest: number | null = null;
export const booleanTest: boolean | null = null;
export const symbolTest: symbol | null = null;
export const objectTest: object | null = null;
export const stringTest: string | null = null;
export const objectShorthandTest: object | null = null;
",
"warningCount": 0,
}
`;
exports[`lints with the Node config: fixtures/node-00.js 1`] = `
Object {
"errorCount": 0,
Expand Down
@@ -0,0 +1,92 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`lints: fixtures/typescript-analysis-00.ts 1`] = `
Object {
"errorCount": 0,
"fixableErrorCount": 0,
"fixableWarningCount": 0,
"messages": Array [],
"output": "type T = {
a?: {
b?: {
c: string;
method?: () => void;
};
};
};
export function myFunc(foo: T | null) {
return foo?.a?.b?.c;
}",
"warningCount": 0,
}
`;
exports[`lints: fixtures/typescript-analysis-01.ts 1`] = `
Object {
"errorCount": 1,
"fixableErrorCount": 0,
"fixableWarningCount": 0,
"messages": Array [
Object {
"column": 1,
"endColumn": 2,
"endLine": 6,
"line": 4,
"message": "For-in loops over arrays are forbidden. Use for-of or array.forEach instead.",
"messageId": "forInViolation",
"nodeType": "ForInStatement",
"ruleId": "@typescript-eslint/no-for-in-array",
"severity": 2,
},
],
"source": "export const arr = [1, 2, 3];
let sum = '';
for (const b in arr) {
sum += b;
}
export { sum };",
"warningCount": 0,
}
`;
exports[`lints: fixtures/typescript-analysis-02.ts 1`] = `
Object {
"errorCount": 0,
"fixableErrorCount": 0,
"fixableWarningCount": 0,
"messages": Array [],
"output": "export function getBlah(): string | null {
const a: string | null = Math.random() ? '' : null;
const b: string | null = Math.random() ? '' : null;
return a ?? b;
}",
"warningCount": 0,
}
`;
exports[`lints: fixtures/typescript-analysis-03.ts 1`] = `
Object {
"errorCount": 0,
"fixableErrorCount": 0,
"fixableWarningCount": 0,
"messages": Array [
Object {
"column": 9,
"endColumn": 10,
"endLine": 2,
"line": 2,
"message": "Expected an error object to be thrown.",
"messageId": "object",
"nodeType": "Literal",
"ruleId": "@typescript-eslint/no-throw-literal",
"severity": 1,
},
],
"source": "export function wat() {
throw 3;
}",
"warningCount": 1,
}
`;
Expand Up @@ -144,6 +144,61 @@ function tsUnused2() {
}
`;
exports[`lints with the web config: fixtures/all-04.ts 1`] = `
Object {
"errorCount": 0,
"fixableErrorCount": 0,
"fixableWarningCount": 0,
"messages": Array [],
"output": "export const blah = (): string[] => {
return ['1'];
};
",
"warningCount": 0,
}
`;
exports[`lints with the web config: fixtures/all-05.ts 1`] = `
Object {
"errorCount": 0,
"fixableErrorCount": 0,
"fixableWarningCount": 0,
"messages": Array [
Object {
"column": 21,
"endColumn": 23,
"endLine": 1,
"line": 1,
"message": "Forbidden extra non-null assertion.",
"messageId": "noExtraNonNullAssertion",
"nodeType": "TSNonNullExpression",
"ruleId": "@typescript-eslint/no-extra-non-null-assertion",
"severity": 1,
},
],
"output": "export const blah = 3!!;
",
"warningCount": 1,
}
`;
exports[`lints with the web config: fixtures/all-06.ts 1`] = `
Object {
"errorCount": 0,
"fixableErrorCount": 0,
"fixableWarningCount": 0,
"messages": Array [],
"output": "export const numberTest: number | null = null;
export const booleanTest: boolean | null = null;
export const symbolTest: symbol | null = null;
export const objectTest: object | null = null;
export const stringTest: string | null = null;
export const objectShorthandTest: object | null = null;
",
"warningCount": 0,
}
`;
exports[`lints with the web config: fixtures/web-native-00.js 1`] = `
Object {
"errorCount": 1,
Expand Down
3 changes: 3 additions & 0 deletions packages/eslint-config-universe/__tests__/fixtures/all-04.ts
@@ -0,0 +1,3 @@
export const blah = (): Array<string> => {
return ['1'];
};
@@ -0,0 +1 @@
export const blah = 3!!;
6 changes: 6 additions & 0 deletions packages/eslint-config-universe/__tests__/fixtures/all-06.ts
@@ -0,0 +1,6 @@
export const numberTest: Number | null = null;
export const booleanTest: Boolean | null = null;
export const symbolTest: Symbol | null = null;
export const objectTest: Object | null = null;
export const stringTest: String | null = null;
export const objectShorthandTest: {} | null = null;

0 comments on commit 70918b0

Please sign in to comment.