Skip to content

Commit

Permalink
Fix crash in no-page-custom-font eslint rule when default export is u…
Browse files Browse the repository at this point in the history
…nnamed. (#32251)

 fixes #32250
  • Loading branch information
bryanrsmith committed Dec 7, 2021
1 parent 836334e commit b7eeb6c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
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

0 comments on commit b7eeb6c

Please sign in to comment.