Skip to content

Commit

Permalink
Run eslint on tests (slevithan#211)
Browse files Browse the repository at this point in the history
Run eslint on tests, add .eslintignore and tests/.eslintrc.js

This helps the specs conform to the same style as the library,
with the exception of a handful of rules that are not enforced.
  • Loading branch information
josephfrazier authored and slevithan committed Jan 22, 2018
1 parent e13d1f7 commit bac1cb9
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 25 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
tests/perf/versions
tests/vendor
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"LICENSE"
],
"scripts": {
"lint": "eslint src",
"lint": "eslint src tests",
"babel": "babel src -d lib",
"prebuild": "npm run lint && npm run babel",
"build": "browserify lib/index.js --standalone XRegExp > xregexp-all.js",
Expand Down
28 changes: 28 additions & 0 deletions tests/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module.exports = {
"env": {
"jasmine": true
},
"globals": {
"XRegExp": true,
"disableOptInFeatures": true,
"REGEX_DATA": true,
"hasNativeU": true,
"hasNativeY": true,
"hasStrictMode": true,
"testUnicodeToken": true
},
"rules": {
"brace-style": 0,
"dot-location": 0,
"key-spacing": 0,
"no-control-regex": 0,
"no-empty-function": 0,
"no-loop-func": 0,
"no-multi-assign": 0,
"no-multi-spaces": 0,
"no-template-curly-in-string": 0,
"no-useless-call": 0,
"no-warning-comments": 0,
"object-property-newline": 0,
}
}
7 changes: 7 additions & 0 deletions tests/helpers/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
"rules": {
"global-require": 0,
"no-global-assign": 0,
"no-native-reassign": 0,
}
}
2 changes: 1 addition & 1 deletion tests/helpers/h-matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ global.addToEqualMatchMatcher = function() {
};
}
});
}
};
10 changes: 6 additions & 4 deletions tests/helpers/h-unicode.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
if (typeof global === 'undefined') {
global = window;
}

/*
* Runs a series of `expect` assertions, given a Unicode token name and arrays of code points that
* should or should not be matched.
*/
global.testUnicodeToken = function (name, options) {
global.testUnicodeToken = function(name, options) {
var pattern = '^\\p{' + name + '}$';
var negated = '^\\P{' + name + '}$';
var astralRegex = XRegExp(pattern, 'A');
Expand Down Expand Up @@ -45,7 +46,7 @@ global.testUnicodeToken = function (name, options) {
}
});
}
}
};


