Skip to content

Commit c31d632

Browse files
committed
Change rule and docs
1 parent ca162fd commit c31d632

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

docs/rules/jsx-no-script-url.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Examples of **correct** code for this rule:
2323
<a href={"javascript:"}></a>
2424
```
2525

26+
This rule takes the `linkComponents` setting into account.
27+
2628
## Rule Options
2729

2830
```json
@@ -44,6 +46,10 @@ Examples of **correct** code for this rule:
4446
```
4547

4648
Allows you to indicate a specific list of properties used by a custom component to be checked.
49+
This will override anything passed to `linkComponents` setting.
50+
51+
NOTE: This rule now takes into account the `linkComponents` setting, which should be used as the sole source of truth for link components.
52+
The rule still allows passing link components as rule option for backwards compatibility, but this option is deprecated.
4753

4854
### name
4955

lib/rules/jsx-no-script-url.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
'use strict';
77

88
const docsUrl = require('../util/docsUrl');
9+
const linkComponentsUtil = require('../util/linkComponents');
910
const report = require('../util/report');
1011

1112
// ------------------------------------------------------------------------------
@@ -21,26 +22,21 @@ function hasJavaScriptProtocol(attr) {
2122
&& isJavaScriptProtocol.test(attr.value.value);
2223
}
2324

24-
function shouldVerifyElement(node, config) {
25-
const name = node.name && node.name.name;
26-
return name === 'a' || config.find((i) => i.name === name);
27-
}
28-
2925
function shouldVerifyProp(node, config) {
3026
const name = node.name && node.name.name;
3127
const parentName = node.parent.name && node.parent.name.name;
3228

33-
if (parentName === 'a' && name === 'href') {
34-
return true;
35-
}
29+
if (!name || !parentName || !config.has(parentName)) return false;
3630

37-
const el = config.find((i) => i.name === parentName);
38-
if (!el) {
39-
return false;
40-
}
31+
return name === config.get(parentName);
32+
}
4133

42-
const props = el.props || [];
43-
return node.name && props.indexOf(name) !== -1;
34+
function parseLegacyOption(config, option) {
35+
option.forEach((opt) => {
36+
opt.props.forEach((prop) => {
37+
config.set(opt.name, prop);
38+
});
39+
});
4440
}
4541

4642
const messages = {
@@ -82,11 +78,14 @@ module.exports = {
8278
},
8379

8480
create(context) {
85-
const config = context.options[0] || [];
81+
const linkComponents = linkComponentsUtil.getLinkComponents(context);
82+
if (context.options[0]) {
83+
parseLegacyOption(linkComponents, context.options[0]);
84+
}
85+
8686
return {
8787
JSXAttribute(node) {
88-
const parent = node.parent;
89-
if (shouldVerifyElement(parent, config) && shouldVerifyProp(node, config) && hasJavaScriptProtocol(node)) {
88+
if (shouldVerifyProp(node, linkComponents) && hasJavaScriptProtocol(node)) {
9089
report(context, messages.noScriptURL, 'noScriptURL', {
9190
node,
9291
});

0 commit comments

Comments
 (0)