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

pmcelhaney / counterfact / 3919348865

pending completion
3919348865

push

github

GitHub
Update dependency eslint-config-hardcore to v26

398 of 420 branches covered (94.76%)

Branch coverage included in aggregate %.

1802 of 2023 relevant lines covered (89.08%)

22.18 hits per line

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

94.14
/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;
52✔
8
    this.exports = new Map();
52✔
9
    this.imports = new Map();
52✔
10
    this.externalImports = new Map();
52✔
11
    this.cache = new Map();
52✔
12
    this.typeCache = new Map();
52✔
13
    this.path = path;
52✔
14
  }
52✔
15

1✔
16
  firstUniqueName(coder) {
1✔
17
    for (const name of coder.names()) {
123✔
18
      if (!this.imports.has(name) && !this.exports.has(name)) {
146✔
19
        return name;
123✔
20
      }
123✔
21
    }
146✔
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
89✔
28
      ? "default"
63✔
29
      : `${coder.id}@${nodePath}:${isType}`;
63✔
30

89✔
31
    if (this.cache.has(cacheKey)) {
89✔
32
      return this.cache.get(cacheKey);
24✔
33
    }
24✔
34

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

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

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

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

65✔
55
        return availableCoder;
65✔
56
      })
65✔
57
      // eslint-disable-next-line promise/prefer-await-to-then
65✔
58
      .catch((error) => {
65✔
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
65✔
63
      .finally(() => {
65✔
64
        exportStatement.done = true;
65✔
65
      });
65✔
66

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

38✔
118
    return name;
38✔
119
  }
38✔
120

1✔
121
  importExternalType(name, modulePath) {
1✔
122
    return this.importExternal(name, modulePath, true);
38✔
123
  }
38✔
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(
39✔
131
      (exportStatement) => !exportStatement.done
39✔
132
    );
39✔
133
  }
39✔
134

1✔
135
  finished() {
1✔
136
    return Promise.all(
38✔
137
      Array.from(this.exports.values(), (value) => value.promise)
38✔
138
    );
38✔
139
  }
38✔
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

© 2025 Coveralls, Inc