detect-non-literal-regexp.js 913 B

12345678910111213141516171819202122232425262728
  1. /**
  2. * Tries to detect RegExp's created from non-literal strings.
  3. * @author Jon Lamendola
  4. */
  5. //------------------------------------------------------------------------------
  6. // Rule Definition
  7. //------------------------------------------------------------------------------
  8. module.exports = function(context) {
  9. "use strict";
  10. return {
  11. "NewExpression": function(node) {
  12. if (node.callee.name === 'RegExp') {
  13. var args = node.arguments;
  14. if (args && args.length > 0 && args[0].type !== 'Literal') {
  15. var token = context.getTokens(node)[0];
  16. return context.report(node, 'Found non-literal argument to RegExp Constructor');
  17. }
  18. }
  19. }
  20. }
  21. }