1 | /**
|
2 | * @fileoverview Prevent usage of dangerous JSX props
|
3 | * @author Scott Andrews
|
4 | */
|
5 |
|
6 | ;
|
7 |
|
8 | const docsUrl = require('../util/docsUrl');
|
9 | const jsxUtil = require('../util/jsx');
|
10 |
|
11 | // ------------------------------------------------------------------------------
|
12 | // Constants
|
13 | // ------------------------------------------------------------------------------
|
14 |
|
15 | const DANGEROUS_MESSAGE = 'Dangerous property \'{{name}}\' found';
|
16 |
|
17 | const DANGEROUS_PROPERTY_NAMES = [
|
18 | 'dangerouslySetInnerHTML'
|
19 | ];
|
20 |
|
21 | const DANGEROUS_PROPERTIES = DANGEROUS_PROPERTY_NAMES.reduce((props, prop) => {
|
22 | props[prop] = prop;
|
23 | return props;
|
24 | }, Object.create(null));
|
25 |
|
26 | // ------------------------------------------------------------------------------
|
27 | // Helpers
|
28 | // ------------------------------------------------------------------------------
|
29 |
|
30 | /**
|
31 | * Checks if a JSX attribute is dangerous.
|
32 | * @param {String} name - Name of the attribute to check.
|
33 | * @returns {boolean} Whether or not the attribute is dnagerous.
|
34 | */
|
35 | function isDangerous(name) {
|
36 | return name in DANGEROUS_PROPERTIES;
|
37 | }
|
38 |
|
39 | // ------------------------------------------------------------------------------
|
40 | // Rule Definition
|
41 | // ------------------------------------------------------------------------------
|
42 |
|
43 | module.exports = {
|
44 | meta: {
|
45 | docs: {
|
46 | description: 'Prevent usage of dangerous JSX props',
|
47 | category: 'Best Practices',
|
48 | recommended: false,
|
49 | url: docsUrl('no-danger')
|
50 | },
|
51 | schema: []
|
52 | },
|
53 |
|
54 | create(context) {
|
55 | return {
|
56 |
|
57 | JSXAttribute(node) {
|
58 | if (jsxUtil.isDOMComponent(node.parent) && isDangerous(node.name.name)) {
|
59 | context.report({
|
60 | node,
|
61 | message: DANGEROUS_MESSAGE,
|
62 | data: {
|
63 | name: node.name.name
|
64 | }
|
65 | });
|
66 | }
|
67 | }
|
68 |
|
69 | };
|
70 | }
|
71 | };
|