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

source-academy / js-slang / 13622100329

03 Mar 2025 02:25AM UTC coverage: 81.126% (-0.005%) from 81.131%
13622100329

Pull #1743

github

web-flow
Merge 68a52672d into 3c5afad3e
Pull Request #1743: Remove lazy

3440 of 4608 branches covered (74.65%)

Branch coverage included in aggregate %.

13 of 13 new or added lines in 3 files covered. (100.0%)

15 existing lines in 4 files now uncovered.

10762 of 12898 relevant lines covered (83.44%)

134575.29 hits per line

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

82.57
/src/utils/ast/astCreator.ts
1
import type es from 'estree'
2

3
import {
4
  AllowedDeclarations,
5
  type BlockExpression,
6
  type FunctionDeclarationExpression,
7
  type Node,
8
  type StatementSequence
9
} from '../../types'
10

11
export const getVariableDeclarationName = (decl: es.VariableDeclaration) =>
74✔
12
  (decl.declarations[0].id as es.Identifier).name
22,193✔
13

14
export const locationDummyNode = (line: number, column: number, source: string | null) =>
74✔
15
  literal('Dummy', { start: { line, column }, end: { line, column }, source })
49,775,198✔
16

17
export const identifier = (name: string, loc?: es.SourceLocation | null): es.Identifier => ({
740,438✔
18
  type: 'Identifier',
19
  name,
20
  loc
21
})
22

23
export const importDeclaration = (
74✔
24
  source: string,
25
  specifiers: es.ImportDeclaration['specifiers'],
26
  loc?: es.SourceLocation | null
27
): es.ImportDeclaration => ({
31✔
28
  type: 'ImportDeclaration',
29
  source: literal(source),
30
  specifiers,
31
  loc
32
})
33

34
export const importSpecifier = (
74✔
35
  importedName: string,
36
  localName: string,
37
  loc?: es.SourceLocation | null
38
): es.ImportSpecifier => ({
44✔
39
  type: 'ImportSpecifier',
40
  imported: identifier(importedName),
41
  local: identifier(localName),
42
  loc
43
})
44

45
export const importDefaultSpecifier = (
74✔
46
  localName: string,
47
  loc?: es.SourceLocation | null
48
): es.ImportDefaultSpecifier => ({
7✔
49
  type: 'ImportDefaultSpecifier',
50
  local: identifier(localName),
51
  loc
52
})
53

54
export const importNamespaceSpecifier = (
74✔
55
  localName: string,
56
  loc?: es.SourceLocation | null
57
): es.ImportNamespaceSpecifier => ({
1✔
58
  type: 'ImportNamespaceSpecifier',
59
  local: identifier(localName),
60
  loc
61
})
62

63
export const literal = (
74✔
64
  value: string | number | boolean | null,
65
  loc?: es.SourceLocation | null
66
): es.Literal => ({
51,041,384✔
67
  type: 'Literal',
68
  value,
69
  loc
70
})
71

72
export const memberExpression = (
74✔
73
  object: es.Expression,
74
  property: string | number
75
): es.MemberExpression => ({
160,871✔
76
  type: 'MemberExpression',
77
  object,
78
  computed: typeof property === 'number',
79
  optional: false,
80
  property: typeof property === 'number' ? literal(property) : identifier(property)
160,871✔
81
})
82

83
export const declaration = (
74✔
84
  name: string,
85
  kind: AllowedDeclarations,
86
  init: es.Expression,
87
  loc?: es.SourceLocation | null
88
): es.VariableDeclaration => ({
97,781✔
89
  type: 'VariableDeclaration',
90
  declarations: [
91
    {
92
      type: 'VariableDeclarator',
93
      id: identifier(name),
94
      init
95
    }
96
  ],
97
  kind,
98
  loc
99
})
100

101
export const constantDeclaration = (
74✔
102
  name: string,
103
  init: es.Expression,
104
  loc?: es.SourceLocation | null
105
) => declaration(name, 'const', init, loc)
95,302✔
106

107
export const callExpression = (
74✔
108
  callee: es.Expression,
109
  args: (es.Expression | es.SpreadElement)[],
110
  loc?: es.SourceLocation | null
111
): es.CallExpression => ({
298,027✔
112
  type: 'CallExpression',
113
  callee,
114
  arguments: args,
115
  optional: false,
116
  loc
117
})
118

119
export const expressionStatement = (expression: es.Expression): es.ExpressionStatement => ({
15,600✔
120
  type: 'ExpressionStatement',
121
  expression
122
})
123

