Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import prettier from 'prettier/standalone'
  2. import parserTypeScript from 'prettier/parser-typescript'
  3. import { CodeError } from 'types'
  4. /**
  5. * Format code with prettier
  6. * @param code
  7. */
  8. export function getFormattedCode(code: string): string {
  9. return prettier.format(code, {
  10. parser: 'typescript',
  11. plugins: [parserTypeScript],
  12. singleQuote: true,
  13. trailingComma: 'es5',
  14. semi: false,
  15. })
  16. }
  17. /**
  18. * Get line and column from error.
  19. * @param e
  20. */
  21. export const getErrorWithLineAndColumn = (e: Error | any): CodeError => {
  22. if ('line' in e) {
  23. return { message: e.message, line: Number(e.line), column: e.column }
  24. }
  25. const result = e.stack.split('/n')[0].match(/(.*)\(([0-9]+):([0-9]+)/)
  26. if (result) {
  27. return {
  28. message: result[1],
  29. line: Number(result[2]) + 1,
  30. column: result[3],
  31. }
  32. } else {
  33. return {
  34. message: e.message,
  35. line: null,
  36. column: null,
  37. }
  38. }
  39. }