Unverified Commit ce4f1e88 authored by Houssein Djirdeh's avatar Houssein Djirdeh Committed by GitHub

[ESLint] Adds `--quiet` flag, TypeScript resolver and bug fixes (#26280)

parent b4e0eb0b
...@@ -58,6 +58,9 @@ module.exports = { ...@@ -58,6 +58,9 @@ module.exports = {
[require.resolve('eslint-import-resolver-node')]: { [require.resolve('eslint-import-resolver-node')]: {
extensions: ['.js', '.jsx', '.ts', '.tsx'], extensions: ['.js', '.jsx', '.ts', '.tsx'],
}, },
[require.resolve('eslint-import-resolver-typescript')]: {
alwaysTryTypes: true,
},
}, },
}, },
} }
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
"@rushstack/eslint-patch": "^1.0.6", "@rushstack/eslint-patch": "^1.0.6",
"@typescript-eslint/parser": "^4.20.0", "@typescript-eslint/parser": "^4.20.0",
"eslint-import-resolver-node": "^0.3.4", "eslint-import-resolver-node": "^0.3.4",
"eslint-import-resolver-typescript": "^2.4.0",
"eslint-plugin-import": "^2.22.1", "eslint-plugin-import": "^2.22.1",
"eslint-plugin-jsx-a11y": "^6.4.1", "eslint-plugin-jsx-a11y": "^6.4.1",
"eslint-plugin-react": "^7.23.1", "eslint-plugin-react": "^7.23.1",
......
...@@ -44,9 +44,7 @@ module.exports = { ...@@ -44,9 +44,7 @@ module.exports = {
const hasAnchorChild = children.some( const hasAnchorChild = children.some(
(attr) => (attr) =>
attr.type === 'JSXElement' && attr.type === 'JSXElement' && attr.openingElement.name.name === 'a'
attr.openingElement.name.name === 'a' &&
attr.closingElement.name.name === 'a'
) )
if (!hasAnchorChild && !hasPassHref) { if (!hasAnchorChild && !hasPassHref) {
......
...@@ -28,7 +28,6 @@ const eslintOptions = (args: arg.Spec) => ({ ...@@ -28,7 +28,6 @@ const eslintOptions = (args: arg.Spec) => ({
args['--report-unused-disable-directives'] || null, args['--report-unused-disable-directives'] || null,
cache: args['--cache'] ?? false, cache: args['--cache'] ?? false,
cacheLocation: args['--cache-location'] || '.eslintcache', cacheLocation: args['--cache-location'] || '.eslintcache',
cacheStrategy: args['--cache-strategy'] || 'metadata',
errorOnUnmatchedPattern: !Boolean(args['--no-error-on-unmatched-pattern']), errorOnUnmatchedPattern: !Boolean(args['--no-error-on-unmatched-pattern']),
}) })
...@@ -55,11 +54,11 @@ const nextLint: cliCommand = (argv) => { ...@@ -55,11 +54,11 @@ const nextLint: cliCommand = (argv) => {
'--fix-type': [String], '--fix-type': [String],
'--ignore-path': String, '--ignore-path': String,
'--no-ignore': Boolean, '--no-ignore': Boolean,
'--quiet': Boolean,
'--no-inline-config': Boolean, '--no-inline-config': Boolean,
'--report-unused-disable-directives': String, '--report-unused-disable-directives': String,
'--cache': Boolean, '--cache': Boolean,
'--cache-location': String, '--cache-location': String,
'--cache-strategy': String,
'--no-error-on-unmatched-pattern': Boolean, '--no-error-on-unmatched-pattern': Boolean,
// Aliases // Aliases
...@@ -107,6 +106,9 @@ const nextLint: cliCommand = (argv) => { ...@@ -107,6 +106,9 @@ const nextLint: cliCommand = (argv) => {
--ignore-path path::String Specify path of ignore file --ignore-path path::String Specify path of ignore file
--no-ignore Disable use of ignore files and patterns --no-ignore Disable use of ignore files and patterns
Handling warnings:
--quiet Report errors only - default: false
Inline configuration comments: Inline configuration comments:
--no-inline-config Prevent comments from changing config or rules --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") --report-unused-disable-directives Adds reported errors for unused eslint-disable directives ("error" | "warn" | "off")
...@@ -114,7 +116,6 @@ const nextLint: cliCommand = (argv) => { ...@@ -114,7 +116,6 @@ const nextLint: cliCommand = (argv) => {
Caching: Caching:
--cache Only check changed files - default: false --cache Only check changed files - default: false
--cache-location path::String Path to the cache file or directory - default: .eslintcache --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: Miscellaneous:
--no-error-on-unmatched-pattern Prevent errors when pattern is unmatched - default: false --no-error-on-unmatched-pattern Prevent errors when pattern is unmatched - default: false
...@@ -140,7 +141,10 @@ const nextLint: cliCommand = (argv) => { ...@@ -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) => { .then(async (lintResults) => {
const lintOutput = const lintOutput =
typeof lintResults === 'string' ? lintResults : lintResults?.output typeof lintResults === 'string' ? lintResults : lintResults?.output
......
...@@ -28,7 +28,8 @@ async function lint( ...@@ -28,7 +28,8 @@ async function lint(
lintDirs: string[], lintDirs: string[],
eslintrcFile: string | null, eslintrcFile: string | null,
pkgJsonPath: string | null, pkgJsonPath: string | null,
eslintOptions: any = null eslintOptions: any = null,
reportErrorsOnly: boolean = false
): Promise< ): Promise<
| string | string
| null | null
...@@ -59,7 +60,6 @@ async function lint( ...@@ -59,7 +60,6 @@ async function lint(
'error' 'error'
)} - ESLint class not found. Please upgrade to ESLint version 7 or later` )} - ESLint class not found. Please upgrade to ESLint version 7 or later`
} }
let options: any = { let options: any = {
useEslintrc: true, useEslintrc: true,
baseConfig: {}, baseConfig: {},
...@@ -110,8 +110,9 @@ async function lint( ...@@ -110,8 +110,9 @@ async function lint(
} }
const lintStart = process.hrtime() const lintStart = process.hrtime()
const results = await eslint.lintFiles(lintDirs) let results = await eslint.lintFiles(lintDirs)
if (options.fix) await ESLint.outputFixes(results) 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 formattedResult = formatResults(baseDir, results)
const lintEnd = process.hrtime(lintStart) const lintEnd = process.hrtime(lintStart)
...@@ -141,7 +142,8 @@ export async function runLintCheck( ...@@ -141,7 +142,8 @@ export async function runLintCheck(
baseDir: string, baseDir: string,
lintDirs: string[], lintDirs: string[],
lintDuringBuild: boolean = false, lintDuringBuild: boolean = false,
eslintOptions: any = null eslintOptions: any = null,
reportErrorsOnly: boolean = false
): ReturnType<typeof lint> { ): ReturnType<typeof lint> {
try { try {
// Find user's .eslintrc file // Find user's .eslintrc file
...@@ -202,7 +204,8 @@ export async function runLintCheck( ...@@ -202,7 +204,8 @@ export async function runLintCheck(
lintDirs, lintDirs,
eslintrcFile, eslintrcFile,
pkgJsonPath, pkgJsonPath,
eslintOptions eslintOptions,
reportErrorsOnly
) )
} catch (err) { } catch (err) {
throw err throw err
......
...@@ -3,6 +3,6 @@ ...@@ -3,6 +3,6 @@
"root": true, "root": true,
"rules": { "rules": {
"@next/next/no-html-link-for-pages": 0, "@next/next/no-html-link-for-pages": 0,
"@next/next/no-sync-scripts": 2 "@next/next/no-sync-scripts": 1
} }
} }
const Home = () => ( const Home = () => (
<div> <div>
<p>Home</p> <p>Home</p>
<script src="https://example.com" />
/* Badly formatted comment */ /* Badly formatted comment */
</div> </div>
) )
......
...@@ -41,6 +41,9 @@ describe('ESLint', () => { ...@@ -41,6 +41,9 @@ describe('ESLint', () => {
}) })
const output = stdout + stderr const output = stdout + stderr
expect(output).toContain(
'Warning: External synchronous scripts are forbidden'
)
expect(output).toContain( expect(output).toContain(
'Error: Comments inside children section of tag should be placed inside braces' 'Error: Comments inside children section of tag should be placed inside braces'
) )
...@@ -75,7 +78,7 @@ describe('ESLint', () => { ...@@ -75,7 +78,7 @@ describe('ESLint', () => {
) )
}) })
test.only('invalid eslint version', async () => { test('invalid eslint version', async () => {
const { stdout, stderr } = await nextBuild(dirInvalidEslintVersion, [], { const { stdout, stderr } = await nextBuild(dirInvalidEslintVersion, [], {
stdout: true, stdout: true,
stderr: true, stderr: true,
...@@ -115,6 +118,9 @@ describe('ESLint', () => { ...@@ -115,6 +118,9 @@ describe('ESLint', () => {
}) })
const output = stdout + stderr const output = stdout + stderr
expect(output).toContain(
'Warning: External synchronous scripts are forbidden'
)
expect(output).toContain( expect(output).toContain(
'Error: Comments inside children section of tag should be placed inside braces' 'Error: Comments inside children section of tag should be placed inside braces'
) )
...@@ -167,5 +173,20 @@ describe('ESLint', () => { ...@@ -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'
)
})
}) })
}) })
...@@ -7991,6 +7991,17 @@ eslint-import-resolver-node@^0.3.4: ...@@ -7991,6 +7991,17 @@ eslint-import-resolver-node@^0.3.4:
debug "^2.6.9" debug "^2.6.9"
resolve "^1.13.1" 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: eslint-module-utils@^2.6.0:
version "2.6.0" version "2.6.0"
resolved "https://registry.yarnpkg.com/eslint-module-utils/-/eslint-module-utils-2.6.0.tgz#579ebd094f56af7797d19c9866c9c9486629bfa6" resolved "https://registry.yarnpkg.com/eslint-module-utils/-/eslint-module-utils-2.6.0.tgz#579ebd094f56af7797d19c9866c9c9486629bfa6"
......
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