testScript.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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: {
  11. ch: lint.from.ch + 1,
  12. line: lint.from.line + 1,
  13. },
  14. to: {
  15. ch: lint.from.ch + 1,
  16. line: lint.to.line + 1,
  17. },
  18. severity: "error",
  19. message: `[semantic] ${lint.message}`,
  20. }))
  21. )
  22. // Syntax linting
  23. try {
  24. const res: any = esprima.parseScript(text, { tolerant: true })
  25. if (res.errors && res.errors.length > 0) {
  26. results = results.concat(
  27. res.errors.map((err: any) => {
  28. const fromPos: { line: number; ch: number } = {
  29. line: err.lineNumber,
  30. ch: err.column,
  31. }
  32. const toPos: { line: number; ch: number } = {
  33. line: err.lineNumber,
  34. ch: err.column,
  35. }
  36. return <LinterResult>{
  37. from: fromPos,
  38. to: toPos,
  39. message: `[syntax] ${err.description}`,
  40. severity: "error",
  41. }
  42. })
  43. )
  44. }
  45. } catch (e: any) {
  46. const fromPos: { line: number; ch: number } = {
  47. line: e.lineNumber,
  48. ch: e.column,
  49. }
  50. const toPos: { line: number; ch: number } = {
  51. line: e.lineNumber,
  52. ch: e.column,
  53. }
  54. results = results.concat([
  55. <LinterResult>{
  56. from: fromPos,
  57. to: toPos,
  58. message: `[syntax] ${e.description}`,
  59. severity: "error",
  60. },
  61. ])
  62. }
  63. return results
  64. }
  65. export default linter