-
Notifications
You must be signed in to change notification settings - Fork 26.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Experimental: New ESLint Plugin (#11986)
- Loading branch information
1 parent
a795e48
commit 5ad512b
Showing
6 changed files
with
218 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
'no-sync-scripts': require('./rules/no-sync-scripts'), | ||
}, | ||
configs: { | ||
recommended: { | ||
rules: { | ||
'@next/next/no-css-tags': 1, | ||
'@next/next/no-sync-scripts': 1, | ||
}, | ||
}, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module.exports = function(context) { | ||
return { | ||
JSXOpeningElement(node) { | ||
if (node.name.name !== 'link') { | ||
return | ||
} | ||
if (node.attributes.length === 0) { | ||
return | ||
} | ||
|
||
if ( | ||
node.attributes.find( | ||
attr => attr.name.name === 'rel' && attr.value.value === 'stylesheet' | ||
) && | ||
node.attributes.find( | ||
attr => attr.name.name === 'href' && !/^https?/.test(attr.value.value) | ||
) | ||
) { | ||
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 = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 can impact your webpage's performance", | ||
}) | ||
} | ||
}, | ||
} | ||
} | ||
|
||
module.exports.schema = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"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/eslint-plugin-next" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
const rule = require('@next/eslint-plugin-next/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> | ||
); | ||
} | ||
}`, | ||
|
||
`import {Head} from 'next/document'; | ||
export class Blah extends Head { | ||
render() { | ||
return ( | ||
<div> | ||
<h1>Hello title</h1> | ||
<link href="https://fonts.googleapis.com/css?family=Open+Sans&display=swap" rel="stylesheet" /> | ||
</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', | ||
}, | ||
], | ||
}, | ||
], | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
const rule = require('@next/eslint-plugin-next/lib/rules/no-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 can impact your webpage's performance", | ||
type: 'JSXOpeningElement', | ||
}, | ||
], | ||
}, | ||
], | ||
}) |