All files / app/assets/javascripts/diffs/store getters.js

95.53% Statements 107/112
87.75% Branches 43/49
96.42% Functions 54/56
96.47% Lines 82/85

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228                        21x   21x   21x 79x 79x   79x             21x             21x 3x   3x 4x                   21x 3x   3x     2x                   21x 5x 3x 2x 5x   5x             21x 5x 7x 7x 4x 15x 7x   3x 3x   1x                 21x 3x   2x                 21x 4x 1x     21x 11x     3x 3x       21x 92x   21x 8x 19x   19x 13x             25x   19x     21x 3x               21x 4x 4x 4x 2x   2x 1x   1x 1x                       21x 13x   31x     21x               18x 18x                         21x 103x       103x     83x 69x   21x 21x 21x 3x 3x 3x   18x     21x 143x 49x    
import { __, n__ } from '~/locale';
import { getParameterValues } from '~/lib/utils/url_utility';
import {
  PARALLEL_DIFF_VIEW_TYPE,
  INLINE_DIFF_VIEW_TYPE,
  INLINE_DIFF_LINES_KEY,
} from '../constants';
import { computeSuggestionCommitMessage } from '../utils/suggestions';
import { parallelizeDiffLines } from './utils';
 
export * from './getters_versions_dropdowns';
 
export const isParallelView = (state) => state.diffViewType === PARALLEL_DIFF_VIEW_TYPE;
 
export const isInlineView = (state) => state.diffViewType === INLINE_DIFF_VIEW_TYPE;
 
export const whichCollapsedTypes = (state) => {
  const automatic = state.diffFiles.some((file) => file.viewer?.automaticallyCollapsed);
  const manual = state.diffFiles.some((file) => file.viewer?.manuallyCollapsed);
 
  return {
    any: automatic || manual,
    automatic,
    manual,
  };
};
 
export const commitId = (state) => (state.commit && state.commit.id ? state.commit.id : null);
 
/**
 * Checks if the diff has all discussions expanded
 * @param {Object} diff
 * @returns {Boolean}
 */
export const diffHasAllExpandedDiscussions = (state, getters) => (diff) => {
  const discussions = getters.getDiffFileDiscussions(diff);
 
  return (
    (discussions && discussions.length && discussions.every((discussion) => discussion.expanded)) ||
    false
  );
};
 
/**
 * Checks if the diff has all discussions collapsed
 * @param {Object} diff
 * @returns {Boolean}
 */
export const diffHasAllCollapsedDiscussions = (state, getters) => (diff) => {
  const discussions = getters.getDiffFileDiscussions(diff);
 
  return (
    (discussions &&
      discussions.length &&
      discussions.every((discussion) => !discussion.expanded)) ||
    false
  );
};
 
/**
 * Checks if the diff has any open discussions
 * @param {Object} diff
 * @returns {Boolean}
 */
export const diffHasExpandedDiscussions = () => (diff) => {
  const diffLineDiscussionsExpanded = diff[INLINE_DIFF_LINES_KEY].filter(
    (l) => l.discussions.length >= 1,
  ).some((l) => l.discussionsExpanded);
  const diffFileDiscussionsExpanded = diff.discussions?.some((d) => d.expanded);
 
  return diffFileDiscussionsExpanded || diffLineDiscussionsExpanded;
};
 
/**
 * Checks if every diff has every discussion open
 * @returns {Boolean}
 */
export const allDiffDiscussionsExpanded = (state) => {
  return state.diffFiles.every((diff) => {
    const highlightedLines = diff[INLINE_DIFF_LINES_KEY];
    if (highlightedLines.length) {
      return highlightedLines
        .filter((l) => l.discussions.length >= 1)
        .every((l) => l.discussionsExpanded);
    }
    if (diff.viewer.name === 'image') {
      return diff.discussions.every((discussion) => discussion.expandedOnDiff);
    }
    return true;
  });
};
 
/**
 * Checks if the diff has any discussion
 * @param {Boolean} diff
 * @returns {Boolean}
 */
