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

refactor: migrate eslint-plugin-next to typescript #41046

Merged
merged 3 commits into from
Sep 30, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/eslint-plugin-next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"glob": "7.1.7"
},
"devDependencies": {
"@types/glob": "7.1.1",
"eslint": "7.24.0"
},
"scripts": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const NodeAttributes = require('../utils/node-attributes.js')
import { defineRule } from '../utils/define-rule'
import NodeAttributes from '../utils/node-attributes'

const url = 'https://nextjs.org/docs/messages/google-font-display'

module.exports = {
export = defineRule({
balazsorban44 marked this conversation as resolved.
Show resolved Hide resolved
meta: {
docs: {
description: 'Enforce font-display behavior with Google Fonts.',
Expand All @@ -12,10 +13,10 @@ module.exports = {
type: 'problem',
schema: [],
},
create: function (context) {
create(context) {
return {
JSXOpeningElement(node) {
let message
let message: string | undefined

if (node.name.name !== 'link') {
return
Expand Down Expand Up @@ -58,4 +59,4 @@ module.exports = {
},
}
},
}
})
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const NodeAttributes = require('../utils/node-attributes.js')
import { defineRule } from '../utils/define-rule'
import NodeAttributes from '../utils/node-attributes'

const url = 'https://nextjs.org/docs/messages/google-font-preconnect'

module.exports = {
export = defineRule({
meta: {
docs: {
description: 'Ensure `preconnect` is used with Google Fonts.',
Expand All @@ -12,7 +13,7 @@ module.exports = {
type: 'problem',
schema: [],
},
create: function (context) {
create(context) {
return {
JSXOpeningElement(node) {
if (node.name.name !== 'link') {
Expand Down Expand Up @@ -43,4 +44,4 @@ module.exports = {
},
}
},
}
})
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { defineRule } from '../utils/define-rule'

const url = 'https://nextjs.org/docs/messages/inline-script-id'

module.exports = {
export = defineRule({
meta: {
docs: {
description:
Expand All @@ -11,7 +13,7 @@ module.exports = {
type: 'problem',
schema: [],
},
create: function (context) {
create(context) {
let nextScriptImportName = null

return {
Expand Down Expand Up @@ -70,4 +72,4 @@ module.exports = {
},
}
},
}
})
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const NodeAttributes = require('../utils/node-attributes.js')
import { defineRule } from '../utils/define-rule'
import NodeAttributes from '../utils/node-attributes'

const SUPPORTED_SRCS = [
'www.google-analytics.com/analytics.js',
Expand All @@ -18,7 +19,7 @@ const containsStr = (str, strList) => {
return strList.some((s) => str.includes(s))
}

module.exports = {
export = defineRule({
meta: {
docs: {
description,
Expand All @@ -28,7 +29,7 @@ module.exports = {
type: 'problem',
schema: [],
},
create: function (context) {
create(context) {
return {
JSXOpeningElement(node) {
if (node.name.name !== 'script') {
Expand Down Expand Up @@ -76,4 +77,4 @@ module.exports = {
},
}
},
}
})
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { defineRule } from '../utils/define-rule'
const url = 'https://nextjs.org/docs/messages/no-assign-module-variable'

module.exports = {
export = defineRule({
meta: {
docs: {
description: 'Prevent assignment to the `module` variable.',
Expand All @@ -11,13 +12,16 @@ module.exports = {
schema: [],
},

create: function (context) {
create(context) {
return {
VariableDeclaration(node) {
// Checks node.declarations array for variable with id.name of `module`
const moduleVariableFound = node.declarations.some(
(declaration) => declaration.id.name === 'module'
)
const moduleVariableFound = node.declarations.some((declaration) => {
if ('name' in declaration.id) {
return declaration.id.name === 'module'
}
return false
})

// Return early if no `module` variable is found
if (!moduleVariableFound) {
Expand All @@ -31,4 +35,4 @@ module.exports = {
},
}
},
}
})
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const path = require('path')
import { defineRule } from '../utils/define-rule'
import * as path from 'path'
balazsorban44 marked this conversation as resolved.
Show resolved Hide resolved

const url =
'https://nextjs.org/docs/messages/no-before-interactive-script-outside-document'

module.exports = {
export = defineRule({
meta: {
docs: {
description:
Expand All @@ -14,7 +15,7 @@ module.exports = {
type: 'problem',
schema: [],
},
create: function (context) {
create(context) {
let scriptImportName = null

return {
Expand Down Expand Up @@ -56,4 +57,4 @@ module.exports = {
},
}
},
}
})
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { defineRule } from '../utils/define-rule'
const url = 'https://nextjs.org/docs/messages/no-css-tags'

module.exports = {
export = defineRule({
meta: {
docs: {
description: 'Prevent manual stylesheet tags.',
Expand All @@ -10,7 +11,7 @@ module.exports = {
type: 'problem',
schema: [],
},
create: function (context) {
create(context) {
return {
JSXOpeningElement(node) {
if (node.name.name !== 'link') {
Expand Down Expand Up @@ -43,4 +44,4 @@ module.exports = {
},
}
},
}
})
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const path = require('path')
import { defineRule } from '../utils/define-rule'
import * as path from 'path'

const url = 'https://nextjs.org/docs/messages/no-document-import-in-page'

module.exports = {
export = defineRule({
meta: {
docs: {
description:
Expand All @@ -13,7 +14,7 @@ module.exports = {
type: 'problem',
schema: [],
},
create: function (context) {
create(context) {
return {
ImportDeclaration(node) {
if (node.source.value !== 'next/document') {
Expand All @@ -38,4 +39,4 @@ module.exports = {
},
}
},
}
})
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { defineRule } from '../utils/define-rule'
const url = 'https://nextjs.org/docs/messages/no-duplicate-head'

module.exports = {
export = defineRule({
meta: {
docs: {
description:
Expand All @@ -11,7 +12,7 @@ module.exports = {
type: 'problem',
schema: [],
},
create: function (context) {
create(context) {
let documentImportName
return {
ImportDeclaration(node) {
Expand All @@ -30,14 +31,21 @@ module.exports = {
(ancestorNode) =>
ancestorNode.type === 'ClassDeclaration' &&
ancestorNode.superClass &&
'name' in ancestorNode.superClass &&
ancestorNode.superClass.name === documentImportName
)

if (!documentClass) {
return
}

if (node.argument && node.argument.children) {
// @ts-expect-error - `node.argument` could be a `JSXElement` which has property `children`
if (
node.argument &&
'children' in node.argument &&
node.argument.children
) {
// @ts-expect-error - `node.argument` could be a `JSXElement` which has property `children`
const headComponents = node.argument.children.filter(
(childrenNode) =>
childrenNode.openingElement &&
Expand All @@ -57,4 +65,4 @@ module.exports = {
},
}
},
}
})
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { defineRule } from '../utils/define-rule'

const url = 'https://nextjs.org/docs/messages/no-head-element'

module.exports = {
export = defineRule({
meta: {
docs: {
description: 'Prevent usage of `<head>` element.',
Expand All @@ -11,7 +13,7 @@ module.exports = {
type: 'problem',
schema: [],
},
create: function (context) {
create(context) {
return {
JSXOpeningElement(node) {
const paths = context.getFilename()
Expand All @@ -29,4 +31,4 @@ module.exports = {
},
}
},
}
})
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const path = require('path')
import { defineRule } from '../utils/define-rule'
import * as path from 'path'

const url = 'https://nextjs.org/docs/messages/no-head-import-in-document'

module.exports = {
export = defineRule({
meta: {
docs: {
description: 'Prevent usage of `next/head` in `pages/_document.js`.',
Expand All @@ -12,7 +13,7 @@ module.exports = {
type: 'problem',
schema: [],
},
create: function (context) {
create(context) {
return {
ImportDeclaration(node) {
if (node.source.value !== 'next/head') {
Expand All @@ -38,4 +39,4 @@ module.exports = {
},
}
},
}
})
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// @ts-check
const path = require('path')
const fs = require('fs')
const getRootDir = require('../utils/get-root-dirs')
const {
import { defineRule } from '../utils/define-rule'
import * as path from 'path'
import * as fs from 'fs'
import { getRootDirs } from '../utils/get-root-dirs'

import {
getUrlFromPagesDirectories,
normalizeURL,
execOnce,
} = require('../utils/url')
} from '../utils/url'

const pagesDirWarning = execOnce((pagesDirs) => {
console.warn(
Expand All @@ -21,7 +22,7 @@ const fsExistsSyncCache = {}

const url = 'https://nextjs.org/docs/messages/no-html-link-for-pages'

module.exports = {
export = defineRule({
meta: {
docs: {
description:
Expand Down Expand Up @@ -51,16 +52,12 @@ module.exports = {

/**
* Creates an ESLint rule listener.
*
* @param {import('eslint').Rule.RuleContext} context - ESLint rule context
* @returns {import('eslint').Rule.RuleListener} An ESLint rule listener
*/
create: function (context) {
/** @type {(string|string[])[]} */
const ruleOptions = context.options
create(context) {
const ruleOptions: (string | string[])[] = context.options
const [customPagesDirectory] = ruleOptions

const rootDirs = getRootDir(context)
const rootDirs = getRootDirs(context)

const pagesDirs = (
customPagesDirectory
Expand Down Expand Up @@ -135,4 +132,4 @@ module.exports = {
},
}
},
}
})
Loading