This repository has been archived by the owner on Sep 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
feat: adds no-global-fetch rule to avoid direct usage of fetch in favour of our thin wrapper #62
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
49 changes: 49 additions & 0 deletions
49
plugins/eslint-plugin-liferay-portal/docs/rules/no-global-fetch.md
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,49 @@ | ||
# Disallow use of global fetch (no-global-fetch) | ||
|
||
This rule guards against the direct use of the `fetch` API. As a secured-environment, Liferay Portal requests often rely on specific security headers and tokens being set in the requests. The `frontend-js-web` module offers a thin wrapper around `fetch` that takes care of the most common configuration to avoid issues. | ||
|
||
## Rule Details | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
function doSomething(url) { | ||
return fetch(foo); | ||
} | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
import {fetch} from 'frontend-js-web'; | ||
|
||
function doSomething(url) { | ||
return fetch(foo); | ||
} | ||
|
||
function doSomethingElse(url) { | ||
return Liferay.Util.fetch(url); | ||
} | ||
``` | ||
|
||
## Further Reading | ||
|
||
- [LPS-94712 Make a utility that encapsulates common 'fetch' configuration and use](https://issues.liferay.com/browse/LPS-94712) | ||
- [LPS-96778 Standardize Ajax requests in frontend-js-web](https://issues.liferay.com/browse/LPS-96778) | ||
- [LPS-96933 Standardize Ajax requests in frontend-js-aui-web](https://issues.liferay.com/browse/LPS-96933) | ||
- [LPS-98655 Extend 'fetch' utility to set 'x-csrf-token' header by default](https://issues.liferay.com/browse/LPS-98655) | ||
- [LPS-98888 Standardize Ajax requests in layout modules](https://issues.liferay.com/browse/LPS-98888) | ||
- [LPS-98940 Standardize Ajax requests in collaboration modules #1](https://issues.liferay.com/browse/LPS-98940) | ||
- [LPS-98941 Standardize Ajax requests in collaboration modules #2](https://issues.liferay.com/browse/LPS-98941) | ||
- [LPS-98942 Standardize Ajax requests in collaboration modules #3](https://issues.liferay.com/browse/LPS-98942) | ||
- [LPS-98946 Standardize Ajax requests in wem1 (asset, mobile-device-rules, journal)](https://issues.liferay.com/browse/LPS-98946) | ||
- [LPS-98947 Standardize Ajax requests in users modules](https://issues.liferay.com/browse/LPS-98947) | ||
- [LPS-98948 Standardize Ajax requests in site modules](https://issues.liferay.com/browse/LPS-98948) | ||
- [LPS-98949 Standardize Ajax requests in segments](https://issues.liferay.com/browse/LPS-98949) | ||
- [LPS-98953 Standardize Ajax requests in change-tracking modules](https://issues.liferay.com/browse/LPS-98953) | ||
- [LPS-98962 Standardize Ajax requests in dynamic-data-mapping](https://issues.liferay.com/browse/LPS-98962) | ||
- [LPS-99339 Standardize Ajax requests in frontend-js-aui-web, part 2](https://issues.liferay.com/browse/LPS-99339) | ||
- [LPS-99414 Standardize Ajax requests in common #1](https://issues.liferay.com/browse/LPS-99414) | ||
- [LPS-99610 Standardize Ajax requests in common #2](https://issues.liferay.com/browse/LPS-99610) | ||
- [LPS-99611 Standardize Ajax requests in product-navigation](https://issues.liferay.com/browse/LPS-99611) | ||
- [LPS-99612 Standardize Ajax requests in staging and export-import](https://issues.liferay.com/browse/LPS-99612) |
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
57 changes: 57 additions & 0 deletions
57
plugins/eslint-plugin-liferay-portal/lib/rules/no-global-fetch.js
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 @@ | ||
/** | ||
* © 2017 Liferay, Inc. <https://liferay.com> | ||
* | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
const DESCRIPTION = | ||
'Direct usage of the `fetch` API is discouraged in favour of our wrapped version that takes care of the necessary security configuration; import fetch from frontend-js-web instead'; | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: DESCRIPTION, | ||
category: 'Best Practices', | ||
recommended: false, | ||
url: 'https://issues.liferay.com/browse/LPS-98283', | ||
}, | ||
fixable: null, | ||
messages: { | ||
noGlobalFetch: DESCRIPTION, | ||
}, | ||
schema: [], | ||
type: 'problem', | ||
}, | ||
|
||
create(context) { | ||
let foundFetchImport = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is sufficiently scoped per-context... if it's not, we can store this using some additional |
||
|
||
return { | ||
CallExpression(node) { | ||
if ( | ||
node.callee.type === 'Identifier' && | ||
node.callee.name === 'fetch' && | ||
!foundFetchImport | ||
) { | ||
context.report({ | ||
messageId: 'noGlobalFetch', | ||
node, | ||
}); | ||
} | ||
}, | ||
|
||
ImportDeclaration(node) { | ||
if ( | ||
node.source && | ||
node.source.type === 'Literal' && | ||
node.source.value === 'frontend-js-web' && | ||
node.specifiers.find( | ||
specifier => specifier.imported.name === 'fetch' | ||
) | ||
) { | ||
foundFetchImport = true; | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
56 changes: 56 additions & 0 deletions
56
plugins/eslint-plugin-liferay-portal/tests/lib/rules/no-global-fetch.js
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,56 @@ | ||
/** | ||
* © 2017 Liferay, Inc. <https://liferay.com> | ||
* | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
const {RuleTester} = require('eslint'); | ||
const rule = require('../../../lib/rules/no-global-fetch'); | ||
|
||
const parserOptions = { | ||
parserOptions: { | ||
ecmaVersion: 6, | ||
sourceType: 'module', | ||
}, | ||
}; | ||
|
||
const ruleTester = new RuleTester(parserOptions); | ||
|
||
ruleTester.run('no-global-fetch', rule, { | ||
valid: [ | ||
{ | ||
// Imported from frontend-js-web | ||
code: ` | ||
import {fetch} from 'frontend-js-web'; | ||
|
||
function doSomething(url) { | ||
return fetch(url); | ||
} | ||
`, | ||
}, | ||
{ | ||
// Namespaced from Liferay.Util | ||
code: ` | ||
function doSomething(url) { | ||
return Liferay.Util.fetch(url); | ||
} | ||
`, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
// As a global fetch without an import. | ||
code: ` | ||
function doSomething(url) { | ||
return fetch(url); | ||
}; | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'noGlobalFetch', | ||
type: 'CallExpression', | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, obviously we need a Markdown linter to enforce order here. 😂