diff --git a/packages/eslint-plugin-next/src/rules/no-before-interactive-script-outside-document.ts b/packages/eslint-plugin-next/src/rules/no-before-interactive-script-outside-document.ts index 6cb82c6849..5f22931e6a 100644 --- a/packages/eslint-plugin-next/src/rules/no-before-interactive-script-outside-document.ts +++ b/packages/eslint-plugin-next/src/rules/no-before-interactive-script-outside-document.ts @@ -4,10 +4,8 @@ import * as path from 'path' const url = 'https://nextjs.org/docs/messages/no-before-interactive-script-outside-document' -const startsWithUsingCorrectSeparators = (str: string, start: string) => - [path.sep, path.posix.sep].some((sep) => - str.startsWith(start.replace(/\//g, sep)) - ) +const convertToCorrectSeparator = (str: string) => + str.replace(/[\\/]/g, path.sep) export = defineRule({ meta: { @@ -25,24 +23,17 @@ export = defineRule({ return { 'ImportDeclaration[source.value="next/script"] > ImportDefaultSpecifier'( - node + node: any ) { scriptImportName = node.local.name }, JSXOpeningElement(node) { - let pathname = context.getFilename() + const pathname = convertToCorrectSeparator(context.getFilename()) - if (startsWithUsingCorrectSeparators(pathname, 'src/')) { - pathname = pathname.slice(4) - } else if (startsWithUsingCorrectSeparators(pathname, '/src/')) { - pathname = pathname.slice(5) - } + const isInAppDir = pathname.includes(`${path.sep}app${path.sep}`) // This rule shouldn't fire in `app/` - if ( - startsWithUsingCorrectSeparators(pathname, 'app/') || - startsWithUsingCorrectSeparators(pathname, '/app/') - ) { + if (isInAppDir) { return } diff --git a/test/unit/eslint-plugin-next/no-before-interactive-script-outside-document.test.ts b/test/unit/eslint-plugin-next/no-before-interactive-script-outside-document.test.ts index 945ed0925e..147b0043e9 100644 --- a/test/unit/eslint-plugin-next/no-before-interactive-script-outside-document.test.ts +++ b/test/unit/eslint-plugin-next/no-before-interactive-script-outside-document.test.ts @@ -109,59 +109,33 @@ ruleTester.run('no-before-interactive-script-outside-document', rule, { export default function Index() { return ( - + + {children} + + + {children} + - ); - }`, - filename: 'app/deep/randomFile.tsx', - }, - { - code: ` - import Script from "next/script"; - - export default function Index() { - return ( - - ); - }`, - filename: 'src/app/deep/randomFile.tsx', + filename: 'C:\\Users\\username\\projects\\project-name\\app\\layout.tsx', }, { code: ` @@ -178,13 +152,13 @@ ruleTester.run('no-before-interactive-script-outside-document', rule, { ); }`, - filename: '/src/app/layout.tsx', + filename: '/Users/user_name/projects/project-name/src/app/layout.tsx', }, { code: ` import Script from "next/script"; - export default function Index() { + export default function test() { return ( {children} @@ -195,7 +169,8 @@ ruleTester.run('no-before-interactive-script-outside-document', rule, { ); }`, - filename: '/app/layout.tsx', + filename: + 'C:\\Users\\username\\projects\\project-name\\src\\app\\layout.tsx', }, ].map((obj, idx) => ({ ...obj, @@ -249,6 +224,100 @@ ruleTester.run('no-before-interactive-script-outside-document', rule, { }, ], }, + { + code: ` + import Script from "next/script"; + + export default function Index() { + return ( + + {children} +