Skip to content

Commit

Permalink
Complete test coverage for isUnquotedPropertyName
Browse files Browse the repository at this point in the history
  • Loading branch information
jskrzypek committed Jul 13, 2016
1 parent 7148687 commit 7e8b087
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 77 deletions.
3 changes: 1 addition & 2 deletions lib/isIdentifier.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 29 additions & 23 deletions lib/isUnquotedPropertyName.js
Expand Up @@ -35,8 +35,9 @@ var default_property_options = {
forbid_numeric_literals: false,
forbid_numeric_decimal: false,
allow_octal_literals: false,
test_propety_initilization: false,
test_property_initilization: false,
test_dot_access: false,
use_brackets_for_numerics: false,
object_mode: false
};

Expand All @@ -51,43 +52,48 @@ function isUnquotedPropertyName(value, options) {
var valueAsUnescapedString = identVal.valueAsUnescapedString;
var valueAsNumber = Number(value); // Number('010') returns 10, not 8 :'(

var isNumeric = !identVal.isIdentifierES6 && isNumericLiteral(value, valueAsNumber);
var isNumeric = !identVal.isValid && isNumericLiteral(value, valueAsNumber);
var numericInvalid = isNumeric && options.forbid_numeric_literals;
var isOctal = isNumeric && regexOctalLiteral.test(value);
var octalInvalid = isOctal && !options.allow_octal_literals;
var hasDecimal = isNumeric && regexDecimal.test(value);
var decimalInvalid = hasDecimal && options.forbid_numeric_decimal;

var needsQuotes = true;
if (identVal.isIdentifierES6) {
needsQuotes = !identVal.isValid;
} else if (isNumeric) {
needsQuotes = numericInvalid || octalInvalid || decimalInvalid;
}
var needsBrackets = !identVal.isIdentifierES6 || needsQuotes;
var isValid = !needsQuotes;

var propertyInitializationFailure = options.test_propety_initilization && function () {
var propertyInitializationFailure = void 0;
if (options.test_property_initilization) {
try {
/* eslint-disable no-new-func */
Function('{ ' + valueAsUnescapedString + ' : true };');
Function('({ ' + valueAsUnescapedString + ' : true })')();
/* eslint-enable no-new-func */
return false;
propertyInitializationFailure = false;
} catch (exception) {
return true;
propertyInitializationFailure = true;
}
}();
}

var dotAccessFailure = options.test_dot_access && function () {
var dotAccessFailure = void 0;
if (options.test_dot_access) {
try {
/* eslint-disable no-new-func */
Function('({ \'' + valueAsUnescapedString + '\' : true }).' + valueAsUnescapedString + ';');
/* eslint-enable no-new-func */
return false;
/* eslint-disable no-new-func, max-len */
Function('({ \'' + valueAsUnescapedString + '\' : true })' + (isNumeric && options.use_brackets_for_numerics ? '[' + valueAsUnescapedString + ']' : '.' + valueAsUnescapedString))();
/* eslint-enable no-new-func, max-len */
dotAccessFailure = false;
} catch (exception) {
return true;
dotAccessFailure = true;
}
}();
}

var needsQuotes = void 0; // = propertyInitializationFailure !== undefined && propertyInitializationFailure;
var needsBrackets = void 0; // = dotAccessFailure !== undefined && dotAccessFailure;
if (isNumeric) {
needsQuotes = numericInvalid || octalInvalid || decimalInvalid;
needsBrackets = true;
} else if (!identVal.isValid) {
needsQuotes = true;
needsBrackets = true;
}

var isValid = !needsQuotes && !propertyInitializationFailure && !dotAccessFailure;

return !options.object_mode ? isValid : {
value: value,
Expand Down
3 changes: 1 addition & 2 deletions src/lib/isIdentifier.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 29 additions & 23 deletions src/lib/isUnquotedPropertyName.js
Expand Up @@ -18,8 +18,9 @@ const default_property_options = {
forbid_numeric_literals: false,
forbid_numeric_decimal: false,
allow_octal_literals: false,
test_propety_initilization: false,
test_property_initilization: false,
test_dot_access: false,
use_brackets_for_numerics: false,
object_mode: false,
};

Expand All @@ -34,43 +35,48 @@ export default function isUnquotedPropertyName(value, options) {
let valueAsUnescapedString = identVal.valueAsUnescapedString;
let valueAsNumber = Number(value); // Number('010') returns 10, not 8 :'(

let isNumeric = !identVal.isIdentifierES6 && isNumericLiteral(value, valueAsNumber);
let isNumeric = !identVal.isValid && isNumericLiteral(value, valueAsNumber);
let numericInvalid = isNumeric && options.forbid_numeric_literals;
let isOctal = isNumeric && regexOctalLiteral.test(value);
let octalInvalid = isOctal && !options.allow_octal_literals;
let hasDecimal = isNumeric && regexDecimal.test(value);
let decimalInvalid = hasDecimal && options.forbid_numeric_decimal;

let needsQuotes = true;
if (identVal.isIdentifierES6) {
needsQuotes = !identVal.isValid;
} else if (isNumeric) {
needsQuotes = numericInvalid || octalInvalid || decimalInvalid;
}
let needsBrackets = !identVal.isIdentifierES6 || needsQuotes;
let isValid = !needsQuotes;

let propertyInitializationFailure = options.test_propety_initilization && (function () {
let propertyInitializationFailure;
if (options.test_property_initilization) {
try {
/* eslint-disable no-new-func */
Function(`{ ${valueAsUnescapedString} : true };`);
Function(`({ ${valueAsUnescapedString} : true })`)();
/* eslint-enable no-new-func */
return false;
propertyInitializationFailure = false;
} catch (exception) {
return true;
propertyInitializationFailure = true;
}
}());
}

let dotAccessFailure = options.test_dot_access && (function () {
let dotAccessFailure;
if (options.test_dot_access) {
try {
/* eslint-disable no-new-func */
Function(`({ '${valueAsUnescapedString}' : true }).${valueAsUnescapedString};`);
/* eslint-enable no-new-func */
return false;
/* eslint-disable no-new-func, max-len */
Function(`({ '${valueAsUnescapedString}' : true })${isNumeric && options.use_brackets_for_numerics ? `[${valueAsUnescapedString}]` : `.${valueAsUnescapedString}`}`)();
/* eslint-enable no-new-func, max-len */
dotAccessFailure = false;
} catch (exception) {
return true;
dotAccessFailure = true;
}
}());
}

let needsQuotes;// = propertyInitializationFailure !== undefined && propertyInitializationFailure;
let needsBrackets;// = dotAccessFailure !== undefined && dotAccessFailure;
if (isNumeric) {
needsQuotes = numericInvalid || octalInvalid || decimalInvalid;
needsBrackets = true;
} else if (!identVal.isValid) {
needsQuotes = true;
needsBrackets = true;
}

let isValid = !needsQuotes && !propertyInitializationFailure && !dotAccessFailure;

return !options.object_mode ? isValid : {
value,
Expand Down
28 changes: 28 additions & 0 deletions test/validators.js
Expand Up @@ -3339,4 +3339,32 @@ describe('Validators', function () {
valid: ['foo', 'ಠ_ಠ', '012', '13.4', '12', '5e23', '12e34', '\\u12e3'],
});
});

it('property initialization test should work (test_property_initilization)', function () {
test({
validator: 'isUnquotedPropertyName',
args: [{ test_property_initilization: true }],
valid: ['foo', 'ಠ_ಠ', '12', '13.4', '5e23', '12e34', '\\u12e3'],
invalid: ['a-1', '012'],
});
});

it('dot access test should work for identifiers (test_dot_access)', function () {
test({
validator: 'isUnquotedPropertyName',
args: [{ test_dot_access: true, use_brackets_for_numerics: false }],
valid: ['foo', 'ಠ_ಠ', '\\u12e3'],
invalid: ['a-1', '012', '12', '13.4', '5e23', '12e34'],
});
});

it('dot access test should use brackets with numbers (use_brackets_for_numerics)', function () {
test({
validator: 'isUnquotedPropertyName',
args: [{ test_dot_access: true, use_brackets_for_numerics: true }],
valid: ['foo', 'ಠ_ಠ', '12', '13.4', '5e23', '12e34', '\\u12e3'],
invalid: ['a-1', '012'],
});
});
});

