Unverified Commit 3b2732bc authored by kaykdm's avatar kaykdm Committed by GitHub

[ESLint] add no-typos rule to eslint (#26650)

* add no-typos rule to eslint

* return early when function name is correct

* Handle null decl.type

* update check
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent 0d49d9f2
...@@ -75,7 +75,7 @@ yarn lint --dir pages --dir utils ...@@ -75,7 +75,7 @@ yarn lint --dir pages --dir utils
Next.js provides an ESLint plugin, [`eslint-plugin-next`](https://www.npmjs.com/package/@next/eslint-plugin-next), making it easier to catch common issues and problems in a Next.js application. The full set of rules is as follows: Next.js provides an ESLint plugin, [`eslint-plugin-next`](https://www.npmjs.com/package/@next/eslint-plugin-next), making it easier to catch common issues and problems in a Next.js application. The full set of rules is as follows:
| | Rule | Description | | | Rule | Description |
| :-: | ---------------------------------------------------------------------------------------------- | ---------------------------------------------------------------- | | :-: | ---------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------- |
| ✔️ | [next/google-font-display](https://nextjs.org/docs/messages/google-font-display) | Enforce optional or swap font-display behavior with Google Fonts | | ✔️ | [next/google-font-display](https://nextjs.org/docs/messages/google-font-display) | Enforce optional or swap font-display behavior with Google Fonts |
| ✔️ | [next/google-font-preconnect](https://nextjs.org/docs/messages/google-font-preconnect) | Enforce preconnect usage with Google Fonts | | ✔️ | [next/google-font-preconnect](https://nextjs.org/docs/messages/google-font-preconnect) | Enforce preconnect usage with Google Fonts |
| ✔️ | [next/link-passhref](https://nextjs.org/docs/messages/link-passhref) | Enforce passHref prop usage with custom Link components | | ✔️ | [next/link-passhref](https://nextjs.org/docs/messages/link-passhref) | Enforce passHref prop usage with custom Link components |
...@@ -88,6 +88,7 @@ Next.js provides an ESLint plugin, [`eslint-plugin-next`](https://www.npmjs.com/ ...@@ -88,6 +88,7 @@ Next.js provides an ESLint plugin, [`eslint-plugin-next`](https://www.npmjs.com/
| ✔️ | [next/no-sync-scripts](https://nextjs.org/docs/messages/no-sync-scripts) | Forbid synchronous scripts | | ✔️ | [next/no-sync-scripts](https://nextjs.org/docs/messages/no-sync-scripts) | Forbid synchronous scripts |
| ✔️ | [next/no-title-in-document-head](https://nextjs.org/docs/messages/no-title-in-document-head) | Disallow using &lt;title&gt; with Head from next/document | | ✔️ | [next/no-title-in-document-head](https://nextjs.org/docs/messages/no-title-in-document-head) | Disallow using &lt;title&gt; with Head from next/document |
| ✔️ | [next/no-unwanted-polyfillio](https://nextjs.org/docs/messages/no-unwanted-polyfillio) | Prevent duplicate polyfills from Polyfill.io | | ✔️ | [next/no-unwanted-polyfillio](https://nextjs.org/docs/messages/no-unwanted-polyfillio) | Prevent duplicate polyfills from Polyfill.io |
| ✔️ | next/no-typos | Ensure no typos were made declaring [Next.js's data fetching function](https://nextjs.org/docs/basic-features/data-fetching) |
- ✔: Enabled in the recommended configuration - ✔: Enabled in the recommended configuration
......
...@@ -12,6 +12,7 @@ module.exports = { ...@@ -12,6 +12,7 @@ module.exports = {
'link-passhref': require('./rules/link-passhref'), 'link-passhref': require('./rules/link-passhref'),
'no-document-import-in-page': require('./rules/no-document-import-in-page'), 'no-document-import-in-page': require('./rules/no-document-import-in-page'),
'no-head-import-in-document': require('./rules/no-head-import-in-document'), 'no-head-import-in-document': require('./rules/no-head-import-in-document'),
'no-typos': require('./rules/no-typos'),
'no-duplicate-head': require('./rules/no-duplicate-head'), 'no-duplicate-head': require('./rules/no-duplicate-head'),
}, },
configs: { configs: {
...@@ -30,6 +31,7 @@ module.exports = { ...@@ -30,6 +31,7 @@ module.exports = {
'@next/next/link-passhref': 1, '@next/next/link-passhref': 1,
'@next/next/no-document-import-in-page': 2, '@next/next/no-document-import-in-page': 2,
'@next/next/no-head-import-in-document': 2, '@next/next/no-head-import-in-document': 2,
'@next/next/no-typos': 1,
'@next/next/no-duplicate-head': 2, '@next/next/no-duplicate-head': 2,
}, },
}, },
......
const path = require('path')
const NEXT_EXPORT_FUNCTIONS = [
'getStaticProps',
'getStaticPaths',
'getServerSideProps',
]
// 0 is the exact match
const THRESHOLD = 1
// the minimum number of operations required to convert string a to string b.
function minDistance(a, b) {
const m = a.length
const n = b.length
if (m < n) {
return minDistance(b, a)
}
if (n === 0) {
return m
}
let previousRow = Array.from({ length: n + 1 }, (_, i) => i)
for (let i = 0; i < m; i++) {
const s1 = a[i]
let currentRow = [i + 1]
for (let j = 0; j < n; j++) {
const s2 = b[j]
const insertions = previousRow[j + 1] + 1
const deletions = currentRow[j] + 1
const substitutions = previousRow[j] + Number(s1 !== s2)
currentRow.push(Math.min(insertions, deletions, substitutions))
}
previousRow = currentRow
}
return previousRow[previousRow.length - 1]
}
module.exports = {
meta: {
docs: {
description: 'Prevent common typos',
category: 'Stylistic Issues',
recommended: true,
},
fixable: null,
},
create: function (context) {
function checkTypos(node, name) {
if (NEXT_EXPORT_FUNCTIONS.includes(name)) {
return
}
const potentialTypos = NEXT_EXPORT_FUNCTIONS.map((o) => ({
option: o,
distance: minDistance(o, name),
}))
.filter(({ distance }) => distance <= THRESHOLD && distance > 0)
.sort((a, b) => a.distance - b.distance)
if (potentialTypos.length) {
context.report({
node,
message: `${name} may be a typo. Did you mean ${potentialTypos[0].option}?`,
})
}
}
return {
ExportNamedDeclaration(node) {
const page = context.getFilename().split('pages')[1]
if (!page || path.parse(page).dir.startsWith('/api')) {
return
}
const decl = node.declaration
if (!decl) {
return
}
switch (decl.type) {
case 'FunctionDeclaration': {
checkTypos(node, decl.id.name)
break
}
case 'VariableDeclaration': {
decl.declarations.forEach((d) => {
if (d.id.type !== 'Identifier') {
return
}
checkTypos(node, d.id.name)
})
break
}
default: {
break
}
}
return
},
}
},
}
const rule = require('@next/eslint-plugin-next/lib/rules/no-typos')
const RuleTester = require('eslint').RuleTester
RuleTester.setDefaultConfig({
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
ecmaFeatures: {
modules: true,
jsx: true,
},
},
})
var ruleTester = new RuleTester()
ruleTester.run('no-typos', rule, {
valid: [
`
export default function Page() {
return <div></div>;
}
export const getStaticPaths = async () => {};
export const getStaticProps = async () => {};
`,
`
export default function Page() {
return <div></div>;
}
export const getServerSideProps = async () => {};
`,
`
export default function Page() {
return <div></div>;
}
export async function getStaticPaths() {};
export async function getStaticProps() {};
`,
`
export default function Page() {
return <div></div>;
}
export async function getServerSideProps() {};
`,
// detect only typo that is one operation away from the correct one
`
export default function Page() {
return <div></div>;
}
export async function getServerSidePropsss() {};
`,
`
export default function Page() {
return <div></div>;
}
export async function getstatisPath() {};
`,
],
invalid: [
{
code: `
export default function Page() {
return <div></div>;
}
export const getStaticpaths = async () => {};
export const getStaticProps = async () => {};
`,
filename: 'pages/index.js',
errors: [
{
message: 'getStaticpaths may be a typo. Did you mean getStaticPaths?',
type: 'ExportNamedDeclaration',
},
],
},
{
code: `
export default function Page() {
return <div></div>;
}
export async function getStaticPathss(){};
export async function getStaticPropss(){};
`,
filename: 'pages/index.js',
errors: [
{
message:
'getStaticPathss may be a typo. Did you mean getStaticPaths?',
type: 'ExportNamedDeclaration',
},
{
message:
'getStaticPropss may be a typo. Did you mean getStaticProps?',
type: 'ExportNamedDeclaration',
},
],
},
{
code: `
export default function Page() {
return <div></div>;
}
export async function getServurSideProps(){};
`,
filename: 'pages/index.js',
errors: [
{
message:
'getServurSideProps may be a typo. Did you mean getServerSideProps?',
type: 'ExportNamedDeclaration',
},
],
},
{
code: `
export default function Page() {
return <div></div>;
}
export const getServurSideProps = () => {};
`,
filename: 'pages/index.js',
errors: [
{
message:
'getServurSideProps may be a typo. Did you mean getServerSideProps?',
type: 'ExportNamedDeclaration',
},
],
},
],
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment