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(nextjs): fix .next folder linting #11614

Merged
merged 1 commit into from
Aug 22, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ describe('app', () => {
],
"ignorePatterns": Array [
"!**/*",
".next/**/*",
],
"overrides": Array [
Object {
Expand Down
5 changes: 5 additions & 0 deletions packages/next/src/generators/application/lib/add-linting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ export async function addLinting(
// Turn off @next/next/no-html-link-for-pages since there is an issue with nextjs throwing linting errors
// TODO(nicholas): remove after Vercel updates nextjs linter to only lint ["*.ts", "*.tsx", "*.js", "*.jsx"]

reactEslintJson.ignorePatterns = [
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this go into a .eslintignore file at the root?

Copy link
Contributor Author

@ndcunningham ndcunningham Aug 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so since next uses the react .eslintignore.

I think the pattern .next/ is project specific, so probably not.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think since .eslintignore isn't always present ,we can keep this just in the app's ignore folder.

...reactEslintJson.ignorePatterns,
'.next/**/*',
];

reactEslintJson.rules = {
'@next/next/no-html-link-for-pages': 'off',
...reactEslintJson.rules,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { addProjectConfiguration, readJson, Tree } from '@nrwl/devkit';
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import { updateNextEslint } from './update-next-eslint';

describe('Add next eslint 14.5.7', () => {
let tree: Tree;

beforeEach(async () => {
tree = createTreeWithEmptyWorkspace();

addProjectConfiguration(tree, 'app1', {
root: 'apps/app1',
targets: {
build: {
executor: '@nrwl/next:build',
},
},
});

tree.write(
'nx.json',
JSON.stringify({
projects: {
app1: {},
},
})
);
});

it('should update "next" config ignorePattern', async () => {
tree.write(
'apps/app1/.eslintrc.json',
JSON.stringify({
extends: ['../../.eslintrc.json'],
ignorePatterns: ['!**/*'],
})
);

await updateNextEslint(tree);

const result = readJson(tree, 'apps/app1/.eslintrc.json');
expect(result.ignorePatterns).toContain('.next/**/*');
});

it('should not update "next" config ignorePattern if .next pattern already exists', async () => {
tree.write(
'apps/app1/.eslintrc.json',
JSON.stringify({
extends: ['../../.eslintrc.json'],
ignorePatterns: ['!**/*', '.next/**/*', '/foo/bar'],
})
);
const before = readJson(tree, 'apps/app1/.eslintrc.json');

await updateNextEslint(tree);

const result = readJson(tree, 'apps/app1/.eslintrc.json');
expect(result.ignorePatterns).toEqual(before.ignorePatterns);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { formatFiles, getProjects, Tree, updateJson } from '@nrwl/devkit';

export async function updateNextEslint(host: Tree) {
const projects = getProjects(host);

projects.forEach((project) => {
if (project.targets?.build?.executor !== '@nrwl/next:build') return;

const eslintPath = `${project.root}/.eslintrc.json`;

if (!host.exists(eslintPath)) return;

updateJson(host, eslintPath, (eslintConfig) => {
const nextIgnorePattern = '.next/**/*';

if (eslintConfig.ignorePatterns.indexOf(nextIgnorePattern) < 0) {
eslintConfig.ignorePatterns = [
...eslintConfig.ignorePatterns,
nextIgnorePattern,
];
}
return eslintConfig;
});
});
await formatFiles(host);
}

export default updateNextEslint;