• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

EduardSergeev / vscode-haskutil / 9620900570

21 Jun 2024 11:20PM UTC coverage: 97.25% (+1.2%) from 96.059%
9620900570

Pull #90

github

web-flow
Merge 0d7c7e45a into 3479e6943
Pull Request #90: Purple Yolk tests

274 of 292 branches covered (93.84%)

89 of 93 new or added lines in 3 files covered. (95.7%)

13 existing lines in 7 files now uncovered.

1273 of 1309 relevant lines covered (97.25%)

28.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.5
/src/features/importProvider/importDeclaration.ts
1
import { Range, TextDocument } from "vscode";
2✔
2
import Configuration from "../../configuration";
2✔
3

2✔
4
export default class ImportDeclaration {
2✔
5
  private _before: string = '';
2✔
6
  private _importElements: string[] = [];
2✔
7
  private _importSeparators: string[] = [];
2✔
8
  private _after: string = '';
2✔
9
  qualified: string = " ";
2✔
10
  alias?: string;
2✔
11
  importList?: string;
2✔
12
  hidingList?: string;
2✔
13
  offset?: number;
2✔
14
  length?: number;
2✔
15

2✔
16
  constructor(
2✔
17
    public module: string,
186✔
18
    optional?: {
186✔
19
      qualified?: string,
186✔
20
      alias?: string,
186✔
21
      importList?: string,
186✔
22
      importElements?: string,
186✔
23
      hidingList?: string,
186✔
24
      offset?: number,
186✔
25
      length?: number
186✔
26
    }) {
186✔
27
    if (optional) {
186✔
28
      this.qualified = optional.qualified || "";
172!
29
      this.alias = optional.alias;
172✔
30
      this.importList = optional.importList;
172✔
31
      this.importElements = optional.importElements;
172✔
32
      this.hidingList = optional.hidingList;
172✔
33
      this.offset = optional.offset;
172✔
34
      this.length = optional.length;
172✔
35
    }
172✔
36
  }
186✔
37

2✔
38
  private get separator() {
2✔
39
    return ', '
4✔
40
  };
2✔
41

2✔
42
  public get importElements() {
2✔
43
    const separators = this._importSeparators.concat('');
56✔
44
    const list = this._importElements.flatMap((elem, i) => [elem, separators[i]]);
56✔
45
    return [this._before, ...list, this._after].join('');
56✔
46
  }
56✔
47

2✔
48
  public set importElements(elementsString: string) {
2✔
49
    const input = elementsString ?? '';
172✔
50
    const empty = /^\s*$/g;
172✔
51
    const before = /^\s*/g;
172✔
52
    const after = /\s*$/g;
172✔
53
    const separators = /(?<=\S)\s*,\s*(?=\S)/g;
172✔
54
    if (empty.test(input)) {
172✔
55
      const middle = input.length / 2;
96✔
56
      this._before = input.slice(0, middle);
96✔
57
      this._after = input.slice(middle)
96✔
58
    } else {
172✔
59
      this._before = input.match(before)[0];
76✔
60
      this._after = input.match(after)[0];
76✔
61
    }
76✔
62
    const matches = [...input.matchAll(separators)].map(m => [m.index, m[0]] as const);
172✔
63
    this._importSeparators = matches.map(m => m[1]);
172✔
64
    const indices = matches.map(m => [m[0], m[0] + m[1].length] as const);
172✔
65
    const starts = [this._before.length].concat(indices.map(ixs => ixs[1]));
172✔
66
    const ends = indices.map(ixs => ixs[0]).concat(input.length - this._after.length);
172✔
67
    this._importElements = starts.map((ix, i) => input.slice(ix, ends[i])).filter(e => e !== '');
172✔
68
  }
172✔
69

2✔
70
  public addImportElement(newElem: string) {
2✔
71
    let before = `(${this.importElements})`;
14✔
72
    if (!this.importList) {
14✔
73
      this.importList = " ()";
8✔
74
      before = "()";
8✔
75
    }
8✔
76

14✔
77
    let index = this._importElements.findIndex(oldElem => this.compareImportElements(newElem, oldElem) < 0);
14✔
78
    index = index === -1 ? this._importElements.length : index;
14✔
79
    if (this._importElements.length > 0) {
14✔
80
      if (index === this._importElements.length) {
4!
NEW
81
        this._importSeparators.push(this.separator);
×
82
      } else {
4✔
83
        this._importSeparators.splice(index, 0, this.separator);
4✔
84
      }
4✔
85
    }
4✔
86
    this._importElements.splice(index, 0, newElem);
14✔
87

14✔
88
    this.importList = this.importList.replace(before, `(${this.importElements})`);
14✔
89
  }
14✔
90

2✔
91
  public removeElement(elem: string) {
2✔
92
    const before = this.importElements;
10✔
93

10✔
94
    const index = this._importElements.findIndex(oldElem => oldElem === elem || oldElem.replace(' ', '') == `${elem}(..)`);
10✔
95
    if (index !== -1) {
10✔
96
      if (this._importElements.length > 1) {
10✔
97
        if (index === this._importElements.length - 1) {
10✔
98
          this._importSeparators.pop();
4✔
99
        } else {
10✔
100
          this._importSeparators.splice(index, 1);
6✔
101
        }
6✔
102
      }
10✔
103
      this._importElements.splice(index, 1);
10✔
104

10✔
105
      this.importList = this.importList.replace(before, this.importElements);
10✔
106
    }
10✔
107
  }
10✔
108

2✔
109
  public get importElementsSorted() {
2✔
110
    return [...this._importElements]
94✔
111
      .sort(this.compareImportElements)
94✔
112
      .every((elem, i) => this._importElements[i] === elem);
94✔
113
  }
94✔
114

2✔
115
  public sortImportElements() {
2✔
116
    if (this._importElements.length > 0) {
4✔
117
      const before = this.importElements;
4✔
118
      this._importElements.sort(this.compareImportElements);
4✔
119
      this.importList = this.importList.replace(before, this.importElements);
4✔
120
    }
4✔
121
  }  
2✔
122

2✔
123
  private compareImportElements(left: string, right: string) {
2✔
124
    const toSortable = (elem: string) => elem.replace('(', '~');
64✔
125
    if (Configuration.shouldplaceOperatorsAfterFunctions) {
64!
NEW
126
      left = toSortable(left);
×
NEW
127
      right = toSortable(right);
×
NEW
128
    }
×
129
    return left < right ? -1 : left > right ? 1 : 0;
64!
130
  }
64✔
131

2✔
132
  public get text() {
2✔
133
    return `import${this.qualified || ""}${this.module}${this.alias || ""}${this.importList || ""}${this.hidingList || ""}`;
104!
134
  }
104✔
135

2✔
136
  public getRange(document: TextDocument): Range {
2✔
137
    return new Range(
168✔
138
      document.positionAt(this.offset),
168✔
139
      document.positionAt(this.offset + this.length));
168✔
140
  }
168✔
141

2✔
142
  public static getImports(text: string): ImportDeclaration[] {
2✔
143
    const importPattern = /^import((?:\s+qualified\s+)|\s+)(\S+)(\s+as\s+(\S+))?(\s*?\(((?:(?:\(.*?\))|.|\r?\n)*?)\))?(\s+hiding\s+\(((?:(?:\(.*?\))|.|\r?\n)*?)\))?/gm;
72✔
144
    const imports = [];
72✔
145
    for (let match; match = importPattern.exec(text);) {
72✔
146
      imports.push(new ImportDeclaration(
172✔
147
        match[2],
172✔
148
        {
172✔
149
          qualified: match[1],
172✔
150
          alias: match[3],
172✔
151
          importList: match[5],
172✔
152
          importElements: match[6],
172✔
153
          hidingList: match[7],
172✔
154
          offset: match.index,
172✔
155
          length: match[0].length
172✔
156
        }));
172✔
157
    }
172✔
158
    return imports;
72✔
159
  }
72✔
160
}
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc