Unverified Commit fc52e027 authored by Josh Goldberg ✨'s avatar Josh Goldberg ✨ Committed by GitHub

chore: enable typescript-eslint's recommended and stylistic configs internally (#52948)

Spinning out from #37151 and my draft PR #52845, this enables the two
basic recommended rulesets from
[typescript-eslint](https://typescript-eslint.io) for the Next.js
monorepo source code:

*
[`plugin:@typescript-eslint/recommended`](https://typescript-eslint.io/linting/configs#recommended):
Our base recommended rules that detect common bugs or _(non-stylistic)_
TypeScript bad practices
*
[`plugin:@typescript-eslint/stylistic`](https://typescript-eslint.io/linting/configs#stylistic):
Our base starting stylistic recommended for keeping codebases visually
consistent and avoiding out-of-practice visual constructs

The process I used is pretty standard (see
https://github.com/typescript-eslint/typescript-eslint/issues/6760 for
other repos it was done on):

1. Enable those base recommended presets
2. Remove any rule settings that are now redundant
3. Reconfigure any rule whose default settings didn't seem to make sense
for this codebase
4. Add a `// Todo: ...` comment, and under it, add a disable for any
rule that immediately reported a lot of complaints

Note that this only enables the presets internally. It doesn't impact
what end-users of published packages such as Next.js or
`create-next-app` experience. That's a separate task in #52845.

I also didn't fix any existing warning from the `canary` branch. Would
you like me to do that? My preference would be a separate PR to get it
in more quickly.

Any code changes are commented inline.

---------
Co-authored-by: 's avatarSteven <steven@ceriously.com>
parent 7182d4dd
...@@ -43,36 +43,51 @@ ...@@ -43,36 +43,51 @@
}, },
{ "files": ["**/__tests__/**"], "env": { "jest": true } }, { "files": ["**/__tests__/**"], "env": { "jest": true } },
{ {
"extends": [
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/stylistic"
],
"files": ["**/*.ts", "**/*.tsx"], "files": ["**/*.ts", "**/*.tsx"],
"parser": "@typescript-eslint/parser", "parser": "@typescript-eslint/parser",
"parserOptions": { "parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module", "sourceType": "module",
"ecmaFeatures": {
"jsx": true
},
"warnOnUnsupportedTypeScriptVersion": false "warnOnUnsupportedTypeScriptVersion": false
}, },
"plugins": ["@typescript-eslint"], "plugins": ["@typescript-eslint"],
"rules": { "rules": {
// Already handled by TS // Todo: investigate, for each of these rules, whether we want them.
"no-dupe-class-members": "off", "@typescript-eslint/array-type": "off",
"no-undef": "off", "@typescript-eslint/ban-ts-comment": "off",
"@typescript-eslint/ban-tslint-comment": "off",
// Add TypeScript specific rules (and turn off ESLint equivalents) "@typescript-eslint/ban-types": "off",
"@typescript-eslint/consistent-type-assertions": "warn", "@typescript-eslint/class-literal-property-style": "off",
"no-array-constructor": "off", "@typescript-eslint/consistent-generic-constructors": "off",
"@typescript-eslint/no-array-constructor": "warn", "@typescript-eslint/consistent-indexed-object-style": "off",
"@typescript-eslint/consistent-type-definitions": "off",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/no-namespace": "off", "@typescript-eslint/no-namespace": "off",
"no-use-before-define": "off", "@typescript-eslint/no-shadow": "off",
"@typescript-eslint/no-use-before-define": [ "@typescript-eslint/no-empty-interface": "off",
"warn", "@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-inferrable-types": "off",
"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/prefer-for-of": "off",
"@typescript-eslint/prefer-function-type": "off",
"@typescript-eslint/no-this-alias": "off",
"@typescript-eslint/triple-slash-reference": "off",
"no-var": "off",
"prefer-const": "off",
"prefer-rest-params": "off",
"prefer-spread": "off",
// These off- or differently-configured rules work well for us.
"no-unused-expressions": "off",
"@typescript-eslint/no-unused-expressions": [
"error",
{ {
"functions": true, "allowShortCircuit": true,
"classes": true, "allowTernary": true,
"variables": true, "allowTaggedTemplates": true
"enums": true,
"typedefs": true
} }
], ],
"no-unused-vars": "off", "no-unused-vars": "off",
...@@ -83,19 +98,20 @@ ...@@ -83,19 +98,20 @@
"ignoreRestSiblings": true "ignoreRestSiblings": true
} }
], ],
"no-unused-expressions": "off", "no-use-before-define": "off",
"@typescript-eslint/no-unused-expressions": [ "@typescript-eslint/no-use-before-define": [
"error", "warn",
{ {
"allowShortCircuit": true, "functions": true,
"allowTernary": true, "classes": true,
"allowTaggedTemplates": true "variables": true,
"enums": true,
"typedefs": true
} }
], ],
"no-useless-constructor": "off", "no-useless-constructor": "off",
"@typescript-eslint/no-useless-constructor": "warn", "@typescript-eslint/no-useless-constructor": "warn",
"@typescript-eslint/prefer-literal-enum-member": "error", "@typescript-eslint/prefer-literal-enum-member": "error"
"@typescript-eslint/prefer-namespace-keyword": "error"
}, },
"overrides": [ "overrides": [
{ {
......
...@@ -102,8 +102,8 @@ ...@@ -102,8 +102,8 @@
"@types/sharp": "0.29.3", "@types/sharp": "0.29.3",
"@types/string-hash": "1.1.1", "@types/string-hash": "1.1.1",
"@types/trusted-types": "2.0.3", "@types/trusted-types": "2.0.3",
"@typescript-eslint/eslint-plugin": "4.29.1", "@typescript-eslint/eslint-plugin": "6.1.0",
"@typescript-eslint/parser": "4.29.1", "@typescript-eslint/parser": "6.1.0",
"@vercel/fetch": "6.1.1", "@vercel/fetch": "6.1.1",
"@vercel/og": "0.5.9", "@vercel/og": "0.5.9",
"@zeit/next-typescript": "1.1.2-canary.0", "@zeit/next-typescript": "1.1.2-canary.0",
......
...@@ -77,7 +77,7 @@ export function getUrlFromPagesDirectories( ...@@ -77,7 +77,7 @@ export function getUrlFromPagesDirectories(
}) })
} }
export function execOnce<TArgs extends any[], TResult extends unknown>( export function execOnce<TArgs extends any[], TResult>(
fn: (...args: TArgs) => TResult fn: (...args: TArgs) => TResult
): (...args: TArgs) => TResult { ): (...args: TArgs) => TResult {
let used = false let used = false
......
...@@ -80,6 +80,8 @@ export function processEnv( ...@@ -80,6 +80,8 @@ export function processEnv(
typeof parsed[key] === 'undefined' && typeof parsed[key] === 'undefined' &&
typeof origEnv[key] === 'undefined' typeof origEnv[key] === 'undefined'
) { ) {
// We're being imprecise in the type system - assume parsed[key] can be undefined
// eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain
parsed[key] = result.parsed?.[key]! parsed[key] = result.parsed?.[key]!
} }
} }
......
...@@ -465,7 +465,7 @@ function getCodeAnalyzer(params: { ...@@ -465,7 +465,7 @@ function getCodeAnalyzer(params: {
buildInfo.importLocByPath = new Map() buildInfo.importLocByPath = new Map()
} }
const importedModule = node.source.value?.toString()! const importedModule = node.source.value?.toString()
buildInfo.importLocByPath.set(importedModule, { buildInfo.importLocByPath.set(importedModule, {
sourcePosition: { sourcePosition: {
...node.loc.start, ...node.loc.start,
......
// eslint-disable-next-line @typescript-eslint/no-unused-vars
declare const __webpack_require__: any declare const __webpack_require__: any
// eslint-disable-next-line @typescript-eslint/no-unused-vars
declare let __webpack_public_path__: string declare let __webpack_public_path__: string
const addChunkSuffix = const addChunkSuffix =
......
...@@ -562,6 +562,7 @@ type SitemapFile = Array<{ ...@@ -562,6 +562,7 @@ type SitemapFile = Array<{
type ResolvingMetadata = Promise<ResolvedMetadata> type ResolvingMetadata = Promise<ResolvedMetadata>
declare namespace MetadataRoute { declare namespace MetadataRoute {
// eslint-disable-next-line @typescript-eslint/no-shadow
export type Robots = RobotsFile export type Robots = RobotsFile
export type Sitemap = SitemapFile export type Sitemap = SitemapFile
export type Manifest = ManifestFile export type Manifest = ManifestFile
......
// source: https://github.com/sindresorhus/fnv1a // source: https://github.com/sindresorhus/fnv1a
// FNV_PRIMES and FNV_OFFSETS from // FNV_PRIMES and FNV_OFFSETS from
// http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-param // http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-param
/* eslint-disable @typescript-eslint/no-loss-of-precision */
const FNV_PRIMES = { const FNV_PRIMES = {
32: BigInt(16_777_619), 32: BigInt(16_777_619),
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment