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

pmcelhaney / counterfact / 5942933897

22 Aug 2023 06:59PM UTC coverage: 83.567% (-3.8%) from 87.388%
5942933897

push

github

web-flow
Merge pull request #517 from pmcelhaney/windows-support

disable the end-to-end test because it won't work on Windows

389 of 423 branches covered (91.96%)

Branch coverage included in aggregate %.

1818 of 2218 relevant lines covered (81.97%)

9.64 hits per line

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

88.46
/src/typescript-generator/script.js
1
import nodePath from "node:path";
1✔
2

1✔
3
import prettier from "prettier";
1✔
4

1✔
5
export class Script {
1✔
6
  constructor(repository, path) {
1✔
7
    this.repository = repository;
14✔
8
    this.exports = new Map();
14✔
9
    this.imports = new Map();
14✔
10
    this.externalImports = new Map();
14✔
11
    this.cache = new Map();
14✔
12
    this.typeCache = new Map();
14✔
13
    this.path = path;
14✔
14
  }
14✔
15

1✔
16
  firstUniqueName(coder) {
1✔
17
    for (const name of coder.names()) {
23✔
18
      if (!this.imports.has(name) && !this.exports.has(name)) {
38✔
19
        return name;
23✔
20
      }
23✔
21
    }
38✔
22

×
23
    throw new Error(`could not find a unique name for ${coder.id}`);
×
24
  }
×
25

1✔
26
  export(coder, isType = false, isDefault = false) {
1✔
27
    const cacheKey = isDefault
15✔
28
      ? "default"
13✔
29
      : `${coder.id}@${nodePath}:${isType}`;
13✔
30

15✔
31
    if (this.cache.has(cacheKey)) {
15!
32
      return this.cache.get(cacheKey);
×
33
    }
×
34

15✔
35
    const name = this.firstUniqueName(coder);
15✔
36

15✔
37
    this.cache.set(cacheKey, name);
15✔
38

15✔
39
    const exportStatement = {
15✔
40
      id: coder.id,
15✔
41
      done: false,
15✔
42
      isType,
15✔
43
      isDefault,
15✔
44
      typeDeclaration: coder.typeDeclaration(this.exports, this),
15✔
45
      beforeExport: coder.beforeExport(this.path),
15✔
46
    };
15✔
47

15✔
48
    exportStatement.promise = coder
15✔
49
      .delegate()
15✔
50
      // eslint-disable-next-line promise/prefer-await-to-then
15✔
51
      .then((availableCoder) => {
15✔
52
        exportStatement.name = name;
15✔
53
        exportStatement.code = availableCoder.write(this);
15✔
54

15✔
55
        return availableCoder;
15✔
56
      })
15✔
57
      // eslint-disable-next-line promise/prefer-await-to-then
15✔
58
      .catch((error) => {
15✔
59
        exportStatement.code = `{/* error creating export "${name}" for ${this.path}: ${error.stack} */}`;
×
60
        exportStatement.error = error;
×
61
      })
×
62
      // eslint-disable-next-line promise/prefer-await-to-then
15✔
63
      .finally(() => {
15✔
64
        exportStatement.done = true;
15✔
65
      });
15✔
66

15✔
67
    this.exports.set(name, exportStatement);
15✔
68

15✔
69
    return name;
15✔
70
  }
15✔
71

1✔
72
  exportDefault(coder, isType = false) {
1✔
73
    this.export(coder, isType, true);
1✔
74
  }
1✔
75

1✔
76
  import(coder, isType = false, isDefault = false) {
1✔
77
    const modulePath = coder.modulePath();
10✔
78

10✔
79
    const cacheKey = `${coder.id}@${modulePath}:${isType}:${isDefault}`;
10✔
80

10✔
81
    if (this.cache.has(cacheKey)) {
10✔
82
      return this.cache.get(cacheKey);
2✔
83
    }
2✔
84

8✔
85
    const name = this.firstUniqueName(coder);
8✔
86

8✔
87
    this.cache.set(cacheKey, name);
8✔
88

8✔
89
    const scriptFromWhichToExport = this.repository.get(modulePath);
8✔
90

8✔
91
    const exportedName = scriptFromWhichToExport.export(
8✔
92
      coder,
8✔
93
      isType,
8✔
94
      isDefault
8✔
95
    );
8✔
96

8✔
97
    this.imports.set(name, {
8✔
98
      script: scriptFromWhichToExport,
8✔
99
      name: exportedName,
8✔
100
      isType,
8✔
101
      isDefault,
8✔
102
    });
8✔
103

8✔
104
    return name;
8✔
105
  }
8✔
106

1✔
107
  importType(coder) {
1✔
108
    return this.import(coder, true);
1✔
109
  }
1✔
110

1✔
111
  importDefault(coder, isType = false) {
1✔
112
    return this.import(coder, isType, true);
1✔
113
  }
1✔
114

1✔
115
  importExternal(name, modulePath, isType = false) {
1✔
116
    this.externalImports.set(name, { modulePath, isType });
×
117

×
118
    return name;
×
119
  }
×
120

1✔
121
  importExternalType(name, modulePath) {
1✔
122
    return this.importExternal(name, modulePath, true);
×
123
  }
×
124

1✔
125
  exportType(coder) {
1✔
126
    return this.export(coder, true);
1✔
127
  }
1✔
128

1✔
129
  isInProgress() {
1✔
130
    return Array.from(this.exports.values()).some(
×
131
      (exportStatement) => !exportStatement.done
×
132
    );
×
133
  }
×
134

1✔
135
  finished() {
1✔
136
    return Promise.all(
2✔
137
      Array.from(this.exports.values(), (value) => value.promise)
2✔
138
    );
2✔
139
  }
2✔
140

1✔
141
  externalImportsStatements() {
1✔
142
    return Array.from(
3✔
143
      this.externalImports,
3✔
144
      ([name, { modulePath, isType, isDefault }]) =>
3✔
145
        `import${isType ? " type" : ""} ${
×
146
          isDefault ? name : `{ ${name} }`
×
147
        } from "${modulePath}";`
×
148
    );
3✔
149
  }
3✔
150

1✔
151
  importStatements() {
1✔
152
    return Array.from(
3✔
153
      this.imports,
3✔
154
      ([name, { script, isType, isDefault }]) =>
3✔
155
        `import${isType ? " type" : ""} ${
3✔
156
          isDefault ? name : `{ ${name} }`
3✔
157
        } from "./${nodePath.relative(
3✔
158
          nodePath.dirname(this.path),
3✔
159
          script.path.replace(/\.ts$/u, ".js")
3✔
160
        )}";`
3✔
161
    );
3✔
162
  }
3✔
163

1✔
164
  exportStatements() {
1✔
165
    return Array.from(
3✔
166
      this.exports.values(),
3✔
167
      ({ name, isType, isDefault, code, typeDeclaration, beforeExport }) => {
3✔
168
        if (code.raw) {
7!
169
          return code.raw;
×
170
        }
×
171

7✔
172
        if (isDefault) {
7✔
173
          return `${beforeExport}export default ${code};`;
1✔
174
        }
1✔
175

6✔
176
        const keyword = isType ? "type" : "const";
7✔
177
        const typeAnnotation =
7✔
178
          typeDeclaration.length === 0 ? "" : `:${typeDeclaration}`;
7!
179

7✔
180
        return `${beforeExport}export ${keyword} ${name}${typeAnnotation} = ${code};`;
7✔
181
      }
7✔
182
    );
3✔
183
  }
3✔
184

1✔
185
  contents() {
1✔
186
    return prettier.format(
3✔
187
      [
3✔
188
        this.externalImportsStatements().join("\n"),
3✔
189
        this.importStatements().join("\n"),
3✔
190
        "\n\n",
3✔
191
        this.exportStatements().join("\n\n"),
3✔
192
      ].join(""),
3✔
193
      { parser: "typescript" }
3✔
194
    );
3✔
195
  }
3✔
196
}
1✔
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