Skip to content

Commit

Permalink
rewrite tests to ES2017 syntax with minimalistic babel config, enable…
Browse files Browse the repository at this point in the history
… eslint on tests
  • Loading branch information
zloirock committed Nov 16, 2017
1 parent 7a40b65 commit b46fc8a
Show file tree
Hide file tree
Showing 410 changed files with 5,216 additions and 5,660 deletions.
3 changes: 0 additions & 3 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
node_modules/**
client/**
tests/helpers/**
tests/library/**
tests/tests/**
tests/bundles/**
!**/.eslintrc.js
41 changes: 38 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ module.exports = {
'tests/worker/**',
],
parserOptions: {
ecmaVersion: 6,
ecmaVersion: 2017,
},
env: {
es6: true,
Expand All @@ -312,7 +312,7 @@ module.exports = {
'tests/tests/**',
],
parserOptions: {
ecmaVersion: 6,
ecmaVersion: 2017,
sourceType: 'module',
},
env: {
Expand All @@ -321,10 +321,23 @@ module.exports = {
rules: {
// enforces return statements in callbacks of array's methods
'array-callback-return': 'off',
// require parentheses around arrow function arguments
'arrow-parens': ['error', 'as-needed'],
// enforce consistent spacing before and after the arrow in arrow functions
'arrow-spacing': 'error',
// encourages use of dot notation whenever possible
'dot-notation': ['error', { allowKeywords: true }],
// specify the maximum length of a line in your program
'max-len': ['error', 140, 2],
'max-len': ['error', 150, 2],
// specify the maximum number of statement allowed in a function
'max-statements': 'off',
// disallow duplicate module imports
'no-duplicate-imports': 'error',
'no-inner-declarations': 'off',
// disallow unnecessary computed property keys in object literals
'no-useless-computed-key': 'error',
// require let or const instead of var
'no-var': 'error',
// disallow function declarations and expressions inside loop statements
'no-loop-func': 'off',
// disallow use of new operator when not part of the assignment or comparison
Expand All @@ -337,6 +350,28 @@ module.exports = {
'no-unused-expressions': 'off',
// disallow unnecessary calls to `.call()` and `.apply()`
'no-useless-call': 'off',
// disallow unnecessary constructors
'no-useless-constructor': 'error',
// disallow renaming import, export, and destructured assignments to the same name
'no-useless-rename': 'error',
// require or disallow method and property shorthand syntax for object literals
'object-shorthand': 'error',
// require using arrow functions for callbacks
'prefer-arrow-callback': 'error',
// require const declarations for variables that are never reassigned after declared
'prefer-const': 'error',
// require destructuring from arrays and/or objects
'prefer-destructuring': 'error',
// require template literals instead of string concatenation
'prefer-template': 'error',
// require or disallow use of quotes around object literal property names
'quote-props': ['error', 'as-needed', { keywords: false }],
// enforce spacing between rest and spread operators and their expressions
'rest-spread-spacing': 'error',
// require or disallow spacing around embedded expressions of template strings
'template-curly-spacing': ['error', 'always'],
// enforce sorted import declarations within modules
'sort-imports': 'error',
},
},
],
Expand Down
18 changes: 16 additions & 2 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,26 @@ module.exports = grunt => {
test: /\.js$/,
loader: 'babel-loader',
options: {
// use transforms which does not use es5+ builtins
plugins: [
// use it instead of webpack es modules for support engines without descriptors
['transform-es2015-modules-commonjs', { loose: true }],
['transform-es3-member-expression-literals'],
['transform-es3-property-literals'],
// use it instead of webpack es modules for support engines without descriptors
['transform-es2015-modules-commonjs', { loose: true }],
['transform-es2015-arrow-functions'],
['transform-es2015-block-scoped-functions'],
['transform-es2015-block-scoping'],
['transform-es2015-classes', { loose: true }],
['transform-es2015-computed-properties', { loose: true }],
['transform-es2015-destructuring', { loose: true }],
['transform-es2015-literals'],
['transform-es2015-parameters'],
['transform-es2015-shorthand-properties'],
['transform-es2015-spread', { loose: true }],
['transform-es2015-template-literals', { loose: true, spec: true }],
['transform-exponentiation-operator'],
['transform-for-of-as-array'],
['check-es2015-constants'],
],
},
}],
Expand Down
13 changes: 13 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,23 @@
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-plugin-check-es2015-constants": "^6.22.0",
"babel-plugin-transform-es2015-arrow-functions": "^6.22.0",
"babel-plugin-transform-es2015-block-scoped-functions": "^6.22.0",
"babel-plugin-transform-es2015-block-scoping": "^6.26.0",
"babel-plugin-transform-es2015-classes": "^6.24.1",
"babel-plugin-transform-es2015-computed-properties": "^6.24.1",
"babel-plugin-transform-es2015-destructuring": "^6.23.0",
"babel-plugin-transform-es2015-literals": "^6.22.0",
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.0",
"babel-plugin-transform-es2015-parameters": "^6.24.1",
"babel-plugin-transform-es2015-shorthand-properties": "^6.24.1",
"babel-plugin-transform-es2015-spread": "^6.22.0",
"babel-plugin-transform-es2015-template-literals": "^6.22.0",
"babel-plugin-transform-es3-member-expression-literals": "^6.22.0",
"babel-plugin-transform-es3-property-literals": "^6.22.0",
"babel-plugin-transform-exponentiation-operator": "^6.24.1",
"babel-plugin-transform-for-of-as-array": "^1.0.4",
"es-observable-tests": "0.2.x",
"eslint": "4.11.x",
"eslint-plugin-import": "2.8.x",
Expand Down
24 changes: 11 additions & 13 deletions tests/helpers/constants.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
'use strict';

export var DESCRIPTORS = !!function () {
export const DESCRIPTORS = !!(() => {
try {
return Object.defineProperty({}, 'a', {
get: function () {
get() {
return 7;
}
}).a === 7;
} catch (e) { /* empty */ }
}();
})();

export var GLOBAL = Function('return this')();
export const GLOBAL = Function('return this')();

export var NATIVE = GLOBAL.NATIVE || false;
export const NATIVE = GLOBAL.NATIVE || false;

export var TYPED_ARRAYS = {
export const TYPED_ARRAYS = {
Float32Array: 4,
Float64Array: 8,
Int8Array: 1,
Expand All @@ -26,16 +24,16 @@ export var TYPED_ARRAYS = {
Uint8ClampedArray: 1
};

export var LITTLE_ENDIAN = function () {
export const LITTLE_ENDIAN = (() => {
try {
return new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
return new GLOBAL.Uint8Array(new GLOBAL.Uint16Array([1]).buffer)[0] === 1;
} catch (e) {
return true;
}
}();
})();

export var PROTO = !!Object.setPrototypeOf || '__proto__' in Object.prototype;
export const PROTO = !!Object.setPrototypeOf || '__proto__' in Object.prototype;

export var STRICT = !function () {
export const STRICT = !function () {
return this;
}();
77 changes: 44 additions & 33 deletions tests/helpers/helpers.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,80 @@
import { GLOBAL } from './constants';

var Promise = GLOBAL.core ? core.Promise : GLOBAL.Promise;
var ArrayBuffer = GLOBAL.core ? core.ArrayBuffer : GLOBAL.ArrayBuffer;
var DataView = GLOBAL.core ? core.DataView : GLOBAL.DataView;
const { core } = GLOBAL;

const Promise = core ? core.Promise : GLOBAL.Promise;
const ArrayBuffer = core ? core.ArrayBuffer : GLOBAL.ArrayBuffer;
const DataView = core ? core.DataView : GLOBAL.DataView;

const ITERATOR = core ? core.Symbol.iterator : GLOBAL.Symbol && GLOBAL.Symbol.iterator;

export function arrayToBuffer(it) {
var buffer = new ArrayBuffer(it.length);
var view = new DataView(buffer);
for (var i = 0, length = it.length; i < length; ++i) {
const buffer = new ArrayBuffer(it.length);
const view = new DataView(buffer);
for (let i = 0, { length } = it; i < length; ++i) {
view.setUint8(i, it[i]);
}
return buffer;
}

export function bufferToArray(it) {
var results = [];
var view = new DataView(it);
for (var i = 0, byteLength = view.byteLength; i < byteLength; ++i) {
const results = [];
const view = new DataView(it);
for (let i = 0, { byteLength } = view; i < byteLength; ++i) {
results.push(view.getUint8(i));
}
return results;
}

export function createIterable(elements, methods) {
var iterable = {
const iterable = {
called: false,
received: false
};
iterable[GLOBAL.core ? core.Symbol.iterator : GLOBAL.Symbol && Symbol.iterator] = function () {
iterable.received = true;
var index = 0;
var iterator = {
next: function () {
iterable.called = true;
return {
value: elements[index++],
done: index > elements.length
};
}
};
if (methods) for (var key in methods) iterator[key] = methods[key];
return iterator;
received: false,
[ITERATOR]() {
iterable.received = true;
let index = 0;
const iterator = {
next() {
iterable.called = true;
return {
value: elements[index++],
done: index > elements.length
};
}
};
if (methods) for (const key in methods) iterator[key] = methods[key];
return iterator;
}
};
return iterable;
}

export function includes(target, element) {
for (var i = 0, length = target.length; i < length; ++i) if (target[i] === element) return true;
export function includes(target, wanted) {
for (const element of target) if (wanted === element) return true;
return false;
}

export function is(a, b) {
// eslint-disable-next-line no-self-compare
return a === b ? a !== 0 || 1 / a === 1 / b : a != a && b != b;
}

export var nativeSubclass = function () {
export const nativeSubclass = (() => {
try {
return Function("'use strict';class O extends Object {};return new O instanceof O;")()
&& Function('F', "'use strict';return class extends F {};");
if (Function(`
'use strict';
class Subclass extends Object { /* empty */ };
return new Subclass() instanceof Subclass;
`)()) return Function('Parent', `
'use strict';
return class extends Parent { /* empty */ };
`);
} catch (e) { /* empty */ }
}();
})();

export function timeLimitedPromise(time, fn) {
return Promise.race([
new Promise(fn), new Promise(function (resolve, reject) {
new Promise(fn), new Promise((resolve, reject) => {
setTimeout(reject, time);
})
]);
Expand Down
24 changes: 12 additions & 12 deletions tests/helpers/qunit-helpers.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
import { GLOBAL, DESCRIPTORS } from './constants';
import { DESCRIPTORS, GLOBAL } from './constants';
import { is } from './helpers';

var toString = Object.prototype.toString;
var propertyIsEnumerable = Object.prototype.propertyIsEnumerable;
const { toString, propertyIsEnumerable } = Object.prototype.propertyIsEnumerable;

QUnit.assert.arity = function (fn, length, message) {
this.pushResult({
result: fn.length === length,
actual: fn.length,
expected: length,
message: message || 'arity is ' + length
message: message || `arity is ${ length }`
});
};

QUnit.assert.arrayEqual = function (a, b, message) {
var result = true;
let result = true;
if (a.length !== b.length) {
result = false;
} else {
for (var i = 0, length = a.length; i < length; ++i) {
for (let i = 0, { length } = a; i < length; ++i) {
if (!is(a[i], b[i])) {
result = false;
break;
}
}
}
this.pushResult({
result: result,
result,
actual: [].slice.call(a),
expected: [].slice.call(b),
message: message
message
});
};

Expand All @@ -38,7 +37,7 @@ QUnit.assert.epsilon = function (a, b, E, message) {
result: Math.abs(a - b) <= (E != null ? E : 1e-11),
actual: a,
expected: b,
message: message
message
});
};

Expand All @@ -53,6 +52,7 @@ QUnit.assert.isFunction = function (fn, message) {

QUnit.assert.isIterable = function (it, message) {
this.pushResult({
// eslint-disable-next-line no-undef
result: GLOBAL.core && core.isIterable ? core.isIterable(it) : !!it[GLOBAL.Symbol && Symbol.iterator],
actual: false,
expected: true,
Expand Down Expand Up @@ -83,7 +83,7 @@ QUnit.assert.name = function (fn, name, message) {
result: fn.name === name,
actual: fn.name,
expected: name,
message: message || "name is '" + name + "'"
message: message || `name is '${ name }'`
});
};

Expand All @@ -92,7 +92,7 @@ QUnit.assert.nonEnumerable = function (O, key, message) {
result: !propertyIsEnumerable.call(O, key),
actual: false,
expected: true,
message: message || (typeof key === 'symbol' ? 'method' : key) + ' is non-enumerable'
message: message || `${ typeof key === 'symbol' ? 'method' : key } is non-enumerable`
});
};

Expand All @@ -101,6 +101,6 @@ QUnit.assert.same = function (a, b, message) {
result: is(a, b),
actual: a,
expected: b,
message: message
message
});
};
8 changes: 4 additions & 4 deletions tests/library/core.get-iterator-method.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { createIterable } from '../helpers/helpers';

QUnit.test('core.getIteratorMethod', function (assert) {
var getIteratorMethod = core.getIteratorMethod;
QUnit.test('core.getIteratorMethod', assert => {
const { getIteratorMethod } = core;
assert.isFunction(getIteratorMethod);
var iterable = createIterable([]);
var iterFn = getIteratorMethod(iterable);
const iterable = createIterable([]);
const iterFn = getIteratorMethod(iterable);
assert.isFunction(iterFn);
assert.isIterator(iterFn.call(iterable));
assert.isFunction(getIteratorMethod([]));
Expand Down
Loading

0 comments on commit b46fc8a

Please sign in to comment.