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

Lint files with mjs, mts, cjs and cts extension by default #40879

Merged
merged 3 commits into from Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 7 additions & 1 deletion packages/eslint-config-next/index.js
Expand Up @@ -107,7 +107,13 @@ module.exports = {
version: 'detect',
},
'import/parsers': {
[require.resolve('@typescript-eslint/parser')]: ['.ts', '.tsx', '.d.ts'],
[require.resolve('@typescript-eslint/parser')]: [
'.ts',
'.mts',
mrkldshv marked this conversation as resolved.
Show resolved Hide resolved
'.cts',
'.tsx',
'.d.ts',
],
},
'import/resolver': {
[require.resolve('eslint-import-resolver-node')]: {
Expand Down
11 changes: 10 additions & 1 deletion packages/next/cli/next-lint.ts
Expand Up @@ -18,7 +18,16 @@ import { getProjectDir } from '../lib/get-project-dir'

const eslintOptions = (args: arg.Spec, defaultCacheLocation: string) => ({
overrideConfigFile: args['--config'] || null,
extensions: args['--ext'] ?? ['.js', '.jsx', '.ts', '.tsx'],
extensions: args['--ext'] ?? [
'.js',
'.mjs',
'.cjs',
'.jsx',
'.ts',
'.mts',
'.cts',
'.tsx',
],
resolvePluginsRelativeTo: args['--resolve-plugins-relative-to'] || null,
rulePaths: args['--rulesdir'] ?? [],
fix: args['--fix'] ?? false,
Expand Down
4 changes: 4 additions & 0 deletions test/integration/eslint/mjs-cjs-linting/.eslintrc.json
@@ -0,0 +1,4 @@
{
"extends": "next",
"root": true
}
10 changes: 10 additions & 0 deletions test/integration/eslint/mjs-cjs-linting/pages/bar.mjs
@@ -0,0 +1,10 @@
export default class Bar {
render() {
return (
<div>
<h1>Hello title</h1>
<img src="img" />
</div>
)
}
}
10 changes: 10 additions & 0 deletions test/integration/eslint/mjs-cjs-linting/pages/index.cjs
@@ -0,0 +1,10 @@
export default class Home {
render() {
return (
<div>
<h1>Hello title</h1>
<script src="test"></script>
</div>
)
}
}
23 changes: 23 additions & 0 deletions test/integration/eslint/test/index.test.js
Expand Up @@ -34,6 +34,7 @@ const dirNoConfig = join(__dirname, '../no-config')
const dirEslintCache = join(__dirname, '../eslint-cache')
const dirEslintCacheCustomDir = join(__dirname, '../eslint-cache-custom-dir')
const dirFileLinting = join(__dirname, '../file-linting')
const mjsCjsLinting = join(__dirname, '../mjs-cjs-linting')

describe('ESLint', () => {
describe('Next Build', () => {
Expand Down Expand Up @@ -767,5 +768,27 @@ describe('ESLint', () => {
`Cannot write to output file path, it is a directory: ${filePath}`
)
})

test('lint files with cjs and mjs file extension', async () => {
const { stdout, stderr } = await nextLint(mjsCjsLinting, [], {
stdout: true,
stderr: true,
})

const output = stdout + stderr

expect(output).toContain('pages/bar.mjs')
expect(output).toContain(
'img elements must have an alt prop, either with meaningful text, or an empty string for decorative images.'
)
expect(output).toContain(
'Do not use `<img>` element. Use `<Image />` from `next/image` instead. See: https://nextjs.org/docs/messages/no-img-element'
)

expect(output).toContain('pages/index.cjs')
expect(output).toContain(
'Synchronous scripts should not be used. See: https://nextjs.org/docs/messages/no-sync-scripts'
)
})
})
})