• 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

77.97
/src/tracer/nodes/Statement/VariableDeclaration.ts
1
import { Comment, SourceLocation, VariableDeclaration, VariableDeclarator } from 'estree'
2
import { StepperBaseNode } from '../../interface'
3
import { convert } from '../../generator'
61✔
4
import { StepperExpression, StepperPattern, undefinedNode } from '..'
61✔
5
import { redex } from '../..'
61✔
6

7
export class StepperVariableDeclarator implements VariableDeclarator, StepperBaseNode {
61✔
8
  type: 'VariableDeclarator'
9
  id: StepperPattern
10
  init?: StepperExpression | null | undefined
11
  leadingComments?: Comment[] | undefined
12
  trailingComments?: Comment[] | undefined
13
  loc?: SourceLocation | null | undefined
14
  range?: [number, number] | undefined
15

16
  constructor(
17
    id: StepperPattern,
18
    init: StepperExpression | null | undefined,
19
    leadingComments?: Comment[] | undefined,
20
    trailingComments?: Comment[] | undefined,
21
    loc?: SourceLocation | null | undefined,
22
    range?: [number, number] | undefined
23
  ) {
24
    this.type = 'VariableDeclarator'
1,623✔
25
    this.id = id
1,623✔
26
    this.init = init
1,623✔
27
    this.leadingComments = leadingComments
1,623✔
28
    this.trailingComments = trailingComments
1,623✔
29
    this.loc = loc
1,623✔
30
    this.range = range
1,623✔
31
  }
32

33
  static create(node: VariableDeclarator) {
34
    return new StepperVariableDeclarator(
348✔
35
      convert(node.id) as StepperPattern,
36
      node.init ? (convert(node.init) as StepperExpression) : node.init,
348!
37
      node.leadingComments,
38
      node.trailingComments,
39
      node.loc,
40
      node.range
41
    )
42
  }
43

44
  isContractible(): boolean {
NEW
45
    return this.init ? this.init.isContractible() : false
×
46
  }
47

48
  isOneStepPossible(): boolean {
49
    return this.init ? this.init.isOneStepPossible() : false
415!
50
  }
51

52
  contract(): StepperVariableDeclarator {
NEW
53
    return new StepperVariableDeclarator(
×
54
      this.id,
55
      this.init!.oneStep(),
56
      this.leadingComments,
57
      this.trailingComments,
58
      this.loc,
59
      this.range
60
    )
61
  }
62

63
  oneStep(): StepperVariableDeclarator {
64
    return new StepperVariableDeclarator(
90✔
65
      this.id,
66
      this.init!.oneStep(),
67
      this.leadingComments,
68
      this.trailingComments,
69
      this.loc,
70
      this.range
71
    )
72
  }
73

74
  substitute(id: StepperPattern, value: StepperExpression): StepperBaseNode {
75
    return new StepperVariableDeclarator(
1,141✔
76
      this.id,
77
      this.init!.substitute(id, value),
78
      this.leadingComments,
79
      this.trailingComments,
80
      this.loc,
81
      this.range
82
    )
83
  }
84

85
  freeNames(): string[] {
86
    return this.init!.freeNames()
90✔
87
  }
88

89
  allNames(): string[] {
90
    return this.init!.allNames()
527✔
91
  }
92

93
  rename(before: string, after: string): StepperVariableDeclarator {
94
    return new StepperVariableDeclarator(
4✔
95
      this.id.rename(before, after),
96
      this.init!.rename(before, after),
97
      this.leadingComments,
98
      this.trailingComments,
99
      this.loc,
100
      this.range
101
    )
102
  }
103
}
104

