Commit graph

44 commits

Author SHA1 Message Date
Jiwon Choi
022cb25640
fix(eslint): next/script beforeInteractive gives warning on appDir (#51148)
## Issue

The `context.getFilename()` gets the absolute path of the files, and the if statement to filter `/src` and `/app` was not working since #46609.

## Expected

Do not show a lint warning if use `beforeInteractive` inside `appDir`.

Fixes #46609 #50261
2023-11-12 09:30:10 +00:00
Jiachi Liu
f4b947fb56
Fix eslint async client component (#51952)
Fixes #51917 

It's throwing error in arrow function, should also be covered in the
lint rule
fix NEXT-1326
2023-06-28 20:03:32 -07:00
Ty Hopp
65f94baa83
feat: Introduce eslint rule for async client components (#51547)
The [React Server Component RFC on promise support](https://github.com/acdlite/rfcs/blob/first-class-promises/text/0000-first-class-support-for-promises.md) indicates that [client components cannot be async functions](https://github.com/acdlite/rfcs/blob/first-class-promises/text/0000-first-class-support-for-promises.md#why-cant-client-components-be-async-functions).

This PR introduces an eslint rule to enforce this.

Fixes #50898
2023-06-26 21:10:24 +00:00
최지원
2aa68ae287
fix: eslint not ignoring "/app" (#50261)
## Why?
```
src/app/layout.tsx
```
![code](https://github.com/vercel/next.js/assets/120007119/3cc991f8-a96d-4ffb-9f84-4dfdbcac5318)

**eslint[@next/next/no-before-interactive-script-outside-document](https://nextjs.org/docs/messages/no-before-interactive-script-outside-document)**
> `next/script`'s `beforeInteractive` strategy should not be used outside of `pages/_document.js`. 

## How?
- Possibility of pathname starts with "/" on OS X and Linux, so added condition to check file path starting with "/"
- e.g. "/src/app" or "/app"
- Added test code



Passed 
- `pnpm build && pnpm lint`
- `pnpm jest test/unit/eslint-plugin-next/no-before-interactive-script-outside-document.test.ts`

Extends #46609
Fixes #50096
Fixes NEXT-1230
2023-05-26 16:27:42 +00:00
Mark Ladyshau
054f786c8c
Skip warning if there is app directory for no-html-link-for-pages rule (#42516)
Skips pages directory warning if there is app directory in the root for `no-html-link-for-pages` lint rule.

Fixes #42448.

## Bug

- [x] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
2023-03-22 02:15:52 +00:00
Jan Kaifer
8b50b80cde
eslint rule for script strategy should work properly in app/ (#46609)
fixes https://github.com/vercel/next.js/issues/46549

The docs are wrong for now, this limitation is just in `pages/`, you can
use it anywhere in `app/`.

Since `app/` is still not stable we will emit that warning in all files
outside of `app/`.
2023-03-08 11:28:52 +01:00
Michael Novotny
5e112c062e
Updates next/image eslint message to denote that costs may be incurred. (#46640)
Changes ESLint warning message to still encourage usage of `next/image`
for the best experience, but also denoting that optimization could come
with incurred costs.

## Feature

- [x] Documentation added
- [x] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [x] Make sure the linting passes by running `pnpm build && pnpm lint`
- [x] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)

---------

Co-authored-by: Steven Tey <stevensteel97@gmail.com>
Co-authored-by: Steven <steven@ceriously.com>
2023-03-01 14:28:09 -08:00
Steven
297459d534
Update no-img-element lint rule (#43982)
This updates the `no-img-element` lint rule to explain the trade-offs of enabling or disabling Image Optimization.
2022-12-13 01:50:59 +00:00
Mark Ladyshau
2b85da7735
Change condition to check for string attribute in lint rules utility function (#42625)
<!--
Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change that you're making:
-->

Fixes #42604.

## Bug

- [x] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
2022-11-09 19:54:38 -08:00
Balázs Orbán
73b473991c
chore: add build step to eslint-plugin-next (#38647)
Follow-up on https://github.com/vercel/next.js/pull/38534#pullrequestreview-1035020450

This lets us use modern JS/TS syntax in ESLint rules and avoid issues like #38530 and #36693

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)


Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2022-09-30 01:37:59 +00:00
Jiachi Liu
82682a3a53
Disable head element lint rule for appDir (#40921)
Only lint `<head>` elements usage in `pages/`, `<head>` should be able
to use in appDir
2022-09-26 15:20:16 -07:00
Đorđe Spasić
804dc6bd10
fix: eslint no-script-component-in-head error url (#40422)
## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [x] Make sure the linting passes by running `pnpm lint`
- [x] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)


Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2022-09-12 16:15:33 +00:00
Julien Deniau
8ebd1a24cb
Update polyfill for eslint no-unwanted-polyfillio rule (#33170)
Fixes #33072

I documented all `esXXX` features to be sure that they were already polyfilled. Only `es2019` feature `Object.fromEntries` is not already polyfilled by nextjs.

I added some unwanted polyfill (that are polyfilled by nextjs).

I kept the `es5`, `es6` and `es2015` "as-is" as they contain functions that does not seem to be explicitly polyfilled (all `Math` functions or `Date.now` for example) in the [polyfill file](https://github.com/vercel/next.js/blob/master/packages/next-polyfill-nomodule/src/index.js)

Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2022-08-08 05:56:31 +00:00
Michael Novotny
edd395a7d3
Adds tests to ensure eslint-plugin-next's available rules are properly exported and recommended rules are correctly defined. (#38183)
* Adds tests to ensure `eslint-plugin-next`'s available rules are properly exported and recommended rules are defined correctly.

* Condenses imports.

* Sets default recommended value.

* replace Object.hasOwn for node 14

Co-authored-by: JJ Kasper <jj@jjsweb.site>
2022-06-30 11:31:33 -05:00
Michael Novotny
c83f94cf8b
Readds missing @next/next/no-assign-module-variable ESLint rule. (#38134)
Readds `@next/next/no-assign-module-variable` ESLint rule that was inadvertently removed in #34335 during the resolution of many merge conflicts.

This PR will get us back to a good / working state. I'll see if I can add a test to ensure all rule are accounted for in a separate PR.

Fixes #34335.

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [x] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [x] Make sure the linting passes by running `pnpm lint`
- [x] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)
2022-06-29 02:54:47 +00:00
Gal Schlezinger
22206651a6
[eslint-plugin-next] remove no-server-import-in-page rule (#38028)
This rule was good and kinda made sense when we had nested Middleware.
Now that we have a single Middleware, one might extract logic into different places
and I don't think we should limit importing `NextResponse` or `NextRequest`.

## Related

- Closes #36239
- Closes #37309



## Bug

- [x] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`
2022-06-26 12:43:15 +00:00
Andrei Stefan
6b829d8bd6
fix(eslint): allow <img> in conjunction with <picture> (#37504) (#37570)
* fix(eslint): allow <img> in conjunction with <picture> (#37504)

* Apply suggestions from code review

* add space

Co-authored-by: JJ Kasper <jj@jjsweb.site>
2022-06-15 21:13:52 -05:00
Michael Novotny
5211ac5cae
Adds consistency to ESLint rules. (#34335)
* Adds consistency to ESLint rules.

* Fixes lint errors.

* Fixes manifest.

* Adds missing title.

* Fixes copy / paste error.

Co-authored-by: Lee Robinson <me@leerob.io>

* Update errors/no-script-in-document.md

Co-authored-by: Lee Robinson <me@leerob.io>

* Update errors/no-sync-scripts.md

Co-authored-by: Lee Robinson <me@leerob.io>

* Updates a couple of rule descriptions.

* Adds redirects.

* Fixes unit tests.

* Removes duplicated section.

* Updates `no-before-interactive-script-outside-document` description.

* Fixes lint.

* Fixes integration tests.

* Adds description to `no-before-interactive-script-outside-document` documentation.

* Removes `link-passhref` from rules list.

* Updates remaining `pages/_middleware.js` references.

* Adds consistancy to messaging in new `no-styled-jsx-in-document` rule.

* Apply suggestions from code review

* Apply suggestions from code review

Co-authored-by: Lee Robinson <me@leerob.io>
Co-authored-by: Tim Neutkens <tim@timneutkens.nl>
Co-authored-by: JJ Kasper <jj@jjsweb.site>
2022-06-13 21:17:42 -05:00
Damien Simonin Feugas
09f48a7590
fix(eslint): false positive for legit next/server imports (#37515)
* fix(eslint): next/server error when next is not in the root

* chore: fix lint issue and posix compatibility

* chore: make it portable
2022-06-08 11:08:59 -05:00
Sharath Challa
11ad65e445
Add eslint rule for not allowing styled-jsx in _document.js (#32678)
## Bug

- [ ] Related issues linked
fixes #32656



Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2022-05-23 02:32:13 +00:00
Javi Velasco
f354f46b3f
Deprecate nested Middleware in favor of root middleware (#36772)
This PR deprecates declaring a middleware under `pages` in favour of the project root naming it after `middleware` instead of `_middleware`. This is in the context of having a simpler execution model for middleware and also ships some refactor work. There is a ton of a code to be simplified after this deprecation but I think it is best to do it progressively.

With this PR, when in development, we will **fail** whenever we find a nested middleware but we do **not** include it in the compiler so if the project is using it, it will no longer work. For production we will **fail** too so it will not be possible to build and deploy a deprecated middleware. The error points to a page that should also be reviewed as part of **documentation**.

Aside from the deprecation, this migrates all middleware tests to work with a single middleware. It also splits tests into multiple folders to make them easier to isolate and work with. Finally it ships some small code refactor and simplifications.
2022-05-19 15:46:21 +00:00
Hannes Bornö
4fd883f238
Remove optional chaining from eslint rule to support older node versions (#36978)
fixes #36693

## Bug

- [x] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`
2022-05-17 17:09:31 +00:00
Tim Neutkens
f550da7031
Remove passhref Eslint rule as it's no longer needed with new link behavior (#36511) 2022-04-27 17:33:13 +02:00
Janicklas Ralph
0441f816a6
Changes to the beforeInteractive strategy to make it work for streaming (#31936)
Changes to the beforeInteractive strategy to make it work for streaming

Splitting `beforeInteractive` into two strategies `beforeInteractive` at the _document level and `beforePageRender` for page level <Scripts>
2022-04-21 21:15:53 +00:00
Michael Ward
1be5f36123
Adds linting rule to avoid assignment to 'module' variable. Fixes #34859 (#35279) 2022-04-15 14:45:53 +02:00
Sukka
3ee458e376
fix(#34030): ignore non-checkable jsx spread attribute (#34473)
## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

Fixes #34030.

The PR is still WIP as the test case hasn't been added, help or change is welcome.

cc @no-ya @ijjk 

Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2022-02-25 04:15:25 +00:00
Kara
61ea8efe42
Warn in dev mode when script tags are added with next/head (#33968)
This commit adds a development mode warning in the console
if you try to include <script> tags in next/head, e.g.

```
<Head>
  <script async src="..." />
</Head>
```

The warning message explains that this pattern will not
work well with Suspense/streaming and recommends using the
next/script component instead.

TODO in follow-up PR: add same warning for stylesheets, etc

## Feature

- [x] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [x] Integration tests added
- [x] Documentation added
- [x] Errors have helpful link attached, see `contributing.md`
2022-02-04 13:06:55 +00:00
stefanprobst
49afcd4871
fix: handle jsxspreadattribute in inline-script-id eslint rule (#32421)
fixes #32178

the `inline-script-id` eslint rule crashed when encountering a `JSXSpreadAttribute`. this pr fixes that, and also handles `id` being passed via the spreaded object.

## Bug

- [x] Related issues linked using `fixes #number`
- [x] ~~Integration~~ Unit tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
2022-02-02 23:04:22 +00:00
Stoyan Delev
a7793c7a0c
fix eslint link-passhref rule (#33857) 2022-02-01 12:53:55 -06:00
Houssein Djirdeh
d72f5bd69d
[ESLint] Adds lint rule to flag usage of <head> (#32897)
## Bug

- [X] Related issues linked using `fixes #number`
- [X] Tests added
- [X] Errors have helpful link attached, see `contributing.md`

Fixes #30142
2021-12-31 05:09:44 +00:00
Joost De Cock
34dcede690
fix(eslint-plugin-next): Broken links in eslint output (#32837)
This fixes broken links in the eslint output by removing the trailing full stop.
It also makes the formatting of (the output of) the various rules consistent.

## Documentation / Examples

- [x] Make sure the linting passes by running `yarn lint`

> I don't think this is a bug, nor a feature, nor is it really documentation. 
> It's just a small nuisance that I bumped into and felt compelled to fix.
> I went with documentation as that seems the closest match

## What does this pull request do?

The elslint output of `eslint-plugin-next` contains useful links to the documentation about the various rules.
Unfortunately, on most (but not all) rules, those links are immediately followed by a full stop (`.`).

The terminal (or any parser) has no way of knowing that the full stop is not part of the URL. 
So it includes it and clicking the link leads to a 404 on the nextjs.org website.
 
![eslint](https://user-images.githubusercontent.com/1708494/147452577-43ad4ce7-df75-4d48-ab78-70b9b8212b7e.png)

This PR fixes that by removing the full stop. 

## But a final full stop is better grammar

I considered alternatives (such as [a zero-width space character](https://en.wikipedia.org/wiki/Zero-width_space#Prohibited_in_URLs)) in case the final full stop was part of the style guide or something.

However, as I went through the eslint rules, I notices that the messages for various rules were formatted inconsistently. 
Some with final full stop, some without.

As such, I made the all consistent with this structure:

> [message]. See: [url]

I feel this is a better solution than using the zero-width space as these sort of invisible characters 
in code can be a red flag that something fishy is going on.

I submit this pull request in the hope it will be useful, and a positive contribution to a project I have a great deal of appreciation for.
That being said, I fully understand if people would consider this a non-issue.
2021-12-28 02:18:39 +00:00
Balázs Orbán
4e345f6677
feat(eslint): allow a for internal url when target="blank" present (#32780)
When an `a` tag is used to link to an internal page, but the `target="_blank"` attribute is present, ESLint should not report it as an error and should not enforce using `next/link`.

Fixes #28547

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
2021-12-25 00:55:43 +00:00
Bryan Smith
b7eeb6c582
Fix crash in no-page-custom-font eslint rule when default export is unnamed. (#32251)
fixes #32250
2021-12-07 19:04:24 +00:00
Thibaut SABOT
5b4e139dde
Fix no-server-import-in-page eslint rule for subfolder middleware (#32139)
People have been reporting on https://github.com/vercel/next.js/pull/30973 that the `no-server-import-in-page` eslint rule is reporting false positives for `_middleware` files inside sub-page folders.

Unlike `_document`, we can have multiple `_middleware` files.

Fixes #32121
2021-12-05 14:41:10 +00:00
Gerrit Alex
d38dd3d7e6
fix: support function components in _document in no-page-custom-font (#31560) 2021-11-26 14:27:13 +01:00
Nils Kaspersson
d1ce7b7c58
[ESLint] Prevent no-html-link-for-pages from warning for static files (#31495)
The rule [`no-html-link-for-pages`](https://nextjs.org/docs/messages/no-html-link-for-pages) will incorrectly flag an `<a>`-tag intended to download a local asset. This PR adds an exception to the rule for any anchor element with a `download` attribute.

Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2021-11-19 03:39:36 +00:00
Thibaut SABOT
d1adf1d503
Add eslint rule to prevent importing next/server outside of _middleware (#30973)
* Add eslint rule to prevent importing next/server outside of _middleware

* add error to manifest.json

Co-authored-by: Javi Velasco <javier.velasco86@gmail.com>
2021-11-12 17:44:39 -06:00
Katsuma Ito
2e51e1bb9b
[ESLint] Add add next/script support for no-unwanted-polyfillio rule (#28944)
Make the `no-unwanted-polyfillio` rule respond to the `next/script` component as well as the `script` tag.

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [x] ~Integration~Unit tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`
2021-10-22 11:19:24 +00:00
Houssein Djirdeh
d0563127c6
[ESLint Plugin] Updates no-document-import-in-page rule to use path separators (#28768)
Context: https://github.com/vercel/next.js/pull/28745/files#r701418932
2021-09-03 18:51:21 +00:00
Steven
9bbba98c01
Fix next lint no-document-import-in-page on Windows (#28745)
Fixes #28596
2021-09-02 20:51:22 +00:00
Prateek Bhatnagar
7c29a262e1
Bug fix for lint rule (#28677) 2021-09-02 10:45:27 +02:00
Rahul Gaba
c21cbdf899
Fix for next-script-for-ga ESLint rule crashing when a method is used in dangerouslySetInnerHTML (#28675)
Fixes #28635



## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [x] Errors have helpful link attached, see `contributing.md`
2021-08-31 20:23:21 +00:00
Houssein Djirdeh
9442925f50
handle edge case for no-import-document rule (#28261)
Co-authored-by: JJ Kasper <jj@jjsweb.site>
2021-08-25 13:30:49 -05:00
JJ Kasper
005b13f1ac
Move unit tests to one folder and migrate them to TypeScript (#28427)
* Move unit tests to one folder

* Migrate unit tests to TypeScript

* add test types to lint

* Ensure ts(x) tests are run with util

* Add tsx extension to jest config

* bump
2021-08-24 07:52:45 -05:00