UNPKG

1.81 kBJavaScriptView Raw
1/**
2 * @fileoverview Prevent usage of dangerous JSX props
3 * @author Scott Andrews
4 */
5
6'use strict';
7
8const docsUrl = require('../util/docsUrl');
9const jsxUtil = require('../util/jsx');
10
11// ------------------------------------------------------------------------------
12// Constants
13// ------------------------------------------------------------------------------
14
15const DANGEROUS_MESSAGE = 'Dangerous property \'{{name}}\' found';
16
17const DANGEROUS_PROPERTY_NAMES = [
18 'dangerouslySetInnerHTML'
19];
20
21const 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 */
35function isDangerous(name) {
36 return name in DANGEROUS_PROPERTIES;
37}
38
39// ------------------------------------------------------------------------------
40// Rule Definition
41// ------------------------------------------------------------------------------
42
43module.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};