/*!
Expand All @@ -71,8 +72,9 @@ global.testUnicodeToken = function (name, options) {
* // Unlike String.fromCharCode, this correctly handles code points above 0xFFFF
*/
if (!String.fromCodePoint) {
String.fromCodePoint = function () {
var chars = [], point, offset, units, i;
String.fromCodePoint = function() {
var chars = [],
i, offset, point, units;
for (i = 0; i < arguments.length; ++i) {
point = arguments[i];
offset = point - 0x10000;
Expand Down
8 changes: 6 additions & 2 deletions tests/helpers/h.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ if (typeof global === 'undefined') {
// Ensure that all opt-in features are disabled when each spec starts
global.disableOptInFeatures = function() {
XRegExp.uninstall('namespacing astral');
}
};

// Property name used for extended regex instance data
global.REGEX_DATA = 'xregexp';
Expand All @@ -17,4 +17,8 @@ global.hasNativeU = XRegExp._hasNativeFlag('u');
// Check for ES6 `y` flag support
global.hasNativeY = XRegExp._hasNativeFlag('y');
// Check for strict mode support
global.hasStrictMode = (function() {'use strict'; return !this;}());
global.hasStrictMode = (function() {
'use strict';

return !this;
}());
5 changes: 5 additions & 0 deletions tests/perf/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
"globals": {
"Benchmark": true,
}
}
12 changes: 6 additions & 6 deletions tests/perf/perf.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@
}

var suiteOptions = {
onStart: function () {
onStart: function() {
log('\n' + this.name + ':');
},

onCycle: function (event) {
onCycle: function(event) {
log('\n' + String(event.target));
scrollToEnd();
},

onComplete: function () {
onComplete: function() {
log('\nFastest is ' + this.filter('fastest').map('name') + '\n');
// Remove current suite from queue
suites.shift();
Expand Down Expand Up @@ -148,7 +148,7 @@
}
}, benchmarkOptions)
.add('XRegExp.exec', function() {
var match = XRegExp.exec(strs[++i] || strs[i = 0], regexG, pos, 'sticky');
var match = XRegExp.exec(strs[++i] || strs[i = 0], regexG, pos, 'sticky'); // eslint-disable-line no-unused-vars
}, benchmarkOptions)
);
}());
Expand Down Expand Up @@ -192,7 +192,7 @@
(r.sticky ? 'y' : '')
);
}
while (match = r.exec(str)) {
while (match = r.exec(str)) { // eslint-disable-line no-cond-assign
matches.push(match[0]);
if (r.lastIndex === match.index) {
++r.lastIndex;
Expand All @@ -204,7 +204,7 @@
var matches = [];
var match;
var pos = 0;
while (match = XRegExp.exec(str, r, pos)) {
while (match = XRegExp.exec(str, r, pos)) { // eslint-disable-line no-cond-assign
matches.push(match[0]);
pos = match.index + (match[0].length || 1);
}
Expand Down
6 changes: 3 additions & 3 deletions tests/spec/s-addons-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('XRegExp.build addon:', function() {
describe('XRegExp.tag()', function() {

it('should escape the metacharacters of interpolated strings', function() {
var inner = '.html'
var inner = '.html';
var re = XRegExp.tag()`^index${inner}$`;

expect(re.test('index.html')).toBe(true);
Expand Down Expand Up @@ -82,9 +82,9 @@ describe('XRegExp.build addon:', function() {
it('should work as described in the comment @example', function() {
var h12 = /1[0-2]|0?[1-9]/;
var h24 = /2[0-3]|[01][0-9]/;
var hours = XRegExp.tag('x')`${h12} : | ${h24}`
var hours = XRegExp.tag('x')`${h12} : | ${h24}`;
var minutes = /^[0-5][0-9]$/;
var time = XRegExp.tag('x')`^ ${hours} (?<minutes>${minutes}) $`
var time = XRegExp.tag('x')`^ ${hours} (?<minutes>${minutes}) $`;

expect(time.test('10:59')).toBe(true);
expect(XRegExp.exec('10:59', time).minutes).toEqual('59');
Expand Down
14 changes: 8 additions & 6 deletions tests/spec/s-xregexp-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ describe('XRegExp.exec()', function() {
});

it('should avoid regression on edge cases', function() {

/*
* From the XRegExp 1.5.1 changelog:
* Fix: RegExp.prototype.exec no longer throws an error in IE when it is simultaneously
Expand Down Expand Up @@ -1123,6 +1124,7 @@ describe('XRegExp.matchChain()', function() {
});

it('should pass forward and return nonparticipating capturing groups as empty strings', function() {

/* Converting nonparticipating capturing groups that are passed forward in the chain
* (rather than returned) from undefineds to empty strings, rather than rejecting any match
* (even if zero-length) within them, is questionable behavior. However, this probably best
Expand Down Expand Up @@ -1158,12 +1160,12 @@ describe('XRegExp.replace()', function() {
*/

it('should pass the `groups` argument when `namespacing` is installed', function() {
XRegExp.install('namespacing')
XRegExp.install('namespacing');
var regex = XRegExp('(?s)(?<groupName>.)');
XRegExp.replace('test', regex, function (matched, capture1, position, S, groups) {
expect(groups).toEqual({groupName: 't'})
})
})
XRegExp.replace('test', regex, function(matched, capture1, position, S, groups) {
expect(groups).toEqual({groupName: 't'});
});
});

it('should perform replace-all for string search with scope "all"', function() {
expect(XRegExp.replace('test', 't', 'x', 'all')).toBe('xesx');
Expand Down Expand Up @@ -1324,7 +1326,7 @@ describe('XRegExp.split()', function() {
{str: '.', separator: /(.?)(.?)/, expected: ['', '.', '', '']},
{str: '.', separator: /(.??)(.??)/, expected: ['.']},
{str: '.', separator: /(.)?(.)?/, expected: ['', '.', undefined, '']},
{str: 'test', separator: /(.?)/, expected: ['','t','','e','','s','','t','']},
{str: 'test', separator: /(.?)/, expected: ['', 't', '', 'e', '', 's', '', 't', '']},
{str: 'tesst', separator: /(s)*/, expected: ['t', undefined, 'e', 's', 't']},
{str: 'tesst', separator: /(s)*?/, expected: ['t', undefined, 'e', undefined, 's', undefined, 's', undefined, 't']},
{str: 'tesst', separator: /(s*)/, expected: ['t', '', 'e', 'ss', 't']},
Expand Down
5 changes: 3 additions & 2 deletions tests/spec/s-xregexp.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ describe('XRegExp()', function() {
];
// XRegExp follows the native RegExp. Chrome 20 follows the spec and converts empty
// patterns to (?:), but Firefox 14.0.1, Safari 5.1.2, Opera 12, and IE 9 do not
var isEmpty = function(regex) {
function isEmpty(regex) {
return regex.source === '(?:)' || regex.source === '';
};
}

emptyTypes.forEach(function(item) {
expect(isEmpty(XRegExp(item))).toBe(true);
Expand Down Expand Up @@ -281,6 +281,7 @@ describe('XRegExp()', function() {
describe('fixes regex syntax cross-browser:', function() {

it('should use the correct JavaScript rules for empty character classes', function() {

/* Traditional regex behavior is that a leading, unescaped ] within a character class
* is treated as a literal character and does not end the character class. However,
* this is not true for ES3/5, which states that [] is an empty set that will never
Expand Down

0 comments on commit bac1cb9

Please sign in to comment.