Skip to content

Commit

Permalink
fix: only lint when enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
wyattjoh committed May 24, 2024
1 parent 269f1fc commit c8c9ddf
Showing 1 changed file with 3 additions and 61 deletions.
64 changes: 3 additions & 61 deletions test/lib/next-test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import fetch from 'node-fetch'
import qs from 'querystring'
import treeKill from 'tree-kill'
import { once } from 'events'
import vm from 'vm'

import server from 'next/dist/server/next'
import _pkg from 'next/package.json'
Expand Down Expand Up @@ -192,6 +191,7 @@ export interface NextOptions {
stderr?: true | 'log'
stdout?: true | 'log'
ignoreFail?: boolean
lint?: boolean

onStdout?: (data: any) => void
onStderr?: (data: any) => void
Expand Down Expand Up @@ -438,71 +438,13 @@ export function launchApp(
)
}

/**
* Read a JavaScript file and return the exports.
*
* @param path The path to the JavaScript file.
* @returns The exports of the JavaScript file.
*/
function readJS(path: string) {
const data = readFileSync(path, 'utf8')
const script = new vm.Script(data, { filename: path })
const context = { module: { exports: {} } }
script.runInNewContext(context)
return context.module.exports
}

/**
* Check if the directory provides an ESLint configuration.
*
* @param dir The directory to check.
* @returns Whether the directory provides an ESLint configuration.
*/
export function shouldDisableLinting(dir: string) {
try {
// If the project already has a `next.config.js` file that includes an eslint
// config, then don't disable linting.
if (
existsSync(path.join(dir, 'next.config.js')) &&
'eslint' in readJS(path.join(dir, 'next.config.js'))
) {
return false
}

// If some of these files exist, we should not disable linting.
if (
['.eslintrc.json', '.eslintrc.js', '.eslintrc'].some((file) =>
existsSync(path.join(dir, file))
)
) {
return false
}

// If the `eslintConfig` field is present in the `package.json` file, we
// should not disable linting.
if (
existsSync(path.join(dir, 'package.json')) &&
readJson(path.join(dir, 'package.json')).eslintConfig
) {
return false
}

return true
} catch {
// An error ocurred while checking the directory, so we should not disable
// linting.
return false
}
}

export function nextBuild(
dir: string,
args: string[] = [],
opts: NextOptions = {}
) {
// Disable linting if the directory provides an ESLint configuration. This
// prevents the project's eslint configuration from affecting the test.
if (shouldDisableLinting(dir)) args.push('--no-lint')
// If the build hasn't requested it to be linted explicitly, disable linting.
if (!opts.lint) args.push('--no-lint')

return runNextCommand(['build', dir, ...args], opts)
}
Expand Down

0 comments on commit c8c9ddf

Please sign in to comment.