export const diffHasDiscussions = () => (diff) => {
  return (
    diff.discussions?.length >= 1 ||
    diff[INLINE_DIFF_LINES_KEY].some((l) => l.discussions.length >= 1)
  );
};
 
/**
 * Returns an array with the discussions of the given diff
 * @param {Object} diff
 * @returns {Array}
 */
export const getDiffFileDiscussions = (state, getters, rootState, rootGetters) => (diff) =>
  rootGetters.discussions.filter(
    (discussion) => discussion.diff_discussion && discussion.diff_file.file_hash === diff.file_hash,
  ) || [];
 
export const getDiffFileByHash = (state) => (fileHash) =>
  state.diffFiles.find((file) => file.file_hash === fileHash);
 
export function isTreePathLoaded(state) {
  return (path) => {
    return Boolean(state.treeEntries[path]?.diffLoaded);
  };
}
 
export const flatBlobsList = (state) =>
  Object.values(state.treeEntries).filter((f) => f.type === 'blob');
 
export const allBlobs = (state, getters) =>
  getters.flatBlobsList.reduce((acc, file) => {
    const { parentPath } = file;
 
    if (parentPath && !acc.some((f) => f.path === parentPath)) {
      acc.push({
        path: parentPath,
        isHeader: true,
        tree: [],
      });
    }
 
    acc.find((f) => f.path === parentPath).tree.push(file);
 
    return acc;
  }, []);
 
export const getCommentFormForDiffFile = (state) => (fileHash) =>
  state.commentForms.find((form) => form.fileHash === fileHash);
 
/**
 * Returns the test coverage hits for a specific line of a given file
 * @param {string} file
 * @param {number} line
 * @returns {number}
 */
export const fileLineCoverage = (state) => (file, line) => {
  Iif (!state.coverageFiles.files) return {};
  const fileCoverage = state.coverageFiles.files[file];
  if (!fileCoverage) return {};
  const lineCoverage = fileCoverage[String(line)];
 
  if (lineCoverage === 0) {
    return { text: __('No test coverage'), class: 'no-coverage' };
  }
  Eif (lineCoverage >= 0) {
    return {
      text: n__('Test coverage: %d hit', 'Test coverage: %d hits', lineCoverage),
      class: 'coverage',
    };
  }
  return {};
};
 
/**
 * Returns index of a currently selected diff in diffFiles
 * @returns {number}
 */
export const currentDiffIndex = (state) =>
  Math.max(
    0,
    flatBlobsList(state).findIndex((diff) => diff.fileHash === state.currentDiffFileId),
  );
 
export const diffLines = (state) => (file) => {
  return parallelizeDiffLines(
    file.highlighted_diff_lines || [],
    state.diffViewType === INLINE_DIFF_VIEW_TYPE,
  );
};
 
export function suggestionCommitMessage(state, _, rootState) {
  return (values = {}) =>
    computeSuggestionCommitMessage({
      message: state.defaultSuggestionCommitMessage,
      values: {
        branch_name: rootState.page.mrMetadata.branch_name,
        project_path: rootState.page.mrMetadata.project_path,
        project_name: rootState.page.mrMetadata.project_name,
        username: rootState.page.mrMetadata.username,
        user_full_name: rootState.page.mrMetadata.user_full_name,
        ...values,
      },
    });
}
 
export const isVirtualScrollingEnabled = (state) => {
  Iif (state.disableVirtualScroller || getParameterValues('virtual_scrolling')[0] === 'false') {
    return false;
  }
 
  return !state.viewDiffsFileByFile;
};
 
export const isBatchLoading = (state) => state.batchLoadingState === 'loading';
export const isBatchLoadingError = (state) => state.batchLoadingState === 'error';
 
export const diffFiles = (state, getters) => {
  const { pinnedFile } = getters;
  if (pinnedFile) {
    const diffs = state.diffFiles.slice(0);
    diffs.splice(diffs.indexOf(pinnedFile), 1);
    return [pinnedFile, ...diffs];
  }
  return state.diffFiles;
};
 
export const pinnedFile = (state) => {
  if (!state.pinnedFileHash) return null;
  return state.diffFiles.find((file) => file.file_hash === state.pinnedFileHash);
};