You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

code-difference.ts 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import { diffLines } from 'diff';
  21. import { groupBy, keyBy } from 'lodash';
  22. import { sanitizeString } from './sanitize';
  23. const NUMBER_OF_EXAMPLES = 2;
  24. type DiffBlock = { noncompliant: Element; compliant: Element };
  25. export default function applyCodeDifferences(element: Element | null) {
  26. if (element === null) {
  27. return;
  28. }
  29. const codeExamples = getExamplesFromDom(element);
  30. codeExamples.forEach(({ noncompliant, compliant }) => {
  31. if (noncompliant === undefined || compliant === undefined) {
  32. return;
  33. }
  34. const [markedNonCompliant, markedCompliantCode] = differentiateCode(
  35. noncompliant.innerHTML,
  36. compliant.innerHTML,
  37. );
  38. replaceInDom(noncompliant, markedNonCompliant);
  39. replaceInDom(compliant, markedCompliantCode);
  40. });
  41. }
  42. function getExamplesFromDom(element: Element) {
  43. const pres = Array.from(element.querySelectorAll(`pre[data-diff-id]`));
  44. return (
  45. Object.values(
  46. groupBy(
  47. pres.filter((e) => e.getAttribute('data-diff-id') !== undefined),
  48. (e) => e.getAttribute('data-diff-id'),
  49. ),
  50. )
  51. // If we have 1 or 3+ example we can't display any differences
  52. .filter((diffsBlock) => diffsBlock.length === NUMBER_OF_EXAMPLES)
  53. .map(
  54. (diffBlock) =>
  55. keyBy(diffBlock, (block) => block.getAttribute('data-diff-type')) as DiffBlock,
  56. )
  57. );
  58. }
  59. function differentiateCode(compliant: string, nonCompliant: string) {
  60. const hunks = diffLines(compliant, nonCompliant);
  61. let nonCompliantCode = '';
  62. let compliantCode = '';
  63. hunks.forEach((hunk) => {
  64. const { value } = hunk;
  65. if (!hunk.added && !hunk.removed) {
  66. nonCompliantCode += value;
  67. compliantCode += value;
  68. }
  69. if (hunk.added) {
  70. compliantCode += `<div class='code-added'>${value}</div>`;
  71. }
  72. if (hunk.removed) {
  73. nonCompliantCode += `<div class='code-removed'>${value}</div>`;
  74. }
  75. });
  76. return [sanitizeString(nonCompliantCode), sanitizeString(compliantCode)];
  77. }
  78. function replaceInDom(current: Element, code: string) {
  79. const markedCode = document.createElement('pre');
  80. markedCode.classList.add('code-difference-scrollable');
  81. const div = document.createElement('div');
  82. div.classList.add('code-difference-container');
  83. div.innerHTML = code;
  84. markedCode.appendChild(div);
  85. current.parentNode?.replaceChild(markedCode, current);
  86. }