ast-utils.js 910 B

1234567891011121314151617181920212223242526272829
  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 = /^(?:(?:Do)?While|For(?:In|Of)?|Switch)Statement$/u;
  11. const lineBreakPattern = /\r\n|[\r\n\u2028\u2029]/u;
  12. const shebangPattern = /^#!([^\r\n]+)/u;
  13. /**
  14. * Creates a version of the `lineBreakPattern` regex with the global flag.
  15. * Global regexes are mutable, so this needs to be a function instead of a constant.
  16. * @returns {RegExp} A global regular expression that matches line terminators
  17. */
  18. function createGlobalLinebreakMatcher() {
  19. return new RegExp(lineBreakPattern.source, "gu");
  20. }
  21. module.exports = {
  22. breakableTypePattern,
  23. lineBreakPattern,
  24. createGlobalLinebreakMatcher,
  25. shebangPattern
  26. };