Skip to content

Commit 90471f3

Browse files
committed
Allow ignoreCase flag if all RegExps use it
1 parent 7295086 commit 90471f3

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

moo.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@
4141
return '(?:' + reEscape(obj) + ')'
4242

4343
} else if (isRegExp(obj)) {
44-
// TODO: consider /u support
45-
if (obj.ignoreCase) throw new Error('RegExp /i flag not allowed')
4644
if (obj.global) throw new Error('RegExp /g flag is implied')
4745
if (obj.sticky) throw new Error('RegExp /y flag is implied')
4846
if (obj.multiline) throw new Error('RegExp /m flag is implied')
@@ -154,6 +152,7 @@
154152
var fast = Object.create(null)
155153
var fastAllowed = true
156154
var unicodeFlag = null
155+
var ignoreCaseFlag = null
157156
var groups = []
158157
var parts = []
159158

@@ -210,7 +209,7 @@
210209

211210
groups.push(options)
212211

213-
// Check unicode flag is used everywhere or nowhere
212+
// Check unicode and ignoreCase flags are used everywhere or nowhere
214213
for (var j = 0; j < match.length; j++) {
215214
var obj = match[j]
216215
if (!isRegExp(obj)) {
@@ -222,6 +221,12 @@
222221
} else if (unicodeFlag !== obj.unicode) {
223222
throw new Error("If one rule is /u then all must be")
224223
}
224+
225+
if (ignoreCaseFlag === null) {
226+
ignoreCaseFlag = obj.ignoreCase
227+
} else if (ignoreCaseFlag !== obj.ignoreCase) {
228+
throw new Error("If one rule is /i then all must be")
229+
}
225230
}
226231

227232
// convert to RegExp
@@ -257,6 +262,7 @@
257262
var suffix = hasSticky || fallbackRule ? '' : '|'
258263

259264
if (unicodeFlag === true) flags += "u"
265+
if (ignoreCaseFlag === true) flags += "i"
260266
var combined = new RegExp(reUnion(parts) + suffix, flags)
261267
return {regexp: combined, groups: groups, fast: fast, error: errorRule || defaultErrorRule}
262268
}

test/test.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ describe('compiler', () => {
2828
expect(lex4.next()).toMatchObject({type: 'err', text: 'nope!'})
2929
})
3030

31-
test("warns for /g, /y, /i, /m", () => {
31+
test("warns for /g, /y, /m", () => {
3232
expect(() => compile({ word: /foo/ })).not.toThrow()
3333
expect(() => compile({ word: /foo/g })).toThrow('implied')
34-
expect(() => compile({ word: /foo/i })).toThrow('not allowed')
3534
expect(() => compile({ word: /foo/y })).toThrow('implied')
3635
expect(() => compile({ word: /foo/m })).toThrow('implied')
3736
})
@@ -1211,3 +1210,22 @@ describe("unicode flag", () => {
12111210
})
12121211

12131212
})
1213+
1214+
1215+
describe('ignoreCase flag', () => {
1216+
1217+
test("allows all rules to be /i", () => {
1218+
expect(() => compile({ a: /foo/i, b: /bar/i, c: "quxx" })).not.toThrow()
1219+
expect(() => compile({ a: /foo/i, b: /bar/, c: "quxx" })).toThrow("If one rule is /i then all must be")
1220+
expect(() => compile({ a: /foo/, b: /bar/i, c: "quxx" })).toThrow("If one rule is /i then all must be")
1221+
})
1222+
1223+
test("supports ignoreCase", () => {
1224+
const lexer = compile({ a: /foo/i, b: /bar/i, c: "quxx" })
1225+
lexer.reset("FoObArQuXx")
1226+
expect(lexer.next()).toMatchObject({value: "FoO"})
1227+
expect(lexer.next()).toMatchObject({value: "bAr"})
1228+
expect(lexer.next()).toMatchObject({value: "QuXx"})
1229+
})
1230+
1231+
})

0 commit comments

Comments
 (0)