Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support --cache-strategy ESLint argument (fixes #29926) #29928

Merged
merged 15 commits into from Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/next/cli/next-lint.ts
Expand Up @@ -30,6 +30,7 @@ const eslintOptions = (args: arg.Spec, defaultCacheLocation: string) => ({
args['--report-unused-disable-directives'] || null,
cache: !Boolean(args['--no-cache']),
cacheLocation: args['--cache-location'] || defaultCacheLocation,
cacheStrategy: args['--cache-strategy'] || 'metadata',
errorOnUnmatchedPattern: args['--error-on-unmatched-pattern']
? Boolean(args['--error-on-unmatched-pattern'])
: false,
Expand Down Expand Up @@ -67,6 +68,7 @@ const nextLint: cliCommand = async (argv) => {
'--cache': Boolean, // Although cache is enabled by default, this dummy flag still exists to not cause any breaking changes
'--no-cache': Boolean,
'--cache-location': String,
'--cache-strategy': String,
'--error-on-unmatched-pattern': Boolean,
'--format': String,

Expand Down Expand Up @@ -134,6 +136,7 @@ const nextLint: cliCommand = async (argv) => {
Caching:
--no-cache Disable caching
--cache-location path::String Path to the cache file or directory - default: .eslintcache
--cache-strategy String Strategy to use for detecting changed files in the cache, either metadata or content - default: metadata

Miscellaneous:
--error-on-unmatched-pattern Show errors when any file patterns are unmatched - default: false
Expand Down
41 changes: 41 additions & 0 deletions test/integration/eslint/test/index.test.js
Expand Up @@ -536,6 +536,47 @@ describe('ESLint', () => {
expect(fs.existsSync(cacheFile)).toBe(true)
})

const getEslintCacheContent = async (cacheDir) => {
const eslintCacheDir = join(cacheDir, 'eslint/')
let files = await fs.readdir(eslintCacheDir)
let cacheFiles = files.filter((f) => /\.cache/.test(f))
expect(cacheFiles.length).toBe(1)
const cacheFile = join(eslintCacheDir, cacheFiles[0])
return await fs.readFile(cacheFile, 'utf8')
}

test('the default eslint caching strategy is metadata', async () => {
const cacheDir = join(dirEslintCache, '.next', 'cache')

await fs.remove(cacheDir)
await nextLint(dirEslintCache)

const defaultStrategyCache = await getEslintCacheContent(cacheDir)

await fs.remove(cacheDir)
await nextLint(dirEslintCache, ['--cache-strategy', 'metadata'])

const metadataStrategyCache = await getEslintCacheContent(cacheDir)

expect(metadataStrategyCache).toBe(defaultStrategyCache)
})

test('cache with content strategy is different from the one with default strategy', async () => {
const cacheDir = join(dirEslintCache, '.next', 'cache')

await fs.remove(cacheDir)
await nextLint(dirEslintCache)

const defaultStrategyCache = await getEslintCacheContent(cacheDir)

await fs.remove(cacheDir)
await nextLint(dirEslintCache, ['--cache-strategy', 'content'])

const contentStrategyCache = await getEslintCacheContent(cacheDir)

expect(contentStrategyCache).not.toBe(defaultStrategyCache)
})

test('file flag can selectively lint only a single file', async () => {
const { stdout, stderr } = await nextLint(
dirFileLinting,
Expand Down