Skip to content

Commit

Permalink
chore: add build step to eslint-plugin-next (#38647)
Browse files Browse the repository at this point in the history
Follow-up on #38534 (review)

This lets us use modern JS/TS syntax in ESLint rules and avoid issues like #38530 and #36693

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)


Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
  • Loading branch information
balazsorban44 and ijjk committed Sep 30, 2022
1 parent 05c2fe0 commit 73b4739
Show file tree
Hide file tree
Showing 50 changed files with 81 additions and 70 deletions.
11 changes: 11 additions & 0 deletions packages/eslint-plugin-next/.swcrc
@@ -0,0 +1,11 @@
{
"$schema": "https://json.schemastore.org/swcrc",
"jsc": {
"parser": {
"syntax": "typescript"
}
},
"module": {
"type": "commonjs"
}
}
43 changes: 0 additions & 43 deletions packages/eslint-plugin-next/lib/rules/no-img-element.js

This file was deleted.

10 changes: 7 additions & 3 deletions packages/eslint-plugin-next/package.json
Expand Up @@ -2,19 +2,23 @@
"name": "@next/eslint-plugin-next",
"version": "12.3.2-canary.15",
"description": "ESLint plugin for NextJS.",
"main": "lib/index.js",
"main": "dist/index.js",
"license": "MIT",
"repository": {
"url": "vercel/next.js",
"directory": "packages/eslint-plugin-next"
},
"files": [
"lib"
"dist"
],
"dependencies": {
"glob": "7.1.7"
},
"devDependencies": {
"@types/eslint": "7.28.0"
"eslint": "7.24.0"
},
"scripts": {
"build": "swc -d dist src",
"prepublishOnly": "cd ../../ && turbo run build"
}
}
File renamed without changes.
37 changes: 37 additions & 0 deletions packages/eslint-plugin-next/src/rules/no-img-element.ts
@@ -0,0 +1,37 @@
import type { Rule } from 'eslint'

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

export const meta: Rule.RuleMetaData = {
docs: {
description: 'Prevent usage of `<img>` element to prevent layout shift.',
category: 'HTML',
recommended: true,
url,
},
type: 'problem',
schema: [],
}

