Skip to content

Commit

Permalink
[ESLint] Adds --quiet flag, TypeScript resolver and bug fixes (#26280)
Browse files Browse the repository at this point in the history
  • Loading branch information
housseindjirdeh authored Jun 18, 2021
1 parent b4e0eb0 commit ce4f1e8
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 14 deletions.
3 changes: 3 additions & 0 deletions packages/eslint-config-next/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
},
}
1 change: 1 addition & 0 deletions packages/eslint-config-next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 1 addition & 3 deletions packages/eslint-plugin-next/lib/rules/link-passhref.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 8 additions & 4 deletions packages/next/cli/next-lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']),
})

Expand All @@ -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
Expand Down Expand Up @@ -107,14 +106,16 @@ 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")
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
Expand All @@ -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
Expand Down
13 changes: 8 additions & 5 deletions packages/next/lib/eslint/runLintCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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: {},
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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<typeof lint> {
try {
// Find user's .eslintrc file
Expand Down Expand Up @@ -202,7 +204,8 @@ export async function runLintCheck(
lintDirs,
eslintrcFile,
pkgJsonPath,
eslintOptions
eslintOptions,
reportErrorsOnly
)
} catch (err) {
throw err
Expand Down
2 changes: 1 addition & 1 deletion test/integration/eslint/custom-config/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
1 change: 1 addition & 0 deletions test/integration/eslint/custom-config/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const Home = () => (
<div>
<p>Home</p>
<script src="https://example.com" />
/* Badly formatted comment */
</div>
)
Expand Down
23 changes: 22 additions & 1 deletion test/integration/eslint/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ describe('ESLint', () => {
})

const output = stdout + stderr
expect(output).toContain(
'Warning: External synchronous scripts are forbidden'
)
expect(output).toContain(
'Error: Comments inside children section of tag should be placed inside braces'
)
Expand Down Expand Up @@ -75,7 +78,7 @@ describe('ESLint', () => {
)
})

test.only('invalid eslint version', async () => {
test('invalid eslint version', async () => {
const { stdout, stderr } = await nextBuild(dirInvalidEslintVersion, [], {
stdout: true,
stderr: true,
Expand Down Expand Up @@ -115,6 +118,9 @@ describe('ESLint', () => {
})

const output = stdout + stderr
expect(output).toContain(
'Warning: External synchronous scripts are forbidden'
)
expect(output).toContain(
'Error: Comments inside children section of tag should be placed inside braces'
)
Expand Down Expand Up @@ -167,5 +173,20 @@ describe('ESLint', () => {
}
}
})

test('quiet flag suppresses warnings and only reports errors', async () => {
const { stdout, stderr } = await nextLint(dirCustomConfig, ['--quiet'], {
stdout: true,
stderr: true,
})

const output = stdout + stderr
expect(output).toContain(
'Error: Comments inside children section of tag should be placed inside braces'
)
expect(output).not.toContain(
'Warning: External synchronous scripts are forbidden'
)
})
})
})
11 changes: 11 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7991,6 +7991,17 @@ eslint-import-resolver-node@^0.3.4:
debug "^2.6.9"
resolve "^1.13.1"

eslint-import-resolver-typescript@^2.4.0:
version "2.4.0"
resolved "https://registry.yarnpkg.com/eslint-import-resolver-typescript/-/eslint-import-resolver-typescript-2.4.0.tgz#ec1e7063ebe807f0362a7320543aaed6fe1100e1"
integrity sha512-useJKURidCcldRLCNKWemr1fFQL1SzB3G4a0li6lFGvlc5xGe1hY343bvG07cbpCzPuM/lK19FIJB3XGFSkplA==
dependencies:
debug "^4.1.1"
glob "^7.1.6"
is-glob "^4.0.1"
resolve "^1.17.0"
tsconfig-paths "^3.9.0"

eslint-module-utils@^2.6.0:
version "2.6.0"
resolved "https://registry.yarnpkg.com/eslint-module-utils/-/eslint-module-utils-2.6.0.tgz#579ebd094f56af7797d19c9866c9c9486629bfa6"
Expand Down

0 comments on commit ce4f1e8

Please sign in to comment.