no-html-method.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright (c) Microsoft Corporation.
  2. // Licensed under the MIT License.
  3. /**
  4. * @fileoverview Rule to disallow call to html() method
  5. * @author Antonios Katopodis
  6. */
  7. "use strict";
  8. const astUtils = require("../ast-utils");
  9. //------------------------------------------------------------------------------
  10. // Rule Definition
  11. //------------------------------------------------------------------------------
  12. module.exports = {
  13. meta: {
  14. type: "suggestion",
  15. fixable: "code",
  16. schema: [],
  17. docs:{
  18. 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.",
  19. url: "https://github.com/microsoft/eslint-plugin-sdl/blob/master/docs/rules/no-html-method.md"
  20. },
  21. messages: {
  22. default: 'Do not write to DOM directly using jQuery html() method'
  23. }
  24. },
  25. create: function(context) {
  26. const fullTypeChecker = astUtils.getFullTypeChecker(context);
  27. return {
  28. // TODO:
  29. // - Cover similar methods that can manipulate DOM such as append(string), jQuery(string)
  30. // - Improve rule with type information from TypeScript parser
  31. // - Consider ignoring all Literals?
  32. "CallExpression[arguments.length=1] > MemberExpression.callee[property.name='html']"(node) {
  33. // Known false positives
  34. if (
  35. // element.html("")
  36. node.parent.arguments[0].type === "Literal"
  37. && (
  38. node.parent.arguments[0].value === ""
  39. || node.parent.arguments[0].value === null
  40. )
  41. ) {
  42. return;
  43. }
  44. context.report(
  45. {
  46. node: node,
  47. messageId: "default"
  48. });
  49. }
  50. };
  51. }
  52. };