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

Experimental: New ESLint Plugin #11986

Merged
merged 20 commits into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
edabb95
Updating native-url version
janicklas-ralph Apr 4, 2020
c147099
Bump version
janicklas-ralph Apr 5, 2020
63b04ca
Merge branch 'canary' of github.com:zeit/next.js into canary
janicklas-ralph Apr 16, 2020
14ae8c3
Setup eslint plugin
janicklas-ralph Apr 17, 2020
07dd2bb
Merge branch 'canary' of github.com:zeit/next.js into eslint
janicklas-ralph Apr 17, 2020
5a1a989
Merge branch 'canary' of github.com:zeit/next.js into eslint
janicklas-ralph Apr 17, 2020
3b52bfc
Merge branch 'canary' of github.com:zeit/next.js into eslint
janicklas-ralph Apr 22, 2020
7891cca
Fix conflicts and review comments
janicklas-ralph Apr 22, 2020
7e090a4
Merge branch 'canary' of github.com:zeit/next.js into eslint
janicklas-ralph May 4, 2020
3eb1999
Merge branch 'canary' of github.com:zeit/next.js into eslint
janicklas-ralph May 4, 2020
0baa620
Merge branch 'canary' of github.com:zeit/next.js into eslint
janicklas-ralph May 4, 2020
22007a7
Merge branch 'canary' of github.com:zeit/next.js into eslint
janicklas-ralph May 4, 2020
0517bff
Make No-css-tags check for first party files only
janicklas-ralph May 5, 2020
0bae2f1
Merge branch 'canary' of github.com:zeit/next.js into eslint
janicklas-ralph May 5, 2020
b02a0a3
Removing mocha from eslint plugin
janicklas-ralph May 5, 2020
d6923b0
Merge branch 'canary' of github.com:zeit/next.js into eslint
janicklas-ralph May 5, 2020
e679d66
Merge branch 'canary' into eslint
Timer May 6, 2020
72d65e0
Merge branch 'canary' into eslint
Timer May 7, 2020
4679168
Rename directory
Timer May 7, 2020
86c7be3
Move tests to correct location
Timer May 7, 2020
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
10 changes: 10 additions & 0 deletions packages/next-plugin-eslint/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module",
"ecmaFeatures": {
"modules": true,
"jsx": true
}
}
}
14 changes: 14 additions & 0 deletions packages/next-plugin-eslint/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
rules: {
'no-css-tags': require('./rules/no-css-tags'),
'sync-scripts': require('./rules/sync-scripts'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit(just for convention sake): no-sync-scripts

},
configs: {
recommended: {
rules: {
'@next/next/no-css-tags': 2,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets start with 1 and then promote it to 2

'@next/next/sync-scripts': 2,
},
},
},
}
26 changes: 26 additions & 0 deletions packages/next-plugin-eslint/lib/rules/no-css-tags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module.exports = function(context) {
return {
JSXOpeningElement(node) {
if (node.name.name !== 'link') {
return
}
if (node.attributes.length === 0) {
return
}

if (
janicklas-ralph marked this conversation as resolved.
Show resolved Hide resolved
node.attributes.find(
attr => attr.name.name === 'rel' && attr.value.value === 'stylesheet'
)
) {
context.report({
node,
message:
'In order to use external stylesheets use @import in the root stylesheet compiled with NextJS. This ensures proper priority to CSS when loading a webpage.',
})
}
},
}
}

module.exports.schema = []
27 changes: 27 additions & 0 deletions packages/next-plugin-eslint/lib/rules/sync-scripts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module.exports = function(context) {
return {
JSXOpeningElement(node) {
if (node.name.name !== 'script') {
return
}
if (node.attributes.length === 0) {
return
}

if (
node.attributes.find(attr => attr.name.name === 'src') &&
!node.attributes.find(
attr => attr.name.name === 'async' || attr.name.name === 'defer'
)
) {
context.report({
node,
message:
"A synchronous script tag in head, can impact your webpage's performance",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are no more looking in head right?

})
}
},
}
}

module.exports.schema = []
17 changes: 17 additions & 0 deletions packages/next-plugin-eslint/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "@next/eslint-plugin-next",
"version": "0.0.0",
"description": "ESLint plugin for NextJS.",
"main": "lib/index.js",
"license": "MIT",
"repository": {
"url": "zeit/next.js",
"directory": "packages/next-plugin-eslint"
},
"scripts": {
"test": "mocha tests --recursive"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we'll want to use jest or our global test suite instead of having it inside the package

},
"devDependencies": {
"mocha": "^3.1.2"
}
}
68 changes: 68 additions & 0 deletions packages/next-plugin-eslint/tests/lib/rules/no-css-tags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const rule = require('../../../lib/rules/no-css-tags')
const RuleTester = require('eslint').RuleTester

RuleTester.setDefaultConfig({
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
ecmaFeatures: {
modules: true,
jsx: true,
},
},
})

var ruleTester = new RuleTester()
ruleTester.run('no-css-tags', rule, {
valid: [
`import {Head} from 'next/document';

export class Blah extends Head {
render() {
return (
<div>
<h1>Hello title</h1>
</div>
);
}
}`,
],

invalid: [
{
code: `
import {Head} from 'next/document';

export class Blah extends Head {
render() {
return (
<div>
<h1>Hello title</h1>
<link href="/_next/static/css/styles.css" rel="stylesheet" />
</div>
);
}
}`,
errors: [
{
message:
'In order to use external stylesheets use @import in the root stylesheet compiled with NextJS. This ensures proper priority to CSS when loading a webpage.',
type: 'JSXOpeningElement',
},
],
},
{
code: `
<div>
<link href="/_next/static/css/styles.css" rel="stylesheet" />
</div>`,
errors: [
{
message:
'In order to use external stylesheets use @import in the root stylesheet compiled with NextJS. This ensures proper priority to CSS when loading a webpage.',
type: 'JSXOpeningElement',
},
],
},
],
})
56 changes: 56 additions & 0 deletions packages/next-plugin-eslint/tests/lib/rules/sync-scripts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const rule = require('../../../lib/rules/sync-scripts')
const RuleTester = require('eslint').RuleTester

RuleTester.setDefaultConfig({
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
ecmaFeatures: {
modules: true,
jsx: true,
},
},
})

var ruleTester = new RuleTester()
ruleTester.run('sync-scripts', rule, {
valid: [
`import {Head} from 'next/document';

export class Blah extends Head {
render() {
return (
<div>
<h1>Hello title</h1>
<script src='https://blah.com' async></script>
</div>
);
}
}`,
],

invalid: [
{
code: `
import {Head} from 'next/document';

export class Blah extends Head {
render() {
return (
<div>
<h1>Hello title</h1>
<script src='https://blah.com'></script>
</div>
);
}
}`,
errors: [
{
message:
"A synchronous script tag in head, can impact your webpage's performance",
type: 'JSXOpeningElement',
},
],
},
],
})
Loading