Skip to content

Commit

Permalink
[eslint-plugin] Stop warning for head element in app/layout on Windows (
Browse files Browse the repository at this point in the history
#42336)

## Bug


![image](https://user-images.githubusercontent.com/13413409/199451248-051751e5-0a5b-4ee0-bcae-05d2933f0090.png)

When trying to use app directory on Windows, the automatically created
`Layout.tsx` get's a lint problem for having a `<head>` element.

## Solution

Update the rule to check paths using `path.sep` rather than hardcoded
`/`

I also checked with `path.posix.sep` because some other rules was doing
that (both `path.sep` and `path.posix.sep`), so I'm just following
pattern
  • Loading branch information
oBusk committed Nov 2, 2022
1 parent f0e768e commit 9de871e
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions packages/eslint-plugin-next/src/rules/no-head-element.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path = require('path')
import { defineRule } from '../utils/define-rule'

const url = 'https://nextjs.org/docs/messages/no-head-element'
Expand All @@ -18,9 +19,13 @@ export = defineRule({
JSXOpeningElement(node) {
const paths = context.getFilename()

const isInAppDir = paths.includes('app/') && !paths.includes('pages/')
const isInAppDir = () =>
(paths.includes(`app${path.sep}`) ||
paths.includes(`app${path.posix.sep}`)) &&
!paths.includes(`pages${path.sep}`) &&
!paths.includes(`pages${path.posix.sep}`)
// Only lint the <head> element in pages directory
if (node.name.name !== 'head' || isInAppDir) {
if (node.name.name !== 'head' || isInAppDir()) {
return
}

Expand Down

0 comments on commit 9de871e

Please sign in to comment.