Skip to content

Commit

Permalink
fix: logic for use-download-and-update-url -> require-download-url (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yash-Singh1 authored Jul 6, 2023
1 parent 723ab65 commit 8c5dc10
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 106 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Add `userscripts` to the plugins section of your `.eslintrc` configuration file:
| [`require-description`](docs/rules/require-description.md) | Ensures userscripts have a description ||
| [`require-version`](docs/rules/require-version.md) | Ensures userscripts have a valid version ||
| [`use-homepage-and-url`](docs/rules/use-homepage-and-url.md) | Ensures that for each `homepage` attribute, `homepageURL` is also used ||
| [`use-download-and-update-url`](docs/rules/use-download-and-update-url.md) | Ensures that for each `downloadURL` there is a `updateURL` ||
| [`require-download-url`](docs/rules/require-download-url.md) | Ensures that for each `downloadURL` there is a `updateURL` ||
| [`align-attributes`](docs/rules/align-attributes.md) | Ensures that attributes are spaced out and aligned ||
| [`require-attribute-space-prefix`](docs/rules/require-attribute-space-prefix.md) | Ensure that attributes are prefixed by one space ||
| [`metadata-spacing`](docs/rules/metadata-spacing.md) | Ensures there is a newline between the metadata and the code ||
Expand Down
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Add `userscripts` to the plugins section of your `.eslintrc` configuration file:
| [`require-description`](docs/rules/require-description.md) | Ensures userscripts have a description ||
| [`require-version`](docs/rules/require-version.md) | Ensures userscripts have a valid version ||
| [`use-homepage-and-url`](docs/rules/use-homepage-and-url.md) | Ensures that for each `homepage` attribute, `homepageURL` is also used ||
| [`use-download-and-update-url`](docs/rules/use-download-and-update-url.md) | Ensures that for each `downloadURL` there is a `updateURL` ||
| [`require-download-url`](docs/rules/require-download-url.md) | Ensures that for each `downloadURL` there is a `updateURL` ||
| [`align-attributes`](docs/rules/align-attributes.md) | Ensures that attributes are spaced out and aligned ||
| [`require-attribute-space-prefix`](docs/rules/require-attribute-space-prefix.md) | Ensure that attributes are prefixed by one space ||
| [`metadata-spacing`](docs/rules/metadata-spacing.md) | Ensures there is a newline between the metadata and the code ||
Expand Down
2 changes: 1 addition & 1 deletion docs/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
- [`require-version`](rules/require-version.md)
- [`require-attribute-space-prefix`](rules/require-attribute-space-prefix.md)
- [`use-homepage-and-url`](rules/use-homepage-and-url.md)
- [`use-download-and-update-url`](rules/use-download-and-update-url.md)
- [`require-download-url`](rules/require-download-url.md)
- [`align-attributes`](rules/align-attributes.md)
- [`metadata-spacing`](rules/metadata-spacing.md)
- [`no-invalid-headers`](rules/no-invalid-headers.md)
Expand Down
58 changes: 58 additions & 0 deletions docs/rules/require-download-url.md
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.
44 changes: 0 additions & 44 deletions docs/rules/use-download-and-update-url.md

This file was deleted.

2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module.exports.configs = {
'userscripts/require-version': ['error', 'required'],
'userscripts/require-attribute-space-prefix': 'error',
'userscripts/use-homepage-and-url': 'error',
'userscripts/use-download-and-update-url': 'error',
'userscripts/require-download-url': 'error',
'userscripts/align-attributes': ['error', 2],
'userscripts/metadata-spacing': ['error', 'always'],
'userscripts/no-invalid-headers': 'error',
Expand Down
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
});
4 changes: 1 addition & 3 deletions lib/utils/createValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ const parse = require('./parse');
* attrVal: Metadata | Metadata[];
* index: number | number[];
* indexMatch: number | number[];
* metadata:
* | Object<string, Metadata | Metadata[]>
* | Object<string, Metadata | Metadata[]>[];
* metadata: Object<string, Metadata | Metadata[]>;
* context: RuleContext;
* keyName: string | string[];
* }} validationInfo
Expand Down
31 changes: 31 additions & 0 deletions tests/lib/rules/require-download-url.js
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'
}
]
}
]
});
44 changes: 0 additions & 44 deletions tests/lib/rules/use-download-and-update-url.js

This file was deleted.

0 comments on commit 8c5dc10

Please sign in to comment.