Welcome to mirror list, hosted at ThFree Co, Russian Federation.

rules_mapper.js « editorconfig « lib « ide « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 25ffa9a15be32dd8265b0425610d9bc502625f6c (plain)
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
import { isBoolean, isNumber } from 'lodash';

const map = (key, validValues) => (value) =>
  value in validValues ? { [key]: validValues[value] } : {};

const bool = (key) => (value) => (isBoolean(value) ? { [key]: value } : {});

const int = (key, isValid) => (value) =>
  isNumber(value) && isValid(value) ? { [key]: Math.trunc(value) } : {};

const rulesMapper = {
  indent_style: map('insertSpaces', { tab: false, space: true }),
  indent_size: int('tabSize', (n) => n > 0),
  tab_width: int('tabSize', (n) => n > 0),
  trim_trailing_whitespace: bool('trimTrailingWhitespace'),
  end_of_line: map('endOfLine', { crlf: 1, lf: 0 }),
  insert_final_newline: bool('insertFinalNewline'),
};

const parseValue = (x) => {
  let value = typeof x === 'string' ? x.toLowerCase() : x;
  if (/^[0-9.-]+$/.test(value)) value = Number(value);
  if (value === 'true') value = true;
  if (value === 'false') value = false;

  return value;
};

export default function mapRulesToMonaco(rules) {
  return Object.entries(rules).reduce((obj, [key, value]) => {
    return Object.assign(obj, rulesMapper[key]?.(parseValue(value)) || {});
  }, {});
}