124
export const blockArrowFunction = (
74✔
125
  params: es.Identifier[],
126
  body: es.Statement[] | es.BlockStatement | es.Expression,
127
  loc?: es.SourceLocation | null
128
): es.ArrowFunctionExpression => ({
51,172✔
129
  type: 'ArrowFunctionExpression',
130
  expression: false,
131
  generator: false,
132
  params,
133
  body: Array.isArray(body) ? blockStatement(body) : body,
51,172!
134
  loc
135
})
136

137
export const functionExpression = (
74✔
138
  params: es.Pattern[],
139
  body: es.Statement[] | es.BlockStatement,
140
  loc?: es.SourceLocation | null,
141
  id?: es.Identifier
142
): es.FunctionExpression => ({
×
143
  type: 'FunctionExpression',
144
  id: id ?? null,
×
145
  async: false,
146
  generator: false,
147
  params,
148
  body: Array.isArray(body) ? blockStatement(body) : body,
×
149
  loc
150
})
151

152
export const blockStatement = (
74✔
153
  body: es.Statement[],
154
  loc?: es.SourceLocation | null
155
): es.BlockStatement => ({
72,873✔
156
  type: 'BlockStatement',
157
  body,
158
  loc
159
})
160

161
export const statementSequence = (
74✔
162
  body: es.Statement[],
163
  loc?: es.SourceLocation | null
164
): StatementSequence => ({
266,932✔
165
  type: 'StatementSequence',
166
  body,
167
  loc
168
})
169

170
export const program = (body: es.Statement[]): es.Program => ({
11,810✔
171
  type: 'Program',
172
  sourceType: 'module',
173
  body
174
})
175

176
export const returnStatement = (
74✔
177
  argument: es.Expression,
178
  loc?: es.SourceLocation | null
179
): es.ReturnStatement => ({
56,143✔
180
  type: 'ReturnStatement',
181
  argument,
182
  loc
183
})
184

185
export const property = (key: string, value: es.Expression): es.Property => ({
152,025✔
186
  type: 'Property',
187
  method: false,
188
  shorthand: false,
189
  computed: false,
190
  key: identifier(key),
191
  value,
192
  kind: 'init'
193
})
194

195
export const objectExpression = (properties: es.Property[]): es.ObjectExpression => ({
27,405✔
196
  type: 'ObjectExpression',
197
  properties
198
})
199

200
export const mutateToCallExpression = (
74✔
201
  node: Node,
202
  callee: es.Expression,
203
  args: es.Expression[]
204
) => {
205
  node.type = 'CallExpression'
45,456✔
206
  node = node as es.CallExpression
45,456✔
207
  node.callee = callee
45,456✔
208
  node.arguments = args
45,456✔
209
}
210

211
export const mutateToAssignmentExpression = (
74✔
212
  node: Node,
213
  left: es.Pattern,
214
  right: es.Expression
215
) => {
216
  node.type = 'AssignmentExpression'
×
217
  node = node as es.AssignmentExpression
×
218
  node.operator = '='
×
219
  node.left = left
×
220
  node.right = right
×
221
}
222

223
export const mutateToExpressionStatement = (node: Node, expr: es.Expression) => {
74✔
UNCOV
224
  node.type = 'ExpressionStatement'
×
UNCOV
225
  node = node as es.ExpressionStatement
×
UNCOV
226
  node.expression = expr
×
227
}
228

229
export const mutateToReturnStatement = (node: Node, expr: es.Expression) => {
74✔
230
  node.type = 'ReturnStatement'
506✔
231
  node = node as es.ReturnStatement
506✔
232
  node.argument = expr
506✔
233
}
234

235
export const mutateToMemberExpression = (node: Node, obj: es.Expression, prop: es.Expression) => {
74✔
236
  node.type = 'MemberExpression'
×
237
  node = node as es.MemberExpression
×
238
  node.object = obj
×
239
  node.property = prop
×
240
  node.computed = false
×
241
}
242

243
export const logicalExpression = (
74✔
244
  operator: es.LogicalOperator,
245
  left: es.Expression,
246
  right: es.Expression,
247
  loc?: es.SourceLocation | null
248
): es.LogicalExpression => ({
3,373✔
249
  type: 'LogicalExpression',
250
  operator,
251
  left,
252
  right,
253
  loc
254
})
255

256
export const mutateToConditionalExpression = (
74✔
257
  node: Node,
258
  test: es.Expression,
259
  consequent: es.Expression,
260
  alternate: es.Expression
261
) => {
262
  node.type = 'ConditionalExpression'
377✔
263
  node = node as es.ConditionalExpression
377✔
264
  node.test = test
377✔
265
  node.consequent = consequent
377✔
266
  node.alternate = alternate
377✔
267
}
268

269
export const conditionalExpression = (
74✔
270
  test: es.Expression,
271
  consequent: es.Expression,
272
  alternate: es.Expression,
273
  loc?: es.SourceLocation | null
274
): es.ConditionalExpression => ({
60,883✔
275
  type: 'ConditionalExpression',
276
  test,
277
  consequent,
278
  alternate,
279
  loc
280
})
281