export const create: Rule.RuleModule['create'] = (context) => {
return {
JSXOpeningElement(node) {
if (node.name.name !== 'img') {
return
}

if (node.attributes.length === 0) {
return
}

if (node.parent?.parent?.openingElement?.name?.name === 'picture') {
return
}

context.report({
node,
message: `Do not use \`<img>\` element. Use \`<Image />\` from \`next/image\` instead. See: ${url}`,
})
},
}
}
Expand Up @@ -3,5 +3,6 @@
"module": "commonjs",
"target": "es2019"
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
}
4 changes: 2 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/unit/eslint-plugin-next/google-font-display.test.ts
@@ -1,4 +1,4 @@
import rule from '@next/eslint-plugin-next/lib/rules/google-font-display'
import rule from '@next/eslint-plugin-next/dist/rules/google-font-display'
import { RuleTester } from 'eslint'
;(RuleTester as any).setDefaultConfig({
parserOptions: {
Expand Down
@@ -1,4 +1,4 @@
import rule from '@next/eslint-plugin-next/lib/rules/google-font-preconnect'
import rule from '@next/eslint-plugin-next/dist/rules/google-font-preconnect'
import { RuleTester } from 'eslint'
;(RuleTester as any).setDefaultConfig({
parserOptions: {
Expand Down
5 changes: 3 additions & 2 deletions test/unit/eslint-plugin-next/index.test.ts
Expand Up @@ -3,7 +3,7 @@ import glob from 'glob'
import index from '@next/eslint-plugin-next'

const getRuleNameFromRulePath = (path) => basename(path, '.js')
const rulePaths = glob.sync('packages/eslint-plugin-next/lib/rules/*js', {
const rulePaths = glob.sync('packages/eslint-plugin-next/dist/rules/*js', {
absolute: true,
})

Expand All @@ -15,7 +15,8 @@ describe('@next/eslint-plugin-next index', () => {
})

rulePaths.forEach((rulePath) => {
const rule = require(rulePath)
let rule = require(rulePath)
rule = rule.default ?? rule
const ruleName = getRuleNameFromRulePath(rulePath)
const { recommended = false } = rule.meta.docs

Expand Down
2 changes: 1 addition & 1 deletion test/unit/eslint-plugin-next/inline-script-id.test.ts
@@ -1,4 +1,4 @@
import rule from '@next/eslint-plugin-next/lib/rules/inline-script-id'
import rule from '@next/eslint-plugin-next/dist/rules/inline-script-id'
import { RuleTester } from 'eslint'
;(RuleTester as any).setDefaultConfig({
parserOptions: {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/eslint-plugin-next/next-script-for-ga.test.ts
@@ -1,4 +1,4 @@
import rule from '@next/eslint-plugin-next/lib/rules/next-script-for-ga'
import rule from '@next/eslint-plugin-next/dist/rules/next-script-for-ga'
import { RuleTester } from 'eslint'
;(RuleTester as any).setDefaultConfig({
parserOptions: {
Expand Down
@@ -1,4 +1,4 @@
import rule from '@next/eslint-plugin-next/lib/rules/no-assign-module-variable'
import rule from '@next/eslint-plugin-next/dist/rules/no-assign-module-variable'
import { RuleTester } from 'eslint'
;(RuleTester as any).setDefaultConfig({
parserOptions: {
Expand Down
@@ -1,4 +1,4 @@
import rule from '@next/eslint-plugin-next/lib/rules/no-before-interactive-script-outside-document'
import rule from '@next/eslint-plugin-next/dist/rules/no-before-interactive-script-outside-document'
import { RuleTester } from 'eslint'
;(RuleTester as any).setDefaultConfig({
parserOptions: {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/eslint-plugin-next/no-css-tags.test.ts
@@ -1,4 +1,4 @@
import rule from '@next/eslint-plugin-next/lib/rules/no-css-tags'
import rule from '@next/eslint-plugin-next/dist/rules/no-css-tags'
import { RuleTester } from 'eslint'
;(RuleTester as any).setDefaultConfig({
parserOptions: {
Expand Down
@@ -1,6 +1,6 @@
import path from 'path'

import rule from '@next/eslint-plugin-next/lib/rules/no-document-import-in-page'
import rule from '@next/eslint-plugin-next/dist/rules/no-document-import-in-page'
import { RuleTester } from 'eslint'
;(RuleTester as any).setDefaultConfig({
parserOptions: {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/eslint-plugin-next/no-duplicate-head.test.ts
@@ -1,4 +1,4 @@
import rule from '@next/eslint-plugin-next/lib/rules/no-duplicate-head'
import rule from '@next/eslint-plugin-next/dist/rules/no-duplicate-head'
import { RuleTester } from 'eslint'
;(RuleTester as any).setDefaultConfig({
parserOptions: {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/eslint-plugin-next/no-head-element.test.ts
@@ -1,4 +1,4 @@
import rule from '@next/eslint-plugin-next/lib/rules/no-head-element'
import rule from '@next/eslint-plugin-next/dist/rules/no-head-element'
import { RuleTester } from 'eslint'
;(RuleTester as any).setDefaultConfig({
parserOptions: {
Expand Down
@@ -1,4 +1,4 @@
import rule from '@next/eslint-plugin-next/lib/rules/no-head-import-in-document'
import rule from '@next/eslint-plugin-next/dist/rules/no-head-import-in-document'
import { RuleTester } from 'eslint'
;(RuleTester as any).setDefaultConfig({
parserOptions: {
Expand Down
@@ -1,5 +1,5 @@
/* eslint-env jest */
import rule from '@next/eslint-plugin-next/lib/rules/no-html-link-for-pages'
import rule from '@next/eslint-plugin-next/dist/rules/no-html-link-for-pages'
import { Linter } from 'eslint'
import assert from 'assert'
import path from 'path'
Expand Down
2 changes: 1 addition & 1 deletion test/unit/eslint-plugin-next/no-img-element.test.ts
@@ -1,4 +1,4 @@
import rule from '@next/eslint-plugin-next/lib/rules/no-img-element'
import * as rule from '@next/eslint-plugin-next/dist/rules/no-img-element'
import { RuleTester } from 'eslint'
;(RuleTester as any).setDefaultConfig({
parserOptions: {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/eslint-plugin-next/no-page-custom-font.test.ts
@@ -1,4 +1,4 @@
import rule from '@next/eslint-plugin-next/lib/rules/no-page-custom-font'
import rule from '@next/eslint-plugin-next/dist/rules/no-page-custom-font'
import { RuleTester } from 'eslint'
;(RuleTester as any).setDefaultConfig({
parserOptions: {
Expand Down
@@ -1,4 +1,4 @@
import rule from '@next/eslint-plugin-next/lib/rules/no-script-component-in-head'
import rule from '@next/eslint-plugin-next/dist/rules/no-script-component-in-head'
import { RuleTester } from 'eslint'
;(RuleTester as any).setDefaultConfig({
parserOptions: {
Expand Down
@@ -1,4 +1,4 @@
import rule from '@next/eslint-plugin-next/lib/rules/no-styled-jsx-in-document'
import rule from '@next/eslint-plugin-next/dist/rules/no-styled-jsx-in-document'
import { RuleTester } from 'eslint'
;(RuleTester as any).setDefaultConfig({
parserOptions: {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/eslint-plugin-next/no-sync-scripts.test.ts
@@ -1,4 +1,4 @@
import rule from '@next/eslint-plugin-next/lib/rules/no-sync-scripts'
import rule from '@next/eslint-plugin-next/dist/rules/no-sync-scripts'
import { RuleTester } from 'eslint'
;(RuleTester as any).setDefaultConfig({
parserOptions: {
Expand Down
@@ -1,4 +1,4 @@
import rule from '@next/eslint-plugin-next/lib/rules/no-title-in-document-head'
import rule from '@next/eslint-plugin-next/dist/rules/no-title-in-document-head'
import { RuleTester } from 'eslint'
;(RuleTester as any).setDefaultConfig({
parserOptions: {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/eslint-plugin-next/no-typos.test.ts
@@ -1,4 +1,4 @@
import rule from '@next/eslint-plugin-next/lib/rules/no-typos'
import rule from '@next/eslint-plugin-next/dist/rules/no-typos'
import { RuleTester } from 'eslint'
;(RuleTester as any).setDefaultConfig({
parserOptions: {
Expand Down
@@ -1,4 +1,4 @@
import rule from '@next/eslint-plugin-next/lib/rules/no-unwanted-polyfillio'
import rule from '@next/eslint-plugin-next/dist/rules/no-unwanted-polyfillio'
import { RuleTester } from 'eslint'
;(RuleTester as any).setDefaultConfig({
parserOptions: {
Expand Down

0 comments on commit 73b4739

Please sign in to comment.