append-argument.js 668 B

1234567891011121314151617181920
  1. 'use strict';
  2. const {isCommaToken} = require('eslint-utils');
  3. function appendArgument(fixer, node, text, sourceCode) {
  4. // This function should also work for `NewExpression`
  5. // But parentheses of `NewExpression` could be omitted, add this check to prevent accident use on it
  6. /* istanbul ignore next */
  7. if (node.type !== 'CallExpression') {
  8. throw new Error(`Unexpected node "${node.type}".`);
  9. }
  10. const [penultimateToken, lastToken] = sourceCode.getLastTokens(node, 2);
  11. if (node.arguments.length > 0) {
  12. text = isCommaToken(penultimateToken) ? ` ${text},` : `, ${text}`;
  13. }
  14. return fixer.insertTextBefore(lastToken, text);
  15. }
  16. module.exports = appendArgument;