fix-space-around-keywords.js 944 B

1234567891011121314151617181920212223242526272829303132333435
  1. 'use strict';
  2. const {getParenthesizedRange} = require('../utils/parentheses.js');
  3. const isProblematicToken = ({type, value}) => (
  4. (type === 'Keyword' && /^[a-z]*$/.test(value))
  5. // ForOfStatement
  6. || (type === 'Identifier' && value === 'of')
  7. // AwaitExpression
  8. || (type === 'Identifier' && value === 'await')
  9. );
  10. function * fixSpaceAroundKeyword(fixer, node, sourceCode) {
  11. const range = getParenthesizedRange(node, sourceCode);
  12. const tokenBefore = sourceCode.getTokenBefore({range}, {includeComments: true});
  13. if (
  14. tokenBefore
  15. && range[0] === tokenBefore.range[1]
  16. && isProblematicToken(tokenBefore)
  17. ) {
  18. yield fixer.insertTextAfter(tokenBefore, ' ');
  19. }
  20. const tokenAfter = sourceCode.getTokenAfter({range}, {includeComments: true});
  21. if (
  22. tokenAfter
  23. && range[1] === tokenAfter.range[0]
  24. && isProblematicToken(tokenAfter)
  25. ) {
  26. yield fixer.insertTextBefore(tokenAfter, ' ');
  27. }
  28. }
  29. module.exports = fixSpaceAroundKeyword;