55 changes: 30 additions & 25 deletions validator.js
Expand Up @@ -1075,7 +1075,6 @@
allow_new_unicode: false,
allow_immutable_global: false,
test_initilization: false,
use_let_not_var: false,
// Skip extra validation checks that ensure backwards compatibility with es3, es5, unicode 5.1.0
skip_extra_validation: false,
// Return an object of the various validation step results instead of just a boolean
Expand Down Expand Up @@ -1135,7 +1134,7 @@
var initializationFailure = options.test_initilization && function () {
try {
/* eslint-disable no-new-func */
Function((options.use_let_not_var ? 'let' : 'var') + ' ' + valueAsUnescapedString);
Function('var ' + valueAsUnescapedString);
/* eslint-enable no-new-func */
return false;
} catch (exception) {
Expand Down Expand Up @@ -1184,8 +1183,9 @@
forbid_numeric_literals: false,
forbid_numeric_decimal: false,
allow_octal_literals: false,
test_propety_initilization: false,
test_property_initilization: false,
test_dot_access: false,
use_brackets_for_numerics: false,
object_mode: false
};

Expand All @@ -1200,43 +1200,48 @@
var valueAsUnescapedString = identVal.valueAsUnescapedString;
var valueAsNumber = Number(value); // Number('010') returns 10, not 8 :'(

var isNumeric = !identVal.isIdentifierES6 && isNumericLiteral(value, valueAsNumber);
var isNumeric = !identVal.isValid && isNumericLiteral(value, valueAsNumber);
var numericInvalid = isNumeric && options.forbid_numeric_literals;
var isOctal = isNumeric && regexOctalLiteral.test(value);
var octalInvalid = isOctal && !options.allow_octal_literals;
var hasDecimal = isNumeric && regexDecimal.test(value);
var decimalInvalid = hasDecimal && options.forbid_numeric_decimal;

var needsQuotes = true;
if (identVal.isIdentifierES6) {
needsQuotes = !identVal.isValid;
} else if (isNumeric) {
needsQuotes = numericInvalid || octalInvalid || decimalInvalid;
}
var needsBrackets = !identVal.isIdentifierES6 || needsQuotes;
var isValid = !needsQuotes;

var propertyInitializationFailure = options.test_propety_initilization && function () {
var propertyInitializationFailure = void 0;
if (options.test_property_initilization) {
try {
/* eslint-disable no-new-func */
Function('{ ' + valueAsUnescapedString + ' : true };');
Function('({ ' + valueAsUnescapedString + ' : true })')();
/* eslint-enable no-new-func */
return false;
propertyInitializationFailure = false;
} catch (exception) {
return true;
propertyInitializationFailure = true;
}
}();
}

var dotAccessFailure = options.test_dot_access && function () {
var dotAccessFailure = void 0;
if (options.test_dot_access) {
try {
/* eslint-disable no-new-func */
Function('({ \'' + valueAsUnescapedString + '\' : true }).' + valueAsUnescapedString + ';');
/* eslint-enable no-new-func */
return false;
/* eslint-disable no-new-func, max-len */
Function('({ \'' + valueAsUnescapedString + '\' : true })' + (isNumeric && options.use_brackets_for_numerics ? '[' + valueAsUnescapedString + ']' : '.' + valueAsUnescapedString))();
/* eslint-enable no-new-func, max-len */
dotAccessFailure = false;
} catch (exception) {
return true;
dotAccessFailure = true;
}
}();
}

var needsQuotes = void 0; // = propertyInitializationFailure !== undefined && propertyInitializationFailure;
var needsBrackets = void 0; // = dotAccessFailure !== undefined && dotAccessFailure;
if (isNumeric) {
needsQuotes = numericInvalid || octalInvalid || decimalInvalid;
needsBrackets = true;
} else if (!identVal.isValid) {
needsQuotes = true;
needsBrackets = true;
}

var isValid = !needsQuotes && !propertyInitializationFailure && !dotAccessFailure;

return !options.object_mode ? isValid : {
value: value,
Expand Down
4 changes: 2 additions & 2 deletions validator.min.js

Large diffs are not rendered by default.

0 comments on commit 7e8b087

Please sign in to comment.