From ce4f1e889e14f948d42eed1027c98f6209dd64bb Mon Sep 17 00:00:00 2001 From: Houssein Djirdeh Date: Fri, 18 Jun 2021 06:17:53 -0700 Subject: [PATCH 1/6] [ESLint] Adds `--quiet` flag, TypeScript resolver and bug fixes (#26280) --- packages/eslint-config-next/index.js | 3 +++ packages/eslint-config-next/package.json | 1 + .../lib/rules/link-passhref.js | 4 +--- packages/next/cli/next-lint.ts | 12 ++++++---- packages/next/lib/eslint/runLintCheck.ts | 13 +++++++---- .../eslint/custom-config/.eslintrc | 2 +- .../eslint/custom-config/pages/index.js | 1 + test/integration/eslint/test/index.test.js | 23 ++++++++++++++++++- yarn.lock | 11 +++++++++ 9 files changed, 56 insertions(+), 14 deletions(-) diff --git a/packages/eslint-config-next/index.js b/packages/eslint-config-next/index.js index 2d8c7a9902e4e..7b8bfbb2f615b 100644 --- a/packages/eslint-config-next/index.js +++ b/packages/eslint-config-next/index.js @@ -58,6 +58,9 @@ module.exports = { [require.resolve('eslint-import-resolver-node')]: { extensions: ['.js', '.jsx', '.ts', '.tsx'], }, + [require.resolve('eslint-import-resolver-typescript')]: { + alwaysTryTypes: true, + }, }, }, } diff --git a/packages/eslint-config-next/package.json b/packages/eslint-config-next/package.json index 39bbba5c98133..5fa19e75c2e04 100644 --- a/packages/eslint-config-next/package.json +++ b/packages/eslint-config-next/package.json @@ -13,6 +13,7 @@ "@rushstack/eslint-patch": "^1.0.6", "@typescript-eslint/parser": "^4.20.0", "eslint-import-resolver-node": "^0.3.4", + "eslint-import-resolver-typescript": "^2.4.0", "eslint-plugin-import": "^2.22.1", "eslint-plugin-jsx-a11y": "^6.4.1", "eslint-plugin-react": "^7.23.1", diff --git a/packages/eslint-plugin-next/lib/rules/link-passhref.js b/packages/eslint-plugin-next/lib/rules/link-passhref.js index cfde6bdc943e3..fc78090a8e3d6 100644 --- a/packages/eslint-plugin-next/lib/rules/link-passhref.js +++ b/packages/eslint-plugin-next/lib/rules/link-passhref.js @@ -44,9 +44,7 @@ module.exports = { const hasAnchorChild = children.some( (attr) => - attr.type === 'JSXElement' && - attr.openingElement.name.name === 'a' && - attr.closingElement.name.name === 'a' + attr.type === 'JSXElement' && attr.openingElement.name.name === 'a' ) if (!hasAnchorChild && !hasPassHref) { diff --git a/packages/next/cli/next-lint.ts b/packages/next/cli/next-lint.ts index 04993ca8def78..b4e9d38343803 100755 --- a/packages/next/cli/next-lint.ts +++ b/packages/next/cli/next-lint.ts @@ -28,7 +28,6 @@ const eslintOptions = (args: arg.Spec) => ({ args['--report-unused-disable-directives'] || null, cache: args['--cache'] ?? false, cacheLocation: args['--cache-location'] || '.eslintcache', - cacheStrategy: args['--cache-strategy'] || 'metadata', errorOnUnmatchedPattern: !Boolean(args['--no-error-on-unmatched-pattern']), }) @@ -55,11 +54,11 @@ const nextLint: cliCommand = (argv) => { '--fix-type': [String], '--ignore-path': String, '--no-ignore': Boolean, + '--quiet': Boolean, '--no-inline-config': Boolean, '--report-unused-disable-directives': String, '--cache': Boolean, '--cache-location': String, - '--cache-strategy': String, '--no-error-on-unmatched-pattern': Boolean, // Aliases @@ -107,6 +106,9 @@ const nextLint: cliCommand = (argv) => { --ignore-path path::String Specify path of ignore file --no-ignore Disable use of ignore files and patterns + Handling warnings: + --quiet Report errors only - default: false + Inline configuration comments: --no-inline-config Prevent comments from changing config or rules --report-unused-disable-directives Adds reported errors for unused eslint-disable directives ("error" | "warn" | "off") @@ -114,7 +116,6 @@ const nextLint: cliCommand = (argv) => { Caching: --cache Only check changed files - default: false --cache-location path::String Path to the cache file or directory - default: .eslintcache - --cache-strategy String Strategy to use for detecting changed files - either: metadata or content - default: metadata Miscellaneous: --no-error-on-unmatched-pattern Prevent errors when pattern is unmatched - default: false @@ -140,7 +141,10 @@ const nextLint: cliCommand = (argv) => { }, [] ) - runLintCheck(baseDir, lintDirs, false, eslintOptions(args)) + + const reportErrorsOnly = Boolean(args['--quiet']) + + runLintCheck(baseDir, lintDirs, false, eslintOptions(args), reportErrorsOnly) .then(async (lintResults) => { const lintOutput = typeof lintResults === 'string' ? lintResults : lintResults?.output diff --git a/packages/next/lib/eslint/runLintCheck.ts b/packages/next/lib/eslint/runLintCheck.ts index 1754f56ab9c88..9787e98652c56 100644 --- a/packages/next/lib/eslint/runLintCheck.ts +++ b/packages/next/lib/eslint/runLintCheck.ts @@ -28,7 +28,8 @@ async function lint( lintDirs: string[], eslintrcFile: string | null, pkgJsonPath: string | null, - eslintOptions: any = null + eslintOptions: any = null, + reportErrorsOnly: boolean = false ): Promise< | string | null @@ -59,7 +60,6 @@ async function lint( 'error' )} - ESLint class not found. Please upgrade to ESLint version 7 or later` } - let options: any = { useEslintrc: true, baseConfig: {}, @@ -110,8 +110,9 @@ async function lint( } const lintStart = process.hrtime() - const results = await eslint.lintFiles(lintDirs) + let results = await eslint.lintFiles(lintDirs) if (options.fix) await ESLint.outputFixes(results) + if (reportErrorsOnly) results = await ESLint.getErrorResults(results) // Only return errors if --quiet flag is used const formattedResult = formatResults(baseDir, results) const lintEnd = process.hrtime(lintStart) @@ -141,7 +142,8 @@ export async function runLintCheck( baseDir: string, lintDirs: string[], lintDuringBuild: boolean = false, - eslintOptions: any = null + eslintOptions: any = null, + reportErrorsOnly: boolean = false ): ReturnType { try { // Find user's .eslintrc file @@ -202,7 +204,8 @@ export async function runLintCheck( lintDirs, eslintrcFile, pkgJsonPath, - eslintOptions + eslintOptions, + reportErrorsOnly ) } catch (err) { throw err diff --git a/test/integration/eslint/custom-config/.eslintrc b/test/integration/eslint/custom-config/.eslintrc index 3b753142cfa11..1940c0953ab0a 100644 --- a/test/integration/eslint/custom-config/.eslintrc +++ b/test/integration/eslint/custom-config/.eslintrc @@ -3,6 +3,6 @@ "root": true, "rules": { "@next/next/no-html-link-for-pages": 0, - "@next/next/no-sync-scripts": 2 + "@next/next/no-sync-scripts": 1 } } diff --git a/test/integration/eslint/custom-config/pages/index.js b/test/integration/eslint/custom-config/pages/index.js index ca1cd577585d0..52239fe62f15a 100644 --- a/test/integration/eslint/custom-config/pages/index.js +++ b/test/integration/eslint/custom-config/pages/index.js @@ -1,6 +1,7 @@ const Home = () => (

Home

+