printValue.js 628 B

123456789101112131415161718
  1. import strip from 'strip-indent';
  2. function deindent(code) {
  3. const firstNewLine = code.indexOf('\n');
  4. return (code.slice(0, firstNewLine + 1) +
  5. // remove indentation from all lines except first.
  6. strip(code.slice(firstNewLine + 1)));
  7. }
  8. /**
  9. * Prints the given path without leading or trailing comments.
  10. */
  11. export default function printValue(path) {
  12. let source = path.getSource();
  13. // variable declarations and interface/type/class members might end with one of these
  14. if (source.endsWith(',') || source.endsWith(';')) {
  15. source = source.slice(0, -1);
  16. }
  17. return deindent(source);
  18. }