forked from microsoft/eslint-plugin-sdl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathno-html-method.js
56 lines (52 loc) · 1.74 KB
/
no-html-method.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
/**
* @fileoverview Rule to disallow call to html() method
* @author Antonios Katopodis
*/
"use strict";
const astUtils = require("../ast-utils");
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
type: "suggestion",
fixable: "code",
schema: [],
docs:{
description: "Direct calls to method `html()` often (e.g. in jQuery framework) manipulate DOM without any sanitization and should be avoided. Use document.createElement() or similar methods instead.",
url: "https://github.com/microsoft/eslint-plugin-sdl/blob/master/docs/rules/no-html-method.md"
},
messages: {
default: 'Do not write to DOM directly using jQuery html() method'
}
},
create: function(context) {
const fullTypeChecker = astUtils.getFullTypeChecker(context);
return {
// TODO:
// - Cover similar methods that can manipulate DOM such as append(string), jQuery(string)
// - Improve rule with type information from TypeScript parser
// - Consider ignoring all Literals?
"CallExpression[arguments.length=1] > MemberExpression.callee[property.name='html']"(node) {
// Known false positives
if (
// element.html("")
node.parent.arguments[0].type === "Literal"
&& (
node.parent.arguments[0].value === ""
|| node.parent.arguments[0].value === null
)
) {
return;
}
context.report(
{
node: node,
messageId: "default"
});
}
};
}
};