latex-linter.test.ts 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. import { assert } from 'chai'
  2. import LintWorker from '../../../../../../frontend/js/features/source-editor/languages/latex/linter/latex-linter.worker.js'
  3. import { errorsToDiagnostics } from '../../../../../../frontend/js/features/source-editor/languages/latex/linter/errors-to-diagnostics'
  4. import { Diagnostic } from '@codemirror/lint'
  5. import { mergeCompatibleOverlappingDiagnostics } from '../../../../../../frontend/js/features/source-editor/languages/latex/linter/merge-overlapping-diagnostics'
  6. const { Parse } = new LintWorker()
  7. describe('LatexLinter', function () {
  8. it('should accept a simple environment match without errors', function () {
  9. const { errors } = Parse('\\begin{foo}\n' + '\\end{foo}\n')
  10. assert.equal(errors.length, 0)
  11. })
  12. it('should accept an invalid \\it* command', function () {
  13. const { errors } = Parse('\\it*hello\n' + '\\bye\n')
  14. assert.equal(errors.length, 0)
  15. })
  16. it('should accept newcomlumntype', function () {
  17. const { errors } = Parse(
  18. 'hello\n' +
  19. '\\newcolumntype{M}[1]{>{\\begin{varwidth}[t]{#1}}l<{\\end{varwidth}}}\n' +
  20. 'bye'
  21. )
  22. assert.equal(errors.length, 0)
  23. })
  24. it('should accept newenvironment', function () {
  25. const { errors } = Parse(
  26. '\\newenvironment{Algorithm}[2][tbh]%\n' +
  27. '{\\begin{myalgo}[#1]\n' +
  28. '\\centering\n' +
  29. '\\part{title}\\begin{minipage}{#2}\n' +
  30. '\\begin{algorithm}[H]}%\n' +
  31. '{\\end{algorithm}\n' +
  32. '\\end{minipage}\n' +
  33. '\\end{myalgo}}'
  34. )
  35. assert.equal(errors.length, 0)
  36. })
  37. it('should accept newenvironment II', function () {
  38. const { errors } = Parse(
  39. '\\newenvironment{claimproof}[1][\\myproofname]{\\begin{proof}[#1]\\renewcommand*{\\qedsymbol}{\\(\\diamondsuit\\)}}{\\end{proof}}'
  40. )
  41. assert.equal(errors.length, 0)
  42. })
  43. it('should accept superscript inside math mode', function () {
  44. const { errors } = Parse('this is $a^b$ test')
  45. assert.equal(errors.length, 0)
  46. })
  47. it('should accept subscript inside math mode', function () {
  48. const { errors } = Parse('this is $a_b$ test')
  49. assert.equal(errors.length, 0)
  50. })
  51. it('should return an error for superscript outside math mode', function () {
  52. const { errors } = Parse('this is a^b test')
  53. assert.equal(errors.length, 1)
  54. assert.equal(errors[0].text, '^ must be inside math mode')
  55. assert.equal(errors[0].type, 'error')
  56. })
  57. it('should return an error subscript outside math mode', function () {
  58. const { errors } = Parse('this is a_b test')
  59. assert.equal(errors.length, 1)
  60. assert.equal(errors[0].text, '_ must be inside math mode')
  61. assert.equal(errors[0].type, 'error')
  62. })
  63. it('should accept math mode inside \\hbox outside math mode', function () {
  64. const { errors } = Parse('this is \\hbox{for every $bar$}')
  65. assert.equal(errors.length, 0)
  66. })
  67. it('should accept math mode inside \\hbox inside math mode', function () {
  68. const { errors } = Parse('this is $foo = \\hbox{for every $bar$}$ test')
  69. assert.equal(errors.length, 0)
  70. })
  71. it('should accept math mode inside \\text inside math mode', function () {
  72. const { errors } = Parse('this is $foo = \\text{for every $bar$}$ test')
  73. assert.equal(errors.length, 0)
  74. })
  75. it('should accept verbatim', function () {
  76. const { errors } = Parse(
  77. 'this is text\n' +
  78. '\\begin{verbatim}\n' +
  79. 'this is verbatim\n' +
  80. '\\end{verbatim}\n' +
  81. 'this is more text\n'
  82. )
  83. assert.equal(errors.length, 0)
  84. })
  85. it('should accept verbatim with environment inside', function () {
  86. const { errors } = Parse(
  87. 'this is text\n' +
  88. '\\begin{verbatim}\n' +
  89. 'this is verbatim\n' +
  90. '\\begin{foo}\n' +
  91. 'this is verbatim too\n' +
  92. '\\end{foo}\n' +
  93. '\\end{verbatim}\n' +
  94. 'this is more text\n'
  95. )
  96. assert.equal(errors.length, 0)
  97. })
  98. it('should accept verbatim with \\begin{verbatim} inside', function () {
  99. const { errors } = Parse(
  100. 'this is text\n' +
  101. '\\begin{verbatim}\n' +
  102. 'this is verbatim\n' +
  103. '\\begin{verbatim}\n' +
  104. 'this is verbatim too\n' +
  105. '\\end{verbatim}\n' +
  106. 'this is more text\n'
  107. )
  108. assert.equal(errors.length, 0)
  109. })
  110. it('should accept equation', function () {
  111. const { errors } = Parse(
  112. 'this is text\n' +
  113. '\\begin{equation}\n' +
  114. '\\alpha^2 + b^2 = c^2\n' +
  115. '\\end{equation}\n' +
  116. 'this is more text\n'
  117. )
  118. assert.equal(errors.length, 0)
  119. })
  120. it('should accept $$', function () {
  121. const { errors } = Parse(
  122. 'this is text\n' +
  123. '$$\n' +
  124. '\\alpha^2 + b^2 = c^2\n' +
  125. '$$\n' +
  126. 'this is more text\n'
  127. )
  128. assert.equal(errors.length, 0)
  129. })
  130. it('should accept $', function () {
  131. const { errors } = Parse(
  132. 'this is text $\\alpha^2 + b^2 = c^2$' + ' this is more text\n'
  133. )
  134. assert.equal(errors.length, 0)
  135. })
  136. it('should accept \\[', function () {
  137. const { errors } = Parse(
  138. 'this is text\n' +
  139. '\\[\n' +
  140. '\\alpha^2 + b^2 = c^2\n' +
  141. '\\]\n' +
  142. 'this is more text\n'
  143. )
  144. assert.equal(errors.length, 0)
  145. })
  146. it('should accept \\(', function () {
  147. const { errors } = Parse(
  148. 'this is text \\(\\alpha^2 + b^2 = c^2\\)' + ' this is more text\n'
  149. )
  150. assert.equal(errors.length, 0)
  151. })
  152. it('should accept \\begin{foo}', function () {
  153. const { errors } = Parse(
  154. 'this is text\n' +
  155. '\\begin{foo}\n' +
  156. 'this is foo\n' +
  157. '\\end{foo}\n' +
  158. 'this is more text\n'
  159. )
  160. assert.equal(errors.length, 0)
  161. })
  162. it('should accept \\begin{foo_bar}', function () {
  163. const { errors } = Parse(
  164. 'this is text\n' +
  165. '\\begin{foo_bar}\n' +
  166. 'this is foo bar\n' +
  167. '\\end{foo_bar}\n' +
  168. 'this is more text\n'
  169. )
  170. assert.equal(errors.length, 0)
  171. })
  172. it('should accept \\begin{foo} \\begin{bar}', function () {
  173. const { errors } = Parse(
  174. 'this is text\n' +
  175. '\\begin{foo}\n' +
  176. '\\begin{bar}\n' +
  177. '\\begin{baz}\n' +
  178. 'this is foo bar baz\n' +
  179. '\\end{baz}\n' +
  180. '\\end{bar}\n' +
  181. '\\end{foo}\n' +
  182. 'this is more text\n'
  183. )
  184. assert.equal(errors.length, 0)
  185. })
  186. it('should accept \\verb|...|', function () {
  187. const { errors } = Parse('this is text \\verb|hello| and more\n')
  188. assert.equal(errors.length, 0)
  189. })
  190. it('should accept \\verb|...| with special chars', function () {
  191. const { errors } = Parse('this is text \\verb|{}()^_@$xhello| and more\n')
  192. assert.equal(errors.length, 0)
  193. })
  194. it('should accept \\url|...|', function () {
  195. const { errors } = Parse(
  196. 'this is text \\url|http://www.overleaf.com/| and more\n'
  197. )
  198. assert.equal(errors.length, 0)
  199. })
  200. it('should accept \\url{...}', function () {
  201. const { errors } = Parse(
  202. 'this is text \\url{http://www.overleaf.com/} and more\n'
  203. )
  204. assert.equal(errors.length, 0)
  205. })
  206. it('should accept \\url{...} with % chars', function () {
  207. const { errors } = Parse(
  208. 'this is text \\url{http://www.overleaf.com/hello%20world} and more\n'
  209. )
  210. assert.equal(errors.length, 0)
  211. })
  212. it('should accept \\href{...}{...}', function () {
  213. const { errors } = Parse(
  214. 'this is text \\href{http://www.overleaf.com/}{test} and more\n'
  215. )
  216. assert.equal(errors.length, 0)
  217. })
  218. it('should accept \\href{...}{...} with dollarsign in url', function () {
  219. const { errors } = Parse(
  220. 'this is text \\href{http://www.overleaf.com/foo=$bar}{test} and more\n'
  221. )
  222. assert.equal(errors.length, 0)
  223. })
  224. it('should not accept \\href|...|{...}', function () {
  225. const { errors } = Parse(
  226. 'this is text \\href|http://www.overleaf.com|{test} and more\n'
  227. )
  228. assert.equal(errors.length, 1)
  229. assert.equal(errors[0].text, 'invalid href command')
  230. assert.equal(errors[0].type, 'error')
  231. })
  232. it('should catch error in text argument of \\href{...}{...}', function () {
  233. const { errors } = Parse(
  234. 'this is text \\href{http://www.overleaf.com/foo=$bar}{i have made an $error} and more\n'
  235. )
  236. assert.equal(errors.length, 2)
  237. assert.equal(errors[0].text, 'unclosed $ found at close group }')
  238. assert.equal(errors[0].type, 'error')
  239. assert.equal(errors[1].text, 'unexpected close group } after $')
  240. assert.equal(errors[1].type, 'error')
  241. })
  242. it('should accept \\left( and \\right)', function () {
  243. const { errors } = Parse('math $\\left( x + y \\right) = y + x$ and more\n')
  244. assert.equal(errors.length, 0)
  245. })
  246. it('should accept \\left( and \\right.', function () {
  247. const { errors } = Parse('math $\\left( x + y \\right. = y + x$ and more\n')
  248. assert.equal(errors.length, 0)
  249. })
  250. it('should accept \\left. and \\right)', function () {
  251. const { errors } = Parse('math $\\left. x + y \\right) = y + x$ and more\n')
  252. assert.equal(errors.length, 0)
  253. })
  254. it('should accept complex math nesting', function () {
  255. const { errors } = Parse(
  256. 'math $\\left( {x + {y + z} + x} \\right\\} = \\left[y + x\\right.$ and more\n'
  257. )
  258. assert.equal(errors.length, 0)
  259. })
  260. it('should accept math toggling $a$$b$', function () {
  261. const { errors } = Parse('math $a$$b$ and more\n')
  262. assert.equal(errors.length, 0)
  263. })
  264. it('should accept math toggling $$display$$$inline$', function () {
  265. const { errors } = Parse('math $$display$$$inline$ and more\n')
  266. assert.equal(errors.length, 0)
  267. })
  268. it('should accept math definition commands', function () {
  269. const { errors } = Parse(
  270. '\\let\\originalleft\\left\n' +
  271. '\\let\\originalright\\right\n' +
  272. '\\renewcommand{\\left}{\\mathopen{}\\mathclose\\bgroup\\originalleft}\n' +
  273. '\\renewcommand{\\right}{\\aftergroup\\egroup\\originalright}\n'
  274. )
  275. assert.equal(errors.length, 0)
  276. })
  277. it('should accept math reflectbox commands', function () {
  278. const { errors } = Parse('$\\reflectbox{$alpha$}$\n')
  279. assert.equal(errors.length, 0)
  280. })
  281. it('should accept math scalebox commands', function () {
  282. const { errors } = Parse('$\\scalebox{2}{$alpha$}$\n')
  283. assert.equal(errors.length, 0)
  284. })
  285. it('should accept math rotatebox commands', function () {
  286. const { errors } = Parse('$\\rotatebox{60}{$alpha$}$\n')
  287. assert.equal(errors.length, 0)
  288. })
  289. it('should accept math resizebox commands', function () {
  290. const { errors } = Parse('$\\resizebox{2}{3}{$alpha$}$\n')
  291. assert.equal(errors.length, 0)
  292. })
  293. it('should accept all math box commands', function () {
  294. const { errors } = Parse(
  295. '\\[ \\left(\n' +
  296. '\\shiftright{2ex}{\\raisebox{-2ex}{\\scalebox{2}{$\\ast$}}}\n' +
  297. '\\reflectbox{$ddots$}\n' +
  298. '\\right). \\]\n'
  299. )
  300. assert.equal(errors.length, 0)
  301. })
  302. it('should accept math tag commands', function () {
  303. const { errors } = Parse('$\\tag{$alpha$}$\n')
  304. assert.equal(errors.length, 0)
  305. })
  306. it('should accept math \\def commands', function () {
  307. const { errors } = Parse(
  308. '\\def\\peb[#1]{{\\left\\lfloor #1\\right\\rfloor}}'
  309. )
  310. assert.equal(errors.length, 0)
  311. })
  312. it('should accept math \\def commands II', function () {
  313. const { errors } = Parse('\\def\\foo#1{\\gamma^#1}')
  314. assert.equal(errors.length, 0)
  315. })
  316. it('should accept DeclareMathOperator', function () {
  317. const { errors } = Parse('\\DeclareMathOperator{\\var}{\\Delta^2\\!}')
  318. assert.equal(errors.length, 0)
  319. })
  320. it('should accept DeclarePairedDelimiter', function () {
  321. const { errors } = Parse(
  322. '\\DeclarePairedDelimiter{\\spro}{\\left(}{\\right)^{\\ast}}'
  323. )
  324. assert.equal(errors.length, 0)
  325. })
  326. it('should accept nested user-defined math commands', function () {
  327. const { errors } = Parse(
  328. '$\\foo{$\\alpha \\bar{x^y}{\\cite{hello}}$}{\\gamma}{$\\beta\\baz{\\alpha}$}{\\cite{foo}}$'
  329. )
  330. assert.equal(errors.length, 0)
  331. })
  332. it('should accept nested user-defined math commands II', function () {
  333. const { errors } = Parse(
  334. '\\foo{$\\alpha \\bar{x^y}{\\cite{hello}}$}{\\gamma}{$\\beta\\baz{\\alpha}$}{\\cite{foo}}'
  335. )
  336. assert.equal(errors.length, 0)
  337. })
  338. it('should accept newenvironment with multiple parameters', function () {
  339. const { errors } = Parse(
  340. '\\newenvironment{case}[1][\\textsc{Case}]\n' +
  341. '{\\begin{trivlist}\\item[\\hskip \\labelsep {\\textsc{#1}}]}{\\end{trivlist}}'
  342. )
  343. assert.equal(errors.length, 0)
  344. })
  345. it('should accept newenvironment with no parameters', function () {
  346. const { errors } = Parse(
  347. '\\newenvironment{case}{\\begin{trivlist}\\item[\\hskip \\labelsep {\\textsc{#1}}]}{\\end{trivlist}}'
  348. )
  349. assert.equal(errors.length, 0)
  350. })
  351. it('should accept tikzfeynman', function () {
  352. const { errors } = Parse(
  353. '\\begin{equation*}\n' +
  354. '\\feynmandiagram[layered layout, medium, horizontal=a to b] {\n' +
  355. ' a [particle=\\(H\\)] -- [scalar] b [dot] -- [photon] f1 [particle=\\(W^{\\pm}\\)],\n' +
  356. ' b -- [boson, edge label=\\(W^{\\mp}\\)] c [dot],\n' +
  357. ' c -- [fermion] f2 [particle=\\(f\\)],\n' +
  358. " c -- [anti fermion] f3 [particle=\\(\\bar{f}'\\)],\n" +
  359. ' };this is a change\n' +
  360. '\\end{equation*}'
  361. )
  362. assert.equal(errors.length, 0)
  363. })
  364. it('should return errors from malformed \\end', function () {
  365. const { errors } = Parse(
  366. 'this is text\n' +
  367. '\\begin{foo}\n' +
  368. '\\begin{bar}\n' +
  369. 'this is foo bar baz\n' +
  370. '\\end{bar\n' +
  371. '\\end{foo}\n' +
  372. 'this is more text\n'
  373. )
  374. assert.equal(errors.length, 4)
  375. assert.equal(errors[0].text, 'unclosed \\begin{bar} found at \\end{foo}')
  376. assert.equal(errors[1].text, 'invalid environment command \\end{bar')
  377. assert.equal(errors[2].text, 'unclosed open group { found at \\end{foo}')
  378. assert.equal(errors[3].text, 'unexpected \\end{foo} after \\begin{bar}')
  379. })
  380. it('should accept \\newcommand*', function () {
  381. const { errors } = Parse('\\newcommand*{\\foo}{\\bar}')
  382. assert.equal(errors.length, 0)
  383. })
  384. it('should accept incomplete \\newcommand*', function () {
  385. const { errors } = Parse('\\newcommand*{\\beq' + '}')
  386. assert.equal(errors.length, 0)
  387. })
  388. it('should accept a plain hyperref command', function () {
  389. const { errors } = Parse('\\hyperref{http://www.overleaf.com/}')
  390. assert.equal(errors.length, 0)
  391. })
  392. it('should accept a hyperref command with underscores in the url ', function () {
  393. const { errors } = Parse('\\hyperref{http://www.overleaf.com/my_page.html}')
  394. assert.equal(errors.length, 0)
  395. })
  396. it('should accept a hyperref command with category, name and text arguments ', function () {
  397. const { errors } = Parse(
  398. '\\hyperref{http://www.overleaf.com/}{category}{name}{text}'
  399. )
  400. assert.equal(errors.length, 0)
  401. })
  402. it('should accept an underscore in a hyperref label', function () {
  403. const { errors } = Parse('\\hyperref[foo_bar]{foo bar}')
  404. assert.equal(errors.length, 0)
  405. })
  406. it('should reject a $ in a hyperref label', function () {
  407. const { errors } = Parse('\\hyperref[foo$bar]{foo bar}')
  408. assert.equal(errors.length, 1)
  409. })
  410. it('should reject an unclosed hyperref label', function () {
  411. const { errors } = Parse('\\hyperref[foo_bar{foo bar}')
  412. assert.equal(errors.length, 2)
  413. assert.equal(errors[0].text, 'invalid hyperref label')
  414. assert.equal(errors[1].text, 'unexpected close group }')
  415. })
  416. it('should accept a hyperref command without an optional argument', function () {
  417. const { errors } = Parse('{\\hyperref{hello}}')
  418. assert.equal(errors.length, 0)
  419. })
  420. it('should accept a hyperref command without an optional argument and multiple other arguments', function () {
  421. const { errors } = Parse('{\\hyperref{}{}{fig411}}')
  422. assert.equal(errors.length, 0)
  423. })
  424. it('should accept a hyperref command without an optional argument in an unclosed group', function () {
  425. const { errors } = Parse('{\\hyperref{}{}{fig411}')
  426. assert.equal(errors.length, 1)
  427. assert.equal(errors[0].text, 'unclosed group {')
  428. })
  429. it('should accept documentclass with no options', function () {
  430. const { errors } = Parse('\\documentclass{article}')
  431. assert.equal(errors.length, 0)
  432. })
  433. it('should accept documentclass with options', function () {
  434. const { errors } = Parse('\\documentclass[a4paper]{article}')
  435. assert.equal(errors.length, 0)
  436. })
  437. it('should accept documentclass with underscore in options', function () {
  438. const { errors } = Parse(
  439. '\\documentclass[my_custom_document_class_option]{my-custom-class}'
  440. )
  441. assert.equal(errors.length, 0)
  442. })
  443. // %novalidate
  444. // %begin novalidate
  445. // %end novalidate
  446. // \begin{foo}
  447. // \begin{new_theorem}
  448. // \begin{foo invalid environment command
  449. // \newcommand{\foo}{\bar}
  450. // \newcommand[1]{\foo}{\bar #1}
  451. // \renewcommand...
  452. // \def
  453. // \DeclareRobustCommand
  454. // \newcolumntype
  455. // \newenvironment
  456. // \renewenvironment
  457. // \verb|....|
  458. // \url|...|
  459. // \url{...}
  460. // \left( \right)
  461. // \left. \right.
  462. // $...$
  463. // $$....$$
  464. // $...$$...$
  465. // $a^b$ vs a^b
  466. // $$a^b$$ vs a^b
  467. // Matrix for envs for {} left/right \[ \] \( \) $ $$ begin end
  468. // begin equation
  469. // align(*)
  470. // equation(*)
  471. // ]
  472. // array(*)
  473. // eqnarray(*)
  474. // split
  475. // aligned
  476. // cases
  477. // pmatrix
  478. // gathered
  479. // matrix
  480. // alignedat
  481. // smallmatrix
  482. // subarray
  483. // vmatrix
  484. // shortintertext
  485. it('should return math mode contexts', function () {
  486. const { contexts } = Parse(
  487. '\\begin{document}\n' +
  488. '$$\n' +
  489. '\\begin{array}\n' +
  490. '\\left( \\foo{bar} \\right] & 2\n' +
  491. '\\end{array}\n' +
  492. '$$\n' +
  493. '\\end{document}'
  494. )
  495. assert.equal(contexts.length, 1)
  496. assert.equal(contexts[0].type, 'math')
  497. assert.equal(contexts[0].range.start.row, 1)
  498. assert.equal(contexts[0].range.start.column, 0)
  499. assert.equal(contexts[0].range.end.row, 5)
  500. assert.equal(contexts[0].range.end.column, 2)
  501. })
  502. it('should remove error when cursor is inside incomplete command', function () {
  503. const { errors } = Parse('\\begin{}')
  504. const diagnostics = errorsToDiagnostics(errors, 7, 9)
  505. assert.equal(errors.length, 1)
  506. assert.equal(diagnostics.length, 0)
  507. })
  508. it('should show an error when cursor is outside incomplete command', function () {
  509. const { errors } = Parse('\\begin{}')
  510. const diagnostics = errorsToDiagnostics(errors, 6, 9)
  511. assert.equal(errors.length, 1)
  512. assert.equal(diagnostics.length, 1)
  513. assert.equal(diagnostics[0].from, 0)
  514. assert.equal(diagnostics[0].to, 6)
  515. })
  516. it('should adjust an error range when the cursor is inside that range', function () {
  517. const { errors } = Parse('\\begin{}')
  518. const diagnostics = errorsToDiagnostics(errors, 4, 7)
  519. assert.equal(errors.length, 1)
  520. assert.equal(errors[0].startPos, 0)
  521. assert.equal(errors[0].endPos, 7)
  522. assert.equal(diagnostics.length, 1)
  523. assert.equal(diagnostics[0].from, 0)
  524. assert.equal(diagnostics[0].to, 4)
  525. })
  526. it('should reject an error when part of the error range is outside of the document boundaries', function () {
  527. const { errors } = Parse('\\begin{}')
  528. const diagnostics = errorsToDiagnostics(errors, 8, 6)
  529. assert.equal(errors.length, 1)
  530. assert.equal(diagnostics.length, 0)
  531. })
  532. it('should merge two overlapping identical diagnostics', function () {
  533. const diagnostics: Diagnostic[] = [
  534. {
  535. from: 0,
  536. to: 2,
  537. message: 'Message 1',
  538. severity: 'error',
  539. },
  540. {
  541. from: 1,
  542. to: 3,
  543. message: 'Message 1',
  544. severity: 'error',
  545. },
  546. ]
  547. const mergedDiagnostics = mergeCompatibleOverlappingDiagnostics(diagnostics)
  548. assert.deepEqual(mergedDiagnostics, [
  549. {
  550. from: 0,
  551. to: 3,
  552. message: 'Message 1',
  553. severity: 'error',
  554. },
  555. ])
  556. })
  557. it('should merge two touching identical diagnostics', function () {
  558. const diagnostics: Diagnostic[] = [
  559. {
  560. from: 0,
  561. to: 2,
  562. message: 'Message 1',
  563. severity: 'error',
  564. },
  565. {
  566. from: 2,
  567. to: 3,
  568. message: 'Message 1',
  569. severity: 'error',
  570. },
  571. ]
  572. const mergedDiagnostics = mergeCompatibleOverlappingDiagnostics(diagnostics)
  573. assert.deepEqual(mergedDiagnostics, [
  574. {
  575. from: 0,
  576. to: 3,
  577. message: 'Message 1',
  578. severity: 'error',
  579. },
  580. ])
  581. })
  582. it('should not merge two overlapping diagnostics with different messages', function () {
  583. const diagnostics: Diagnostic[] = [
  584. {
  585. from: 0,
  586. to: 2,
  587. message: 'Message 1',
  588. severity: 'error',
  589. },
  590. {
  591. from: 1,
  592. to: 3,
  593. message: 'Message 2',
  594. severity: 'error',
  595. },
  596. ]
  597. const mergedDiagnostics = mergeCompatibleOverlappingDiagnostics(diagnostics)
  598. assert.deepEqual(diagnostics, mergedDiagnostics)
  599. })
  600. it('should not merge two overlapping diagnostics with different severities', function () {
  601. const diagnostics: Diagnostic[] = [
  602. {
  603. from: 0,
  604. to: 2,
  605. message: 'Message 1',
  606. severity: 'error',
  607. },
  608. {
  609. from: 1,
  610. to: 3,
  611. message: 'Message 1',
  612. severity: 'warning',
  613. },
  614. ]
  615. const mergedDiagnostics = mergeCompatibleOverlappingDiagnostics(diagnostics)
  616. assert.deepEqual(diagnostics, mergedDiagnostics)
  617. })
  618. it('should merge three overlapping identical diagnostics', function () {
  619. const diagnostics: Diagnostic[] = [
  620. {
  621. from: 0,
  622. to: 2,
  623. message: 'Message 1',
  624. severity: 'error',
  625. },
  626. {
  627. from: 1,
  628. to: 4,
  629. message: 'Message 1',
  630. severity: 'error',
  631. },
  632. {
  633. from: 3,
  634. to: 5,
  635. message: 'Message 1',
  636. severity: 'error',
  637. },
  638. ]
  639. const mergedDiagnostics = mergeCompatibleOverlappingDiagnostics(diagnostics)
  640. assert.deepEqual(mergedDiagnostics, [
  641. {
  642. from: 0,
  643. to: 5,
  644. message: 'Message 1',
  645. severity: 'error',
  646. },
  647. ])
  648. })
  649. it('should merge two separate sets of overlapping identical diagnostics', function () {
  650. const diagnostics: Diagnostic[] = [
  651. {
  652. from: 0,
  653. to: 2,
  654. message: 'Message 1',
  655. severity: 'error',
  656. },
  657. {
  658. from: 2,
  659. to: 3,
  660. message: 'Message 1',
  661. severity: 'error',
  662. },
  663. {
  664. from: 2,
  665. to: 5,
  666. message: 'Message 2',
  667. severity: 'error',
  668. },
  669. {
  670. from: 4,
  671. to: 6,
  672. message: 'Message 3',
  673. severity: 'error',
  674. },
  675. {
  676. from: 5,
  677. to: 7,
  678. message: 'Message 3',
  679. severity: 'error',
  680. },
  681. ]
  682. const mergedDiagnostics = mergeCompatibleOverlappingDiagnostics(diagnostics)
  683. assert.deepEqual(mergedDiagnostics, [
  684. {
  685. from: 0,
  686. to: 3,
  687. message: 'Message 1',
  688. severity: 'error',
  689. },
  690. {
  691. from: 2,
  692. to: 5,
  693. message: 'Message 2',
  694. severity: 'error',
  695. },
  696. {
  697. from: 4,
  698. to: 7,
  699. message: 'Message 3',
  700. severity: 'error',
  701. },
  702. ])
  703. })
  704. })