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 crash in no-page-custom-font eslint rule when default export is unnamed. #32251

Merged
merged 2 commits into from Dec 7, 2021
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
20 changes: 12 additions & 8 deletions packages/eslint-plugin-next/lib/rules/no-page-custom-font.js
Expand Up @@ -24,7 +24,7 @@ module.exports = {
page.startsWith(`${posix.sep}_document`)

let documentImportName
let localDefaultExportName
let localDefaultExportId
let exportDeclarationType

return {
Expand All @@ -43,7 +43,7 @@ module.exports = {
exportDeclarationType = node.declaration.type

if (node.declaration.type === 'FunctionDeclaration') {
localDefaultExportName = node.declaration.id.name
localDefaultExportId = node.declaration.id
return
}

Expand All @@ -52,7 +52,7 @@ module.exports = {
node.declaration.superClass &&
node.declaration.superClass.name === documentImportName
) {
localDefaultExportName = node.declaration.id.name
localDefaultExportId = node.declaration.id
}
},

Expand All @@ -66,15 +66,15 @@ module.exports = {
// if `export default <name>` is further down within the file after the
// currently traversed component, then `localDefaultExportName` will
// still be undefined
if (!localDefaultExportName) {
if (!localDefaultExportId) {
// find the top level of the module
const program = ancestors.find(
(ancestor) => ancestor.type === 'Program'
)

// go over each token to find the combination of `export default <name>`
for (let i = 0; i <= program.tokens.length - 1; i++) {
if (localDefaultExportName) {
if (localDefaultExportId) {
break
}

Expand All @@ -91,7 +91,7 @@ module.exports = {
const maybeIdentifier = program.tokens[i + 2]

if (maybeIdentifier && maybeIdentifier.type === 'Identifier') {
localDefaultExportName = maybeIdentifier.value
localDefaultExportId = { name: maybeIdentifier.value }
}
}
}
Expand All @@ -112,13 +112,13 @@ module.exports = {
if (exportDeclarationType === 'FunctionDeclaration') {
return (
ancestor.type === exportDeclarationType &&
ancestor.id.name === localDefaultExportName
isIdentifierMatch(ancestor.id, localDefaultExportId)
)
}

// function ...() {} export default ...
// class ... extends ...; export default ...
return ancestor.id && ancestor.id.name === localDefaultExportName
return isIdentifierMatch(ancestor.id, localDefaultExportId)
})

// file starts with _document and this <link /> is within the default export
Expand Down Expand Up @@ -153,3 +153,7 @@ module.exports = {
}
},
}

function isIdentifierMatch(id1, id2) {
return (id1 === null && id2 === null) || (id1 && id2 && id1.name === id2.name)
}
19 changes: 17 additions & 2 deletions test/unit/eslint-plugin-next/no-page-custom-font.test.ts
Expand Up @@ -105,10 +105,25 @@ ruleTester.run('no-page-custom-font', rule, {
);
}
}

export default MyDocument;`,
filename,
},
{
code: `export default function() {
return (
<Html>
<Head>
<link
href="https://fonts.googleapis.com/css2?family=Krona+One&display=swap"
rel="stylesheet"
/>
</Head>
</Html>
)
}`,
filename,
},
],

invalid: [
Expand Down Expand Up @@ -153,7 +168,7 @@ ruleTester.run('no-page-custom-font', rule, {
<link
href="https://fonts.googleapis.com/css2?family=Open+Sans"
rel="stylesheet"
/>
/>
</>
)
}
Expand Down