Skip to content

Commit e8938fb

Browse files
committed
JS: introduce RegExpSequence::nextElement and previousElement
1 parent 8a9587f commit e8938fb

File tree

1 file changed

+23
-28
lines changed

1 file changed

+23
-28
lines changed

javascript/ql/src/semmle/javascript/Regexp.qll

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -76,40 +76,22 @@ class RegExpTerm extends Locatable, @regexpterm {
7676
/** Gets the regular expression term that is matched (textually) before this one, if any. */
7777
RegExpTerm getPredecessor() {
7878
exists(RegExpTerm parent | parent = getParent() |
79-
if parent instanceof RegExpSequence
80-
then
81-
exists(RegExpSequence seq, int i |
82-
seq = parent and
83-
seq.getChild(i) = this
84-
|
85-
seq.getChild(i - 1) = result
86-
or
87-
i = 0 and result = seq.getPredecessor()
88-
)
89-
else (
90-
not parent instanceof RegExpSubPattern and
91-
result = parent.getPredecessor()
92-
)
79+
result = parent.(RegExpSequence).previousElement(this)
80+
or
81+
not exists(parent.(RegExpSequence).previousElement(this)) and
82+
not parent instanceof RegExpSubPattern and
83+
result = parent.getPredecessor()
9384
)
9485
}
9586

9687
/** Gets the regular expression term that is matched (textually) after this one, if any. */
9788
RegExpTerm getSuccessor() {
9889
exists(RegExpTerm parent | parent = getParent() |
99-
if parent instanceof RegExpSequence
100-
then
101-
exists(RegExpSequence seq, int i |
102-
seq = parent and
103-
seq.getChild(i) = this
104-
|
105-
seq.getChild(i + 1) = result
106-
or
107-
i = seq.getNumChild() - 1 and result = seq.getSuccessor()
108-
)
109-
else (
110-
not parent instanceof RegExpSubPattern and
111-
result = parent.getSuccessor()
112-
)
90+
result = parent.(RegExpSequence).nextElement(this)
91+
or
92+
not exists(parent.(RegExpSequence).nextElement(this)) and
93+
not parent instanceof RegExpSubPattern and
94+
result = parent.getSuccessor()
11395
)
11496
}
11597

@@ -328,6 +310,19 @@ class RegExpSequence extends RegExpTerm, @regexp_seq {
328310
or
329311
result = getChild(i).getConstantValue() + getConstantValue(i+1)
330312
}
313+
314+
/** Gets the element preceding `element` in this sequence. */
315+
RegExpTerm previousElement(RegExpTerm element) {
316+
element = nextElement(result)
317+
}
318+
319+
/** Gets the element following `element` in this sequence. */
320+
RegExpTerm nextElement(RegExpTerm element) {
321+
exists(int i |
322+
element = this.getChild(i) and
323+
result = this.getChild(i + 1)
324+
)
325+
}
331326
}
332327

333328
/**

0 commit comments

Comments
 (0)