105
// After all variable declarators have been contracted,
106
// StepperVariableDeclaration::contract triggers substitution
107
export class StepperVariableDeclaration implements VariableDeclaration, StepperBaseNode {
61✔
108
  type: 'VariableDeclaration'
109
  declarations: StepperVariableDeclarator[]
110
  kind: 'var' | 'let' | 'const'
111
  leadingComments?: Comment[] | undefined
112
  trailingComments?: Comment[] | undefined
113
  loc?: SourceLocation | null | undefined
114
  range?: [number, number] | undefined
115

116
  constructor(
117
    declarations: StepperVariableDeclarator[],
118
    kind: 'var' | 'let' | 'const',
119
    leadingComments?: Comment[] | undefined,
120
    trailingComments?: Comment[] | undefined,
121
    loc?: SourceLocation | null | undefined,
122
    range?: [number, number] | undefined
123
  ) {
124
    this.type = 'VariableDeclaration'
1,583✔
125
    this.declarations = declarations
1,583✔
126
    this.kind = kind
1,583✔
127
    this.leadingComments = leadingComments
1,583✔
128
    this.trailingComments = trailingComments
1,583✔
129
    this.loc = loc
1,583✔
130
    this.range = range
1,583✔
131
  }
132

133
  static create(node: VariableDeclaration) {
134
    return new StepperVariableDeclaration(
348✔
135
      node.declarations.map(node => convert(node) as StepperVariableDeclarator),
348✔
136
      node.kind,
137
      node.leadingComments,
138
      node.trailingComments,
139
      node.loc,
140
      node.range
141
    )
142
  }
143

144
  isContractible(): boolean {
NEW
145
    return false
×
146
  }
147

148
  isOneStepPossible(): boolean {
149
    return this.declarations
325✔
150
      .map(x => x.isOneStepPossible())
325✔
151
      .reduce((acc, next) => acc || next, false)
325✔
152
  }
153

154
  contract(): typeof undefinedNode {
NEW
155
    redex.preRedex = [this]
×
NEW
156
    redex.postRedex = []
×
NEW
157
    return undefinedNode
×
158
  }
159

160
  contractEmpty() {
NEW
161
    redex.preRedex = [this]
×
NEW
162
    redex.postRedex = []
×
163
  }
164

165
  oneStep(): StepperVariableDeclaration | typeof undefinedNode {
166
    // Find the one that is not contractible.
167
    for (let i = 0; i < this.declarations.length; i++) {
90✔
168
      const ast = this.declarations[i]
90✔
169
      if (ast.isOneStepPossible()) {
90✔
170
        return new StepperVariableDeclaration(
90✔
171
          [
172
            this.declarations.slice(0, i),
173
            ast.oneStep() as StepperVariableDeclarator,
174
            this.declarations.slice(i + 1)
175
          ].flat(),
176
          this.kind,
177
          this.leadingComments,
178
          this.trailingComments,
179
          this.loc,
180
          this.range
181
        )
182
      }
183
    }
184

NEW
185
    return this
×
186
  }
187

188
  substitute(id: StepperPattern, value: StepperExpression): StepperBaseNode {
189
    return new StepperVariableDeclaration(
1,141✔
190
      this.declarations.map(
191
        declaration => declaration.substitute(id, value) as StepperVariableDeclarator
1,141✔
192
      ),
193
      this.kind,
194
      this.leadingComments,
195
      this.trailingComments,
196
      this.loc,
197
      this.range
198
    )
199
  }
200

201
  freeNames(): string[] {
202
    return Array.from(new Set(this.declarations.flatMap(ast => ast.freeNames())))
90✔
203
  }
204

205
  allNames(): string[] {
206
    return Array.from(new Set(this.declarations.flatMap(ast => ast.allNames())))
527✔
207
  }
208

209
  rename(before: string, after: string): StepperVariableDeclaration {
210
    return new StepperVariableDeclaration(
4✔
211
      this.declarations.map(
212
        declaration => declaration.rename(before, after) as StepperVariableDeclarator
4✔
213
      ),
214
      this.kind,
215
      this.leadingComments,
216
      this.trailingComments,
217
      this.loc,
218
      this.range
219
    )
220
  }
221
}
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