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

source-academy / js-slang / 15235726114

25 May 2025 07:55AM UTC coverage: 77.048%. First build
15235726114

Pull #1742

github

web-flow
Merge d7783cf1e into 0be74e78c
Pull Request #1742: Rewrite: Stepper

3433 of 4826 branches covered (71.14%)

Branch coverage included in aggregate %.

1032 of 1260 new or added lines in 27 files covered. (81.9%)

10099 of 12737 relevant lines covered (79.29%)

140954.8 hits per line

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

84.09
/src/tracer/nodes/Expression/ConditionalExpression.ts
1
import { ConditionalExpression, Comment, SourceLocation } from 'estree'
2
import { StepperBaseNode } from '../../interface'
3
import { redex } from '../..'
61✔
4
import { StepperExpression, StepperPattern } from '..'
5
import { convert } from '../../generator'
61✔
6

7
export class StepperConditionalExpression implements ConditionalExpression, StepperBaseNode {
61✔
8
  type: 'ConditionalExpression'
9
  test: StepperExpression
10
  consequent: StepperExpression
11
  alternate: StepperExpression
12
  leadingComments?: Comment[]
13
  trailingComments?: Comment[]
14
  loc?: SourceLocation | null
15
  range?: [number, number]
16

17
  constructor(
18
    test: StepperExpression,
19
    consequent: StepperExpression,
20
    alternate: StepperExpression,
21
    leadingComments?: Comment[],
22
    trailingComments?: Comment[],
23
    loc?: SourceLocation | null,
24
    range?: [number, number]
25
  ) {
26
    this.type = 'ConditionalExpression'
1,762✔
27
    this.test = test
1,762✔
28
    this.consequent = consequent
1,762✔
29
    this.alternate = alternate
1,762✔
30
    this.leadingComments = leadingComments
1,762✔
31
    this.trailingComments = trailingComments
1,762✔
32
    this.loc = loc
1,762✔
33
    this.range = range
1,762✔
34
  }
35

36
  static create(node: ConditionalExpression) {
37
    return new StepperConditionalExpression(
380✔
38
      convert(node.test) as StepperExpression,
39
      convert(node.consequent) as StepperExpression,
40
      convert(node.alternate) as StepperExpression,
41
      node.leadingComments,
42
      node.trailingComments,
43
      node.loc,
44
      node.range
45
    )
46
  }
47

48
  isContractible(): boolean {
49
    if (this.test.type !== 'Literal') return false
2,337✔
50
    const test_value = this.test.value
1,131✔
51
    if (typeof test_value !== 'boolean') {
1,131!
NEW
52
      throw new Error(
×
53
        `Line ${
54
          this.loc?.start.line || 0
×
55
        }: Expected boolean as condition, got ${typeof test_value}.`
56
      )
57
    }
58
    redex.preRedex = [this]
1,131✔
59
    return true
1,131✔
60
  }
61

62
  isOneStepPossible(): boolean {
63
    return this.isContractible() || this.test.isOneStepPossible()
1,816✔
64
  }
65

66
  contract(): StepperExpression {
67
    redex.preRedex = [this]
254✔
68
    if (this.test.type !== 'Literal' || typeof this.test.value !== 'boolean') {
254!
NEW
69
      throw new Error('Cannot contract non-boolean literal test')
×
70
    }
71

72
    const result = this.test.value ? this.consequent : this.alternate
254✔
73
    redex.postRedex = [result]
254✔
74
    return result
254✔
75
  }
76

77
  oneStep(): StepperExpression {
78
    if (this.isContractible()) {
521✔
79
      return this.contract()
254✔
80
    }
81

82
    return new StepperConditionalExpression(
267✔
83
      this.test.oneStep(),
84
      this.consequent,
85
      this.alternate,
86
      this.leadingComments,
87
      this.trailingComments,
88
      this.loc,
89
      this.range
90
    )
91
  }
92

93
  substitute(id: StepperPattern, value: StepperExpression): StepperExpression {
94
    return new StepperConditionalExpression(
1,115✔
95
      this.test.substitute(id, value),
96
      this.consequent.substitute(id, value),
97
      this.alternate.substitute(id, value),
98
      this.leadingComments,
99
      this.trailingComments,
100
      this.loc,
101
      this.range
102
    )
103
  }
104

105
  freeNames(): string[] {
106
    return Array.from(
144✔
107
      new Set([
108
        ...this.test.freeNames(),
109
        ...this.consequent.freeNames(),
110
        ...this.alternate.freeNames()
111
      ])
112
    )
113
  }
114

115
  allNames(): string[] {
116
    return Array.from(
541✔
117
      new Set([
118
        ...this.test.allNames(),
119
        ...this.consequent.allNames(),
120
        ...this.alternate.allNames()
121
      ])
122
    )
123
  }
124

125
  rename(before: string, after: string): StepperExpression {
NEW
126
    return new StepperConditionalExpression(
×
127
      this.test.rename(before, after),
128
      this.consequent.rename(before, after),
129
      this.alternate.rename(before, after),
130
      this.leadingComments,
131
      this.trailingComments,
132
      this.loc,
133
      this.range
134
    )
135
  }
136
}
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