cucumber-expressions
Advanced tools
Comparing version 6.0.0 to 6.0.1
@@ -9,2 +9,3 @@ 'use strict'; | ||
var TreeRegexp = require('./tree_regexp'); | ||
var ParameterType = require('./parameter_type'); | ||
@@ -127,2 +128,3 @@ var _require = require('./errors'), | ||
var typeName = p2; | ||
ParameterType.checkParameterTypeName(typeName); | ||
var parameterType = parameterTypeRegistry.lookupByTypeName(typeName); | ||
@@ -129,0 +131,0 @@ if (!parameterType) throw new UndefinedParameterTypeError(typeName); |
@@ -26,4 +26,8 @@ 'use strict'; | ||
this.defineParameterType(new ParameterType('int', INTEGER_REGEXPS, Number, parseInt, true, true)); | ||
this.defineParameterType(new ParameterType('float', FLOAT_REGEXP, Number, parseFloat, true, false)); | ||
this.defineParameterType(new ParameterType('int', INTEGER_REGEXPS, Number, function (s) { | ||
return s && parseInt(s); | ||
}, true, true)); | ||
this.defineParameterType(new ParameterType('float', FLOAT_REGEXP, Number, function (s) { | ||
return s && parseFloat(s); | ||
}, true, false)); | ||
this.defineParameterType(new ParameterType('word', WORD_REGEXP, String, function (s) { | ||
@@ -59,4 +63,6 @@ return s; | ||
value: function defineParameterType(parameterType) { | ||
if (this._parameterTypeByName.has(parameterType.name)) throw new Error('There is already a parameter type with name ' + parameterType.name); | ||
this._parameterTypeByName.set(parameterType.name, parameterType); | ||
if (parameterType.name) { | ||
if (this._parameterTypeByName.has(parameterType.name)) throw new Error('There is already a parameter type with name ' + parameterType.name); | ||
this._parameterTypeByName.set(parameterType.name, parameterType); | ||
} | ||
@@ -63,0 +69,0 @@ var _iteratorNormalCompletion = true; |
@@ -10,2 +10,8 @@ 'use strict'; | ||
var ILLEGAL_PARAMETER_NAME_PATTERN = /([[\]()$.|?*+])/; | ||
var UNESCAPE_PATTERN = function UNESCAPE_PATTERN() { | ||
return (/(\\([[$.|?*+\]]))/g | ||
); | ||
}; | ||
var ParameterType = function () { | ||
@@ -19,2 +25,9 @@ _createClass(ParameterType, null, [{ | ||
} | ||
}, { | ||
key: 'checkParameterTypeName', | ||
value: function checkParameterTypeName(typeName) { | ||
var unescapedTypeName = typeName.replace(UNESCAPE_PATTERN(), '$2'); | ||
var match = unescapedTypeName.match(ILLEGAL_PARAMETER_NAME_PATTERN); | ||
if (match) throw new CucumberExpressionError('Illegal character \'' + match[1] + '\' in parameter name {' + unescapedTypeName + '}'); | ||
} | ||
@@ -40,2 +53,5 @@ /** | ||
if (preferForRegexpMatch === undefined) preferForRegexpMatch = false; | ||
if (name) ParameterType.checkParameterTypeName(name); | ||
this._name = name; | ||
@@ -52,3 +68,3 @@ this._regexps = stringArray(regexps); | ||
value: function transform(thisObj, groupValues) { | ||
return groupValues.length === 0 ? null : this._transform.apply(thisObj, groupValues); | ||
return this._transform.apply(thisObj, groupValues); | ||
} | ||
@@ -55,0 +71,0 @@ }, { |
@@ -28,3 +28,3 @@ 'use strict'; | ||
return _this._parameterTypeRegistry.lookupByRegexp(parameterTypeRegexp, _this._treeRegexp, text) || new ParameterType(parameterTypeRegexp, parameterTypeRegexp, String, function (s) { | ||
return _this._parameterTypeRegistry.lookupByRegexp(parameterTypeRegexp, _this._treeRegexp, text) || new ParameterType(null, parameterTypeRegexp, String, function (s) { | ||
return s; | ||
@@ -31,0 +31,0 @@ }, false, false); |
{ | ||
"name": "cucumber-expressions", | ||
"version": "6.0.0", | ||
"version": "6.0.1", | ||
"description": "Cucumber Expressions - a simpler alternative to Regular Expressions", | ||
@@ -5,0 +5,0 @@ "main": "dist/src/index.js", |
const Argument = require('./argument') | ||
const TreeRegexp = require('./tree_regexp') | ||
const ParameterType = require('./parameter_type') | ||
const { | ||
@@ -80,2 +81,3 @@ UndefinedParameterTypeError, | ||
const typeName = p2 | ||
ParameterType.checkParameterTypeName(typeName) | ||
const parameterType = parameterTypeRegistry.lookupByTypeName(typeName) | ||
@@ -82,0 +84,0 @@ if (!parameterType) throw new UndefinedParameterTypeError(typeName) |
@@ -19,6 +19,20 @@ const ParameterType = require('./parameter_type') | ||
this.defineParameterType( | ||
new ParameterType('int', INTEGER_REGEXPS, Number, parseInt, true, true) | ||
new ParameterType( | ||
'int', | ||
INTEGER_REGEXPS, | ||
Number, | ||
s => s && parseInt(s), | ||
true, | ||
true | ||
) | ||
) | ||
this.defineParameterType( | ||
new ParameterType('float', FLOAT_REGEXP, Number, parseFloat, true, false) | ||
new ParameterType( | ||
'float', | ||
FLOAT_REGEXP, | ||
Number, | ||
s => s && parseFloat(s), | ||
true, | ||
false | ||
) | ||
) | ||
@@ -69,7 +83,9 @@ this.defineParameterType( | ||
defineParameterType(parameterType) { | ||
if (this._parameterTypeByName.has(parameterType.name)) | ||
throw new Error( | ||
`There is already a parameter type with name ${parameterType.name}` | ||
) | ||
this._parameterTypeByName.set(parameterType.name, parameterType) | ||
if (parameterType.name) { | ||
if (this._parameterTypeByName.has(parameterType.name)) | ||
throw new Error( | ||
`There is already a parameter type with name ${parameterType.name}` | ||
) | ||
this._parameterTypeByName.set(parameterType.name, parameterType) | ||
} | ||
@@ -76,0 +92,0 @@ for (const parameterTypeRegexp of parameterType.regexps) { |
const { CucumberExpressionError } = require('./errors') | ||
const ILLEGAL_PARAMETER_NAME_PATTERN = /([[\]()$.|?*+])/ | ||
const UNESCAPE_PATTERN = () => /(\\([[$.|?*+\]]))/g | ||
class ParameterType { | ||
@@ -10,2 +13,13 @@ static compare(pt1, pt2) { | ||
static checkParameterTypeName(typeName) { | ||
const unescapedTypeName = typeName.replace(UNESCAPE_PATTERN(), '$2') | ||
const match = unescapedTypeName.match(ILLEGAL_PARAMETER_NAME_PATTERN) | ||
if (match) | ||
throw new CucumberExpressionError( | ||
`Illegal character '${ | ||
match[1] | ||
}' in parameter name {${unescapedTypeName}}` | ||
) | ||
} | ||
/** | ||
@@ -30,2 +44,5 @@ * @param name {String} the name of the type | ||
if (preferForRegexpMatch === undefined) preferForRegexpMatch = false | ||
if (name) ParameterType.checkParameterTypeName(name) | ||
this._name = name | ||
@@ -60,5 +77,3 @@ this._regexps = stringArray(regexps) | ||
transform(thisObj, groupValues) { | ||
return groupValues.length === 0 | ||
? null | ||
: this._transform.apply(thisObj, groupValues) | ||
return this._transform.apply(thisObj, groupValues) | ||
} | ||
@@ -65,0 +80,0 @@ } |
@@ -24,4 +24,4 @@ const Argument = require('./argument') | ||
new ParameterType( | ||
null, | ||
parameterTypeRegexp, | ||
parameterTypeRegexp, | ||
String, | ||
@@ -28,0 +28,0 @@ s => s, |
@@ -163,2 +163,16 @@ /* eslint-env mocha */ | ||
for (const c of '[]()$.|?*+'.split('')) { | ||
it(`does not allow parameter type with ${c}`, () => { | ||
try { | ||
match(`{${c}string}`, 'something') | ||
assert.fail() | ||
} catch (expected) { | ||
assert.equal( | ||
expected.message, | ||
`Illegal character '${c}' in parameter name {${c}string}` | ||
) | ||
} | ||
}) | ||
} | ||
it('exposes source', () => { | ||
@@ -165,0 +179,0 @@ const expr = 'I have {int} cuke(s)' |
@@ -48,2 +48,9 @@ /* eslint-env mocha */ | ||
describe('CucumberExpression', () => { | ||
it('throws exception for illegal character in parameter name', () => { | ||
assertThrows( | ||
() => new ParameterType('[string]', /.*/, String, s => s, false, true), | ||
"Illegal character '[' in parameter name {[string]}" | ||
) | ||
}) | ||
it('matches parameters with custom parameter type', () => { | ||
@@ -50,0 +57,0 @@ const expression = new CucumberExpression( |
Sorry, the diff of this file is not supported yet
221338
2838