• 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

75.0
/src/tracer/nodes/Statement/IfStatement.ts
1
import { Comment, IfStatement, SourceLocation } from 'estree'
2
import { StepperBaseNode } from '../../interface'
3
import { StepperExpression, StepperPattern, undefinedNode } from '..'
61✔
4
import { convert } from '../../generator'
61✔
5
import { redex } from '../..'
61✔
6
import { StepperLiteral } from '../Expression/Literal'
61✔
7
import { StepperBlockStatement } from './BlockStatement'
61✔
8
import { StepperExpressionStatement } from './ExpressionStatement'
61✔
9
import { StepperStatement } from '.'
10

11
export class StepperIfStatement implements IfStatement, StepperBaseNode {
61✔
12
  type: 'IfStatement'
13
  test: StepperExpression
14
  consequent: StepperStatement
15
  alternate: StepperStatement | null
16

17
  leadingComments?: Comment[] | undefined
18
  trailingComments?: Comment[] | undefined
19
  loc?: SourceLocation | null | undefined
20
  range?: [number, number] | undefined
21

22
  constructor(
23
    test: StepperExpression,
24
    consequent: StepperStatement,
25
    alternate: StepperStatement | null,
26
    leadingComments?: Comment[] | undefined,
27
    trailingComments?: Comment[] | undefined,
28
    loc?: SourceLocation | null | undefined,
29
    range?: [number, number] | undefined
30
  ) {
31
    this.type = 'IfStatement'
378✔
32
    this.test = test
378✔
33
    this.consequent = consequent
378✔
34
    this.alternate = alternate
378✔
35
    this.leadingComments = leadingComments
378✔
36
    this.trailingComments = trailingComments
378✔
37
    this.loc = loc
378✔
38
    this.range = range
378✔
39
  }
40

41
  static create(node: IfStatement) {
42
    return new StepperIfStatement(
76✔
43
      convert(node.test) as StepperExpression,
44
      convert(node.consequent) as StepperBlockStatement,
45
      node.alternate ? (convert(node.alternate) as StepperBlockStatement) : null,
76!
46
      node.leadingComments,
47
      node.trailingComments,
48
      node.loc,
49
      node.range
50
    )
51
  }
52

53
  isContractible(): boolean {
54
    return this.test instanceof StepperLiteral
382✔
55
  }
56

57
  contract(): StepperBlockStatement | StepperIfStatement {
58
    if (!(this.test instanceof StepperLiteral)) {
32!
NEW
59
      throw new Error('Cannot contract non-literal test')
×
60
    }
61

62
    redex.preRedex = [this]
32✔
63
    const result = this.test.value ? this.consequent : this.alternate || undefinedNode
32!
64

65
    if (result instanceof StepperBlockStatement) {
32✔
66
      redex.postRedex = [result]
29✔
67
      return new StepperBlockStatement(
29✔
68
        [
69
          new StepperExpressionStatement(undefinedNode, undefined, undefined, this.loc, this.range),
70
          ...result.body
71
        ],
72
        result.innerComments,
73
        this.leadingComments,
74
        this.trailingComments,
75
        this.loc,
76
        this.range
77
      )
78
    } else if (result instanceof StepperIfStatement) {
3!
79
      // else if statement
80
      return result
3✔
81
    } else {
NEW
82
      throw new Error('Cannot contract to non-block statement')
×
83
    }
84
  }
85

86
  isOneStepPossible(): boolean {
87
    return this.isContractible() || this.test.isOneStepPossible()
306✔
88
  }
89

90
  oneStep(): StepperIfStatement | StepperBlockStatement {
91
    if (!this.isOneStepPossible()) {
76!
NEW
92
      throw new Error('No step possible in test')
×
93
    }
94

95
    if (this.isContractible()) {
76✔
96
      return this.contract()
32✔
97
    }
98

99
    return new StepperIfStatement(
44✔
100
      this.test.oneStep() as StepperExpression,
101
      this.consequent,
102
      this.alternate,
103
      this.leadingComments,
104
      this.trailingComments,
105
      this.loc,
106
      this.range
107
    )
108
  }
109

110
  substitute(id: StepperPattern, value: StepperExpression): StepperBaseNode {
111
    return new StepperIfStatement(
258✔
112
      this.test.substitute(id, value) as StepperExpression,
113
      this.consequent.substitute(id, value) as StepperStatement,
114
      this.alternate ? (this.alternate.substitute(id, value) as StepperStatement) : null,
258!
115
      this.leadingComments,
116
      this.trailingComments,
117
      this.loc,
118
      this.range
119
    )
120
  }
121

122
  contractEmpty() {
NEW
123
    redex.preRedex = [this]
×
NEW
124
    redex.postRedex = []
×
125
  }
126

127
  freeNames(): string[] {
128
    const names = new Set([
83✔
129
      ...this.test.freeNames(),
130
      ...this.consequent.freeNames(),
131
      ...(this.alternate ? this.alternate.freeNames() : [])
83!
132
    ])
133
    return Array.from(names)
83✔
134
  }
135

136
  allNames(): string[] {
137
    const names = new Set([
405✔
138
      ...this.test.allNames(),
139
      ...this.consequent.allNames(),
140
      ...(this.alternate ? this.alternate.allNames() : [])
405!
141
    ])
142
    return Array.from(names)
405✔
143
  }
144

145
  rename(before: string, after: string): StepperIfStatement {
NEW
146
    return new StepperIfStatement(
×
147
      this.test.rename(before, after) as StepperExpression,
148
      this.consequent.rename(before, after) as StepperStatement,
149
      this.alternate ? (this.alternate.rename(before, after) as StepperStatement) : null,
×
150
      this.leadingComments,
151
      this.trailingComments,
152
      this.loc,
153
      this.range
154
    )
155
  }
156
}
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