ast-utils.js 897 B

123456789101112131415161718192021222324252627282930
  1. /**
  2. * @fileoverview Common utils for AST.
  3. *
  4. * This file contains only shared items for core and rules.
  5. * If you make a utility for rules, please see `../rules/utils/ast-utils.js`.
  6. *
  7. * @author Toru Nagashima <https://github.com/mysticatea>
  8. */
  9. "use strict";
  10. const breakableTypePattern =
  11. /^(?:(?:Do)?While|For(?:In|Of)?|Switch)Statement$/u;
  12. const lineBreakPattern = /\r\n|[\r\n\u2028\u2029]/u;
  13. const shebangPattern = /^#!([^\r\n]+)/u;
  14. /**
  15. * Creates a version of the `lineBreakPattern` regex with the global flag.
  16. * Global regexes are mutable, so this needs to be a function instead of a constant.
  17. * @returns {RegExp} A global regular expression that matches line terminators
  18. */
  19. function createGlobalLinebreakMatcher() {
  20. return new RegExp(lineBreakPattern.source, "gu");
  21. }
  22. module.exports = {
  23. breakableTypePattern,
  24. lineBreakPattern,
  25. createGlobalLinebreakMatcher,
  26. shebangPattern,
  27. };