282
export const arrayExpression = (elements: es.Expression[]): es.ArrayExpression => ({
34,474✔
283
  type: 'ArrayExpression',
284
  elements
285
})
286

287
export const assignmentExpression = (
74✔
288
  left: es.Identifier | es.MemberExpression,
289
  right: es.Expression,
290
  loc?: es.SourceLocation | null
291
): es.AssignmentExpression => ({
1,533✔
292
  type: 'AssignmentExpression',
293
  operator: '=',
294
  left,
295
  right,
296
  loc
297
})
298

299
export const binaryExpression = (
74✔
300
  operator: es.BinaryOperator,
301
  left: es.Expression,
302
  right: es.Expression,
303
  loc?: es.SourceLocation | null
304
): es.BinaryExpression => ({
159,676✔
305
  type: 'BinaryExpression',
306
  operator,
307
  left,
308
  right,
309
  loc
310
})
311

312
export const unaryExpression = (
74✔
313
  operator: es.UnaryOperator,
314
  argument: es.Expression,
315
  loc?: es.SourceLocation | null
316
): es.UnaryExpression => ({
430✔
317
  type: 'UnaryExpression',
318
  operator,
319
  prefix: true,
320
  argument,
321
  loc
322
})
323

324
// primitive: undefined is a possible value
325
export const primitive = (value: any): es.Expression => {
74✔
326
  return value === undefined ? identifier('undefined') : literal(value)
732,346✔
327
}
328

329
export const functionDeclarationExpression = (
74✔
330
  id: es.Identifier,
331
  params: es.Pattern[],
332
  body: es.BlockStatement,
333
  loc?: es.SourceLocation | null
334
): FunctionDeclarationExpression => ({
12,626✔
335
  type: 'FunctionExpression',
336
  id,
337
  params,
338
  body,
339
  loc
340
})
341

342
export const functionDeclaration = (
74✔
343
  id: es.Identifier,
344
  params: es.Pattern[],
345
  body: es.BlockStatement,
346
  loc?: es.SourceLocation | null
347
): es.FunctionDeclaration => ({
34,288✔
348
  type: 'FunctionDeclaration',
349
  id,
350
  params,
351
  body,
352
  loc
353
})
354

355
export const blockExpression = (
74✔
356
  body: es.Statement[],
357
  loc?: es.SourceLocation | null
358
): BlockExpression => ({
362✔
359
  type: 'BlockExpression',
360
  body,
361
  loc
362
})
363

364
export const arrowFunctionExpression = (
74✔
365
  params: es.Pattern[],
366
  body: es.Expression | es.BlockStatement,
367
  loc?: es.SourceLocation | null
368
): es.ArrowFunctionExpression => ({
25,699✔
369
  type: 'ArrowFunctionExpression',
370
  expression: body.type !== 'BlockStatement',
371
  generator: false,
372
  params,
373
  body,
374
  loc
375
})
376

377
export const variableDeclaration = (
74✔
378
  declarations: es.VariableDeclarator[],
379
  loc?: es.SourceLocation | null
380
): es.VariableDeclaration => ({
20,079✔
381
  type: 'VariableDeclaration',
382
  kind: 'const',
383
  declarations,
384
  loc
385
})
386

387
export const variableDeclarator = (
74✔
388
  id: es.Pattern,
389
  init: es.Expression,
390
  loc?: es.SourceLocation | null
391
): es.VariableDeclarator => ({
20,080✔
392
  type: 'VariableDeclarator',
393
  id,
394
  init,
395
  loc
396
})
397

398
export const ifStatement = (
74✔
399
  test: es.Expression,
400
  consequent: es.BlockStatement,
401
  alternate: es.Statement,
402
  loc?: es.SourceLocation | null
403
): es.IfStatement => ({
8,251✔
404
  type: 'IfStatement',
405
  test,
406
  consequent,
407
  alternate,
408
  loc
409
})
410

411
export const whileStatement = (
74✔
412
  body: es.BlockStatement,
413
  test: es.Expression,
414
  loc?: es.SourceLocation | null
415
): es.WhileStatement => ({
9✔
416
  type: 'WhileStatement',
417
  test,
418
  body,
419
  loc
420
})
421

422
export const forStatement = (
74✔
423
  init: es.VariableDeclaration | es.Expression,
424
  test: es.Expression,
425
  update: es.Expression,
426
  body: es.Statement,
427
  loc?: es.SourceLocation | null
428
): es.ForStatement => ({
337✔
429
  type: 'ForStatement',
430
  init,
431
  test,
432
  update,
433
  body,
434
  loc
435
})
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