Skip to content

Commit

Permalink
Warn if type is a string
Browse files Browse the repository at this point in the history
  • Loading branch information
tjvr committed Sep 19, 2018
1 parent 9aa0c81 commit 90c5980
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
7 changes: 6 additions & 1 deletion moo.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,16 @@
if (options.keywords) {
// Warn if both keywords and type are set
if (options.type) {
throw new Error("Cannot have both keywords and type (for token '" + options.defaultType + "')")
throw new Error("Cannot have both keywords and type (for token '" + name + "')")
}
options.type = keywordTransform(options.keywords)
}

// type transform cannot be a string
if (typeof options.type === 'string') {
throw new Error("Type transform cannot be a string (type '" + options.type + "' for token '" + name + "')")
}

// convert to array
var match = options.match
options.match = Array.isArray(match) ? match : match ? [match] : []
Expand Down
8 changes: 8 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,14 @@ describe('type transforms', () => {
})).toThrow("Cannot have both keywords and type (for token 'identifier')")
})

test('cannot set type to a string', () => {
expect(() => compile({
identifier: {
type: 'moo',
},
})).toThrow("Type transform cannot be a string (type 'moo' for token 'identifier')")
})

})

describe('value transforms', () => {
Expand Down

0 comments on commit 90c5980

Please sign in to comment.