testScript.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import * as esprima from "esprima"
  2. import { LinterDefinition, LinterResult } from "./linter"
  3. import { performTestLinting } from "~/helpers/tern"
  4. const linter: LinterDefinition = async (text) => {
  5. let results: LinterResult[] = []
  6. // Semantic linting
  7. const semanticLints = await performTestLinting(text)
  8. results = results.concat(
  9. semanticLints.map((lint: any) => ({
  10. from: lint.from,
  11. to: lint.to,
  12. severity: "error",
  13. message: `[semantic] ${lint.message}`,
  14. }))
  15. )
  16. // Syntax linting
  17. try {
  18. const res: any = esprima.parseScript(text, { tolerant: true })
  19. if (res.errors && res.errors.length > 0) {
  20. results = results.concat(
  21. res.errors.map((err: any) => {
  22. const fromPos: { line: number; ch: number } = {
  23. line: err.lineNumber - 1,
  24. ch: err.column - 1,
  25. }
  26. const toPos: { line: number; ch: number } = {
  27. line: err.lineNumber - 1,
  28. ch: err.column,
  29. }
  30. return <LinterResult>{
  31. from: fromPos,
  32. to: toPos,
  33. message: `[syntax] ${err.description}`,
  34. severity: "error",
  35. }
  36. })
  37. )
  38. }
  39. } catch (e) {
  40. const fromPos: { line: number; ch: number } = {
  41. line: e.lineNumber - 1,
  42. ch: e.column - 1,
  43. }
  44. const toPos: { line: number; ch: number } = {
  45. line: e.lineNumber - 1,
  46. ch: e.column,
  47. }
  48. results = results.concat([
  49. <LinterResult>{
  50. from: fromPos,
  51. to: toPos,
  52. message: `[syntax] ${e.description}`,
  53. severity: "error",
  54. },
  55. ])
  56. }
  57. return results
  58. }
  59. export default linter