-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: logic for use-download-and-update-url -> require-download-url (#80)
- Loading branch information
1 parent
723ab65
commit 8c5dc10
Showing
10 changed files
with
100 additions
and
106 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
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
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
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,58 @@ | ||
# `require-download-url` | ||
|
||
> ✅ The "extends": "plugin:userscripts/recommended" property in a configuration | ||
> file enables this rule. | ||
<!-- markdownlint-disable-next-line no-blanks-blockquote --> | ||
|
||
> 🔧 The `--fix` option on the command line can automatically fix some of the | ||
> problems reported by this rule. | ||
The `require-download-url` rule verifies that if the `updateURL` attribute | ||
is present then the `downloadURL` attribute is too. | ||
|
||
## Why? | ||
|
||
The purpose of the `updateURL` attribute is to specify the metadata endpoint for | ||
a userscript while a `downloadURL` represents the source endpoint. Several | ||
userscript managers require that a `downloadURL` is present for updates as they | ||
don't fallback to the `updateURL` for source downloads and use it solely for | ||
metadata downloads. For more information, check out this GitHub issue ([#79](https://github.com/Yash-Singh1/eslint-plugin-userscripts/issues/79)). | ||
|
||
## Examples | ||
|
||
👍 Examples of **correct** code for this rule | ||
|
||
```js | ||
/* eslint userscripts/use-homepage-and-url: "error" */ | ||
|
||
// ==UserScript== | ||
// @updateURL example.com | ||
// @downloadURL example.com | ||
// ==/UserScript== | ||
``` | ||
|
||
```js | ||
/* eslint userscripts/use-homepage-and-url: "error" */ | ||
|
||
// ==UserScript== | ||
// @downloadURL example.com | ||
// ==/UserScript== | ||
``` | ||
|
||
👎︎ Examples of **incorrect** code for this rule | ||
|
||
```js | ||
/* eslint userscripts/use-homepage-and-url: "error" */ | ||
|
||
// ==UserScript== | ||
// @updateURL example.com | ||
// ==/UserScript== | ||
``` | ||
|
||
## When Not to Use It | ||
|
||
One good reason to leave out this rule would be if you are developing for Greasemonkey | ||
specifically, because Greasemonkey doesn't respect attributes regarding update mechanisms | ||
and simply use the URL from where the userscript was downloaded initially as the | ||
source. |
This file was deleted.
Oops, something went wrong.
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
17 changes: 6 additions & 11 deletions
17
lib/rules/use-download-and-update-url.js → lib/rules/require-download-url.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 |
---|---|---|
@@ -1,34 +1,29 @@ | ||
const createValidator = require('../utils/createValidator'); | ||
|
||
const updateURLs = ['downloadURL', 'updateURL']; | ||
|
||
module.exports = createValidator({ | ||
name: updateURLs, | ||
name: 'updateURL', | ||
validator: ({ attrVal, metadata, context, keyName }) => { | ||
const attribute = updateURLs.find((updateURL) => updateURL !== keyName); | ||
if (!metadata[attribute]) { | ||
if (keyName === 'updateURL' && !metadata['downloadURL']) { | ||
context.report({ | ||
loc: attrVal.loc, | ||
messageId: 'missingAttribute', | ||
data: { | ||
attribute: attribute | ||
}, | ||
messageId: 'includeDownloadURL', | ||
fix: function (fixer) { | ||
return fixer.insertTextAfterRange( | ||
attrVal.comment.range, | ||
`\n${context | ||
.getSourceCode() | ||
.lines[attrVal.comment.loc.start.line - 1].replace( | ||
/^(\s*\/\/\s*@)\S*/, | ||
'$1' + attribute | ||
'$1downloadURL' | ||
)}` | ||
); | ||
} | ||
}); | ||
} | ||
}, | ||
messages: { | ||
missingAttribute: "Didn't find attribute '{{ attribute }}' in the metadata" | ||
includeDownloadURL: | ||
"If you are using 'updateURL', you must also use 'downloadURL'" | ||
}, | ||
fixable: true | ||
}); |
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
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,31 @@ | ||
const rule = require('..')['require-download-url']; | ||
const RuleTester = require('eslint').RuleTester; | ||
|
||
const ruleTester = new RuleTester(); | ||
ruleTester.run('require-download-url', rule, { | ||
valid: [ | ||
`// ==UserScript== | ||
// @downloadURL example.com | ||
// @updateURL example.com | ||
// ==/UserScript==`, | ||
`// ==UserScript== | ||
// @downloadURL example.com | ||
// ==/UserScript==` | ||
], | ||
invalid: [ | ||
{ | ||
code: `// ==UserScript== | ||
// @updateURL example.com | ||
// ==/UserScript==`, | ||
output: `// ==UserScript== | ||
// @updateURL example.com | ||
// @downloadURL example.com | ||
// ==/UserScript==`, | ||
errors: [ | ||
{ | ||
messageId: 'includeDownloadURL' | ||
} | ||
] | ||
} | ||
] | ||
}); |
This file was deleted.
Oops, something went wrong.