This commit is contained in:
chrosey
2017-09-13 07:52:34 +02:00
parent a1f16c37f4
commit 2340b0226b
24621 changed files with 2912161 additions and 149 deletions
+56
View File
@@ -0,0 +1,56 @@
'use strict';
/*jshint asi: true*/
var test = require('tap').test
, util = require('util')
, redeyed = require('..')
function inspect (obj) {
return util.inspect(obj, false, 5, true)
}
test('adding custom asserts ... ', function (t) {
t.constructor.prototype.assertSurrounds = function (code, opts, expected) {
var result = redeyed(code, opts).code
this.equals(result, expected, inspect(code) + ' => ' + inspect(expected))
return this;
}
t.end()
})
test('\nbefore/after config, keywords', function (t) {
var opts001 = { Keyword: { _default: { _before: '*', _after: '&' } } };
t.test('\n# ' + inspect(opts001), function (t) {
t.assertSurrounds('this', opts001, '*this&')
t.assertSurrounds('if (a == 1) return', opts001, '*if& (a == 1) *return&')
t.assertSurrounds('var n = new Test();', opts001, '*var& n = *new& Test();')
t.end()
})
var opts002 = {
Keyword: {
'function': { _before: '^' }
, 'return': { _before: '(', _after: ')' }
, _default: { _before: '*' , _after: '&' }
}
};
t.test('\n# ' + inspect(opts002), function (t) {
t.assertSurrounds(
[ 'function foo (bar) {'
, ' var a = 3;'
, ' return bar + a;'
, '}'
].join('\n')
, opts002
, [ '^function& foo (bar) {'
, ' *var& a = 3;'
, ' (return) bar + a;'
, '}'
].join('\n'))
t.end()
})
t.end()
})
+69
View File
@@ -0,0 +1,69 @@
'use strict'
/*jshint asi: true, browser: true*/
/*global define window */
var test = require('tap').test
, util = require('util')
, redeyedExport = require('..')
, redeyedkey = require.resolve('..')
, esprima = require('esprima')
function setup() {
// remove redeyed from require cache to force re-require for each test
delete require.cache[redeyedkey];
// remove globals
delete global.window;
delete global.define;
}
// TODO: need to run in vm in order to properly simulate require and module not being present
return;
test('define and window exist', function (t) {
var defineCb
, deps
setup()
// declare browser globals
global.window = { }
global.define = function (deps_, cb) {
deps_ = deps
defineCb = cb
}
define.amd = true
var redeyed = require('..')
, definedredeyed = defineCb(esprima)
t.equal(window.redeyed, undefined, 'redeyed is not attached to window')
t.notEqual(redeyed.toString(), redeyedExport.toString(), 'redeyed is not exported')
t.equal(definedredeyed.toString(), redeyedExport.toString(), 'redeyed is defined')
t.end()
})
test('window exists, but define doesn\'t', function (t) {
setup()
// declare browser globals
global.window = { esprima: esprima }
var redeyed = require('..')
t.equal(window.redeyed.toString(), redeyedExport.toString(), 'redeyed is attached to window')
t.notEqual(redeyed.toString(), redeyedExport.toString(), 'redeyed is not exported')
t.end()
})
test('neither window nor define exist', function (t) {
setup()
var redeyed = require('..')
t.equal(redeyed.toString(), redeyedExport.toString(), 'redeyed is exported')
t.end()
})
+75
View File
@@ -0,0 +1,75 @@
'use strict';
/*jshint asi: true*/
var test = require('tap').test
, util = require('util')
, redeyed = require('..')
function inspect (obj) {
return util.inspect(obj, false, 5, true)
}
test('adding custom asserts ... ', function (t) {
t.constructor.prototype.assertSurrounds = function (code, opts, expected) {
var result = redeyed(code, opts)
this.equals(result.code, expected, inspect(code) + ' => ' + inspect(expected))
return this;
}
t.end()
})
test('\nstring config, Line comments', function (t) {
var opts = { Line: { _default: '*:&' } };
t.test('\n# ' + inspect(opts), function (t) {
t.assertSurrounds(
'// a comment'
, opts
, '*// a comment&'
)
t.assertSurrounds(
'// comment then new line\nif (a == 1) return'
, opts
, '*// comment then new line&\nif (a == 1) return'
)
t.assertSurrounds(
'var n = new Test();// some comment after\n//more comment\nvar s = 3;'
, opts
, 'var n = new Test();*// some comment after&\n*//more comment&\nvar s = 3;'
)
t.end()
})
t.end()
})
test('\nstring config, Block comments', function (t) {
var opts = { Block: { _default: '_:-' } };
t.test('\n# ' + inspect(opts), function (t) {
t.assertSurrounds(
'/* a comment */'
, opts
, '_/* a comment */-'
)
t.assertSurrounds(
'/* comment then new line*/\nif (a == 1) /* inline */ return'
, opts
, '_/* comment then new line*/-\nif (a == 1) _/* inline */- return'
)
t.assertSurrounds(
'var n = new Test();/* some comment after*/\n/*more comment*/\nvar s = 3;'
, opts
, 'var n = new Test();_/* some comment after*/-\n_/*more comment*/-\nvar s = 3;'
)
t.assertSurrounds(
'var a = 4;\n/* Multi line comment\n * Next line\n * and another\n*/ var morecode = "here";'
, opts
, 'var a = 4;\n_/* Multi line comment\n * Next line\n * and another\n*/- var morecode = "here";'
)
t.end()
})
t.end()
})
+60
View File
@@ -0,0 +1,60 @@
'use strict';
/*jshint asi: true*/
var test = require('tap').test
, util = require('util')
, redeyed = require('..')
function inspect (obj) {
return util.inspect(obj, false, 5, true)
}
test('adding custom asserts ... ', function (t) {
t.constructor.prototype.assertSurrounds = function (code, opts, expected) {
var optsi = inspect(opts);
var result = redeyed(code, opts).code
this.equals( result
, expected
, util.format('%s: %s => %s', optsi, inspect(code), inspect(expected))
)
return this;
}
t.end()
})
test('\n undefineds only', function (t) {
t.assertSurrounds('1 + 2', { Numeric: { _default: undefined } }, '1 + 2')
t.assertSurrounds('1 + 2', { Numeric: { _default: undefined }, _default: undefined }, '1 + 2')
t.assertSurrounds(
'return true'
, { 'Boolean': { 'true': undefined, 'false': undefined, _default: undefined } , _default: undefined }
, 'return true'
)
t.end()
})
test('\n mixed', function (t) {
t.assertSurrounds(
'return true || false'
, { 'Boolean': { 'true': '&:', 'false': undefined, _default: undefined } , _default: undefined }
, 'return &true || false'
)
t.assertSurrounds(
'return true || false'
, { 'Boolean': { 'true': '&:', 'false': undefined, _default: ':?' } , _default: undefined }
, 'return &true? || false?'
)
t.assertSurrounds(
'return true || false'
, { 'Boolean': { 'true': '&:', 'false': undefined, _default: undefined } , _default: ':?' }
, 'return &true? || false'
)
t.end()
})
+58
View File
@@ -0,0 +1,58 @@
'use strict';
/*jshint asi: true*/
var test = require('tap').test
, util = require('util')
, redeyed = require('..')
function inspect (obj) {
return util.inspect(obj, false, 5, true)
}
test('function - config passing idx and tokens', function (t) {
var args = []
, opts001 = {
Boolean: {
_default: identity
}
, Keyword: {
_default: identity
}
, Identifier: {
_default: identity
}
, Punctuator: {
_default: identity
}
}
, code = 'var fn = function () { return true; }'
function identity (s, info) {
args.push( { value: s, idx: info.tokenIndex, tokens: info.tokens, code: info.code })
// returning unchanged string will keep the splits be equal to the original tokens
return s
}
function tokenValue (t) { return t.value; }
t.test(inspect(opts001) + ' -- ' + code, function (t) {
var result = redeyed(code, opts001, { splits: true })
, tokens = result.tokens
t.equals(args.length, tokens.length, 'called with all tokens')
for (var i = 0; i < tokens.length; i++) {
var token = tokens[i]
, arg = args[i]
t.equals(arg.value, token.value, 'passes correct value: ' + inspect([ arg.value, token.value ]))
t.equals(arg.idx, i, 'passes correct index')
t.equals(arg.code, code, 'passes code')
t.deepEquals(arg.tokens, tokens, 'passes all tokens')
}
t.end()
})
t.end()
})
+89
View File
@@ -0,0 +1,89 @@
'use strict';
/*jshint asi: true*/
var test = require('tap').test
, util = require('util')
, redeyed = require('..')
function inspect (obj) {
return util.inspect(obj, false, 5, true)
}
test('given i skip 2 more tokens after each semicolon', function (t) {
var calls = 0
, opts = {
Punctuator: {
';': function identity (s, info) {
// tell it to skip past second to last token that is 2 ahead of the current one
calls++
var skipToken = info.tokens[info.tokenIndex + 2]
return skipToken ? { replacement: s, skipPastToken: skipToken } : s;
}
}
}
;
[ { code: ';;;' , expectedCalls: 1 }
, { code: ';;;;' , expectedCalls: 2 }
, { code: '; ; ; ;' , expectedCalls: 2 }
, { code: ';;; ;;; ;;; ;' , expectedCalls: 4 }
, { code: ';;; ;;; ;;; ;;; ;' , expectedCalls: 5 }
, { code: ';;; ;;; ;;; ;;; ;;;' , expectedCalls: 5 }
, { code: ';;; ;;; ;;; ;;; ;;; ;' , expectedCalls: 6 }
].forEach(function (x) {
calls = 0
redeyed(x.code, opts);
t.equals(calls, x.expectedCalls, 'calls ' + x.expectedCalls + ' times for ' + x.code)
});
t.end()
})
test('replace log', function (t) {
var kinds = [ 'info', 'warn', 'error' ]
, opts = {
Identifier: {
console: function replaceLog(s, info) {
var code = info.code
, idx = info.tokenIndex
, tokens = info.tokens
, kind = tokens[idx + 2].value
, openParen = tokens[idx + 3].value
, firstArgTkn = tokens[idx + 4]
, argIdx = idx + 3
, open
, tkn
;
open = 1;
while (open) {
tkn = tokens[++argIdx];
if (tkn.value === '(') open++;
if (tkn.value === ')') open--;
}
var argsIncludingClosingParen = code.slice(firstArgTkn.range[0], tkn.range[1])
, result = 'log.' + kind + '("main-logger", ' + argsIncludingClosingParen;
return { replacement: result, skipPastToken: tkn };
}
}
}
, origCode = [
'console.info("info ", 1);'
, 'console.warn("warn ", 3);'
, 'console.error("error ", new Error("oh my!"));'
].join('\n')
, expectedCode = [
'log.info("main-logger", "info ", 1));'
, 'log.warn("main-logger", "warn ", 3));'
, 'log.error("main-logger", "error ", new Error("oh my!")));'
].join('\n')
, code = redeyed(origCode, opts).code
t.equals(code, expectedCode, 'transforms all log statements')
t.end()
});
+148
View File
@@ -0,0 +1,148 @@
'use strict';
/*jshint asi: true*/
var test = require('tap').test
, util = require('util')
, redeyed = require('..')
function inspect (obj) {
return util.inspect(obj, false, 5, true)
}
test('adding custom asserts ... ', function (t) {
t.constructor.prototype.assertSurrounds = function (code, opts, expected) {
var result = redeyed(code, opts).code
this.equals(result, expected, inspect(code) + ' => ' + inspect(expected))
return this;
}
t.end()
})
test('\nfunction config, keywords', function (t) {
var opts001 = { Keyword: { _default: function (s) { return '*' + s + '&'; } } };
t.test('\n# ' + inspect(opts001), function (t) {
t.assertSurrounds('this', opts001, '*this&')
t.assertSurrounds('this ', opts001, '*this& ')
t.assertSurrounds(' this', opts001, ' *this&')
t.assertSurrounds(' this ', opts001, ' *this& ')
t.assertSurrounds('if (a == 1) return', opts001, '*if& (a == 1) *return&')
t.assertSurrounds('var n = new Test();', opts001, '*var& n = *new& Test();')
t.assertSurrounds(
[ 'function foo (bar) {'
, ' var a = 3;'
, ' return bar + a;'
, '}'
].join('\n')
, opts001
, [ '*function& foo (bar) {'
, ' *var& a = 3;'
, ' *return& bar + a;'
, '}'
].join('\n'))
t.end()
})
var opts002 = {
Keyword: {
'function': function (s) { return '^' + s + '&' }
, 'return': function (s) { return '(' + s + ')' }
, _default: function (s) { return '*' + s + '&' }
}
};
t.test('\n# ' + inspect(opts002), function (t) {
t.assertSurrounds(
[ 'function foo (bar) {'
, ' var a = 3;'
, ' return bar + a;'
, '}'
].join('\n')
, opts002
, [ '^function& foo (bar) {'
, ' *var& a = 3;'
, ' (return) bar + a;'
, '}'
].join('\n'))
t.end()
})
t.end()
})
test('#\n functin config - resolving', function (t) {
var opts001 = {
Keyword: {
'var': function (s) { return '^' + s + '&' }
}
, _default: function (s) { return '*' + s + '&' }
};
t.test('\n# specific but no type default and root default - root default not applied' + inspect(opts001), function (t) {
t.assertSurrounds('var n = new Test();', opts001, '^var& n = new Test();').end();
})
var opts002 = {
Keyword: {
'var': function (s) { return '^' + s + '&' }
, _default: function (s) { return '*' + s + '&' }
}
, _default: function (s) { return '(' + s + ')' }
};
t.test('\n# no type default but root default' + inspect(opts002), function (t) {
t.assertSurrounds('var n = new Test();', opts002, '^var& n = *new& Test();').end();
})
t.end()
})
test('#\n function config - replacing', function (t) {
var opts001 = {
Keyword: {
'var': function () { return 'const' }
}
};
t.test('\n# type default and root default (type wins)' + inspect(opts001), function (t) {
t.assertSurrounds('var n = new Test();', opts001, 'const n = new Test();').end();
})
var opts002 = {
Keyword: {
_default: function () { return 'const' }
}
};
t.test('\n# type default' + inspect(opts002), function (t) {
t.assertSurrounds('var n = new Test();', opts002, 'const n = const Test();').end();
})
var opts003 = {
Keyword: {
'new': function () { return 'NEW'; }
, _default: function () { return 'const' }
}
};
t.test('\n# specific and type default' + inspect(opts003), function (t) {
t.assertSurrounds('var n = new Test();', opts003, 'const n = NEW Test();').end();
})
var opts004 = {
Keyword: {
_default: function (s) { return s.toUpperCase() }
}
, _default: function (s) { return 'not applied'; }
};
t.test('\n# type default and root default (type wins)' + inspect(opts004), function (t) {
t.assertSurrounds('var n = new Test();', opts004, 'VAR n = NEW Test();').end();
})
var opts005 = {
Keyword: { }
, _default: function (s) { return s.toUpperCase() }
};
t.test('\n# no type default only root default - not applied' + inspect(opts005), function (t) {
t.assertSurrounds('var n = new Test();', opts005, 'var n = new Test();').end();
})
t.end()
})
+38
View File
@@ -0,0 +1,38 @@
'use strict';
/*jshint asi: true*/
var test = require('tap').test
, util = require('util')
, redeyed = require('..')
function inspect (obj) {
return util.inspect(obj, false, 5, true)
}
test('adding custom asserts ... ', function (t) {
t.constructor.prototype.assertSurrounds = function (code, opts, expected) {
var optsi = inspect(opts);
var result = redeyed(code, opts).code
this.equals( result
, expected
, util.format('%s: %s => %s', optsi, inspect(code), inspect(expected))
)
return this;
}
t.end()
})
test('incomplete statement', function (t) {
t.test('\n# Keyword', function (t) {
var keyconfig = { 'Keyword': { _default: '$:%' } };
t.assertSurrounds('if(foo) { x', keyconfig, '$if%(foo) { x')
t.assertSurrounds('class Foo { constructor(name)', keyconfig, '$class% Foo { constructor(name)')
t.assertSurrounds('const x = ', keyconfig, '$const% x = ')
t.assertSurrounds('function g() { yield', keyconfig, '$function% g() { $yield%')
t.end()
})
t.end()
})
+45
View File
@@ -0,0 +1,45 @@
'use strict';
/*jshint asi: true*/
var test = require('tap').test
, util = require('util')
, redeyed = require('..')
function inspect (obj) {
return util.inspect(obj, false, 5, true)
}
test('adding custom asserts ... ', function (t) {
t.constructor.prototype.assertSurrounds = function (code, opts, expected) {
var optsi = inspect(opts);
var result = redeyed(code, opts).code
this.equals( result
, expected
, util.format('%s: %s => %s', optsi, inspect(code), inspect(expected))
)
return this;
}
t.end()
})
test('types', function (t) {
t.test('\n# Keyword', function (t) {
var keyconfig = { 'Keyword': { _default: '$:%' } };
t.assertSurrounds('import foo from \'foo\';', keyconfig, '$import% foo from \'foo\';')
t.assertSurrounds('export default foo;', keyconfig, '$export% $default% foo;')
t.assertSurrounds('if(foo) { let bar = 1;}', keyconfig, '$if%(foo) { $let% bar = 1;}')
t.assertSurrounds('const x = "foo";', keyconfig, '$const% x = "foo";')
t.assertSurrounds('"use strict";(function* () { yield *v })', keyconfig, '"use strict";($function%* () { $yield% *v })')
t.assertSurrounds('"use strict"; (class A { static constructor() { super() }})', keyconfig
,'"use strict"; ($class% A { $static% constructor() { $super%() }})')
t.assertSurrounds('class Foo { constructor(name){this.name = name;}}', keyconfig
, '$class% Foo { constructor(name){$this%.name = name;}}')
t.assertSurrounds('class Foo extends Bar { constructor(name,value){super(value);this.name = name;}}', keyconfig
, '$class% Foo $extends% Bar { constructor(name,value){$super%(value);$this%.name = name;}}')
t.end()
})
t.end()
})
+48
View File
@@ -0,0 +1,48 @@
'use strict';
/*jshint asi: true*/
var test = require('tap').test
, util = require('util')
, redeyed = require('..')
function inspect (obj) {
return util.inspect(obj, false, 5, true)
}
test('adding custom asserts ... ', function (t) {
t.constructor.prototype.assertSurrounds = function (code, opts, expected) {
var result = redeyed(code, opts).code
this.equals(result, expected, inspect(code) + ' => ' + inspect(expected))
return this;
}
t.end()
})
test('\nmixed config, keywords', function (t) {
var opts001 = {
Keyword: {
'this': function (s) { return '_' + s; }
, 'if': { _before: '^' }
, _default: '*:&'
}
};
t.test('\n# ' + inspect(opts001), function (t) {
t.assertSurrounds('if (this.hello) return "world";', opts001, '^if& (_this.hello) *return& "world";').end()
})
var opts002 = {
Keyword: {
'this': function (s) { return '_' + s; }
, 'if': { _before: '^' }
, 'return': ':)'
, _default: ':&'
}
, _default: '*:&'
};
t.test('\n# ' + inspect(opts002), function (t) {
t.assertSurrounds('if (this.hello) return "world";', opts002, '^if& (_this.hello) *return) "world";').end()
})
t.end()
})
+65
View File
@@ -0,0 +1,65 @@
'use strict';
/*jshint asi: true*/
var test = require('tap').test
, util = require('util')
, redeyed = require('..')
, esprima = require('esprima')
function inspect (obj) {
return util.inspect(obj, false, 5, true)
}
test('redeyed result does not have esprima ast by default', function (t) {
var code = '// a comment\nvar a = 3;'
, conf = { Keyword: { _default: '_:-' } }
, ast = esprima.parse(code, { tokens: true, comment: true, range: true, tolerant: true })
, tokens = ast.tokens
, comments = ast.comments
, result = redeyed(code, conf)
t.type(result.ast, 'undefined', 'ast')
t.deepEquals(result.tokens, tokens, 'tokens')
t.deepEquals(result.comments, comments, 'comments')
t.notEquals(result.code, undefined, 'code')
t.end()
});
test('redeyed result has esprima ast, tokens, comments and splits and transformed code', function (t) {
var code = '// a comment\nvar a = 3;'
, conf = { Keyword: { _default: '_:-' } }
, ast = esprima.parse(code, { tokens: true, comment: true, range: true, tolerant: true })
, tokens = ast.tokens
, comments = ast.comments
, result = redeyed(code, conf, { buildAst: true } )
console.log(ast)
t.deepEquals(result.ast, ast, 'ast')
t.deepEquals(result.tokens, tokens, 'tokens')
t.deepEquals(result.comments, comments, 'comments')
t.notEquals(result.code, undefined, 'code')
t.end()
});
test('redeyed result - { nojoin } has esprima ast, tokens, comments and splits but no transformed code', function (t) {
var code = '// a comment\nvar a = 3;'
, conf = { Keyword: { _default: '_:-' } }
, ast = esprima.parse(code, { tokens: true, comment: true, range: true, tolerant: true })
, tokens = ast.tokens
, comments = ast.comments
, result = redeyed(code, conf, { nojoin: true, buildAst: true })
t.deepEquals(result.ast, ast, 'ast')
t.deepEquals(result.tokens, tokens, 'tokens')
t.deepEquals(result.comments, comments, 'comments')
t.equals(result.code, undefined, 'code')
t.end()
});
+22
View File
@@ -0,0 +1,22 @@
'use strict';
/*jshint asi: true*/
var test = require('tap').test
, util = require('util')
, redeyed = require('..')
function inspect (obj) {
return util.inspect(obj, false, 5, true)
}
test('properly handles script level return -- no blow up', function (t) {
var code = [
, 'return 1;'
].join('\n')
, opts = { Keyword: { 'return': '%:^' } }
, expected = '\n%return^ 1;'
, res = redeyed(code, opts).code
t.equals(res, expected, inspect(code) + ' opts: ' + inspect(opts) + ' => ' + inspect(expected))
t.end()
})
+26
View File
@@ -0,0 +1,26 @@
'use strict';
/*jshint asi: true*/
var test = require('tap').test
, util = require('util')
, redeyed = require('..')
function inspect (obj) {
return util.inspect(obj, false, 5, true)
}
test('preserves shebang', function (t) {
var code = [
'#!/usr/bin/env node'
, 'var util = require("util");'
].join('\n')
, opts = { Keyword: { 'var': '%:^' } }
, expected = [
'#!/usr/bin/env node'
, '%var^ util = require("util");'
].join('\n')
, res = redeyed(code, opts).code
t.equals(res, expected, inspect(code) + ' opts: ' + inspect(opts) + ' => ' + inspect(expected))
t.end()
})
+78
View File
@@ -0,0 +1,78 @@
'use strict';
/*jshint asi: true*/
// applying redeyed to a bunch of files of contained libraries as a smoke test
var test = require('tap').test
, path = require('path')
, fs = require('fs')
, readdirp = require('readdirp')
, redeyed = require('..')
, esprima = require('esprima')
, node_modules = path.join(__dirname, '..', 'node_modules')
, tapdir = path.join(node_modules, 'tap')
, esprimadir = path.join(node_modules, 'esprima')
test('tap', function (t) {
var invalidTapFiles = [
, 'slide/lib/async-map-ordered.js'
]
function shouldProcess (path) {
var include = true
invalidTapFiles.every(function (entry) {
return include = (path.indexOf(entry) < 0)
});
return include
}
function containsVarKeyword(code) {
code = code.replace(/^#!([^\r\n]+)/, function(match, captured) { return "//" + captured; });
return esprima.tokenize(code).some(function (t) {
return t.type === 'Keyword' && t.value === 'var'
})
}
readdirp({ root: tapdir, fileFilter: '*.js' })
.on('data', function (entry) {
var code = fs.readFileSync(entry.fullPath, 'utf-8')
if (!shouldProcess(entry.fullPath) || !containsVarKeyword(code)) return
var resultAst = redeyed(code, { Keyword: { 'var': '+:-' } }, { buildAst: true }).code
, resultTokenize = redeyed(code, { Keyword: { 'var': '+:-' } }, { buildAst: false }).code
t.assert(~resultAst.indexOf('+var-') || !(~resultAst.indexOf('var ')), 'redeyed ' + entry.path)
t.assert(~resultTokenize.indexOf('+var-') || !(~resultTokenize.indexOf('var ')), 'redeyed ' + entry.path)
})
.on('end', t.end.bind(t))
})
test('esprima', function (t) {
readdirp({ root: esprimadir, fileFilter: '*.js' })
.on('data', function (entry) {
var code = fs.readFileSync(entry.fullPath, 'utf-8')
, resultAst = redeyed(code, { Keyword: { 'var': '+:-' } }, { buildAst: true }).code
, resultTokenize = redeyed(code, { Keyword: { 'var': '+:-' } }, { buildAst: false }).code
t.assert(~resultAst.indexOf('+var-') || !(~resultAst.indexOf('var ')), 'redeyed ' + entry.path)
t.assert(~resultTokenize.indexOf('+var-') || !(~resultTokenize.indexOf('var ')), 'redeyed ' + entry.path)
})
.on('end', t.end.bind(t))
})
test('redeyed', function (t) {
readdirp({ root: path.join(__dirname, '..'), fileFilter: '*.js', directoryFilter: ['!.git', '!node_modules' ] })
.on('data', function (entry) {
var code = fs.readFileSync(entry.fullPath, 'utf-8')
, result = redeyed(code, { Keyword: { 'var': '+:-' } }).code
t.assert(~result.indexOf('+var-') || !(~result.indexOf('var ')), 'redeyed ' + entry.path)
})
.on('end', t.end.bind(t))
})
+127
View File
@@ -0,0 +1,127 @@
'use strict';
/*jshint asi: true*/
var test = require('tap').test
, util = require('util')
, redeyed = require('..')
function inspect (obj) {
return util.inspect(obj, false, 5, true)
}
test('adding custom asserts ... ', function (t) {
t.constructor.prototype.assertSurrounds = function (code, opts, expected) {
var result = redeyed(code, opts).code
this.equals(result, expected, inspect(code) + ' => ' + inspect(expected))
return this;
}
t.end()
})
test('\nstring config, keywords', function (t) {
var opts001 = { Keyword: { _default: '*:&' } };
t.test('\n# ' + inspect(opts001), function (t) {
t.assertSurrounds('this', opts001, '*this&')
t.assertSurrounds('if (a == 1) return', opts001, '*if& (a == 1) *return&')
t.assertSurrounds('var n = new Test();', opts001, '*var& n = *new& Test();')
t.end()
})
var opts002 = {
Keyword: {
'function': '^:'
, 'return': '(:)'
, _default: '*:&'
}
};
t.test('\n# ' + inspect(opts002), function (t) {
t.assertSurrounds(
[ 'function foo (bar) {'
, ' var a = 3;'
, ' return bar + a;'
, '}'
].join('\n')
, opts002
, [ '^function& foo (bar) {'
, ' *var& a = 3;'
, ' (return) bar + a;'
, '}'
].join('\n'))
t.end()
})
t.end()
})
test('\nstring configs resolve from type and root', function (t) {
var code = 'var a = new Test();'
function run(t, conf, expected, code_) {
t.test('\n# ' + inspect(conf), function (t) {
t.assertSurrounds(code_ || code, conf, expected);
t.end()
})
}
// at least the token kind has to be configured in order for the root_default to be applied
// otherwise a root._default would affect all tokens, even the ones we want to leave unchanged
run(t, { _default: '*:' }, 'var a = new Test();')
t.test('\n\n# only before or after specified, but no root._default', function (t) {
run(t, { Keyword: { _default: '*:' } }, '*var a = *new Test();')
run(t, { Keyword: { _default: ':-' } }, 'var- a = new- Test();')
t.end()
})
t.test('\n\n# resolve missing from root._default', function (t) {
run(t, { Keyword: { _default: '*:' }, _default: '(:-' }, '*var- a = *new- Test();')
run(t, { Keyword: { _default: ':-' }, _default: '*:)' }, '*var- a = *new- Test();')
t.end()
})
t.test('\n\n# no resolve if all specified', function (t) {
run(t, { Keyword: { _default: '+:-' }, _default: '*:)' }, '+var- a = +new- Test();')
run(t, { Keyword: { _default: ':-' }, _default: ':)' }, 'var- a = new- Test();')
t.end()
})
t.test('\n\n# resolve specific token no defaults', function (t) {
run(t, { Keyword: { 'var': '*:' } }, '*var a = new Test();')
run(t, { Keyword: { 'var': ':-' } }, 'var- a = new Test();')
t.end()
})
t.test('\n\n# resolve specific token with type defaults', function (t) {
run(t, { Keyword: { 'var': '*:', _default: ':-' } }, '*var- a = new- Test();')
run(t, { Keyword: { 'var': '*:', _default: '(:-' } }, '*var- a = (new- Test();')
run(t, { Keyword: { 'var': ':-', _default: '*:' } }, '*var- a = *new Test();')
run(t, { Keyword: { 'var': ':-', _default: '*:)' } }, '*var- a = *new) Test();')
run(t, { Keyword: { 'var': ':-', 'new': ':&', _default: '*:' } }, '*var- a = *new& Test();')
t.end()
})
t.test(
'\n\n# resolve specific token with root defaults, but no type default - root default not applied to unspecified tokens'
, function (t) {
run(t, { Keyword: { 'var': '*:' }, _default: ':-' }, '*var- a = new Test();')
run(t, { Keyword: { 'var': ':-' }, _default: '*:' }, '*var- a = new Test();')
t.end()
}
)
t.test('\n\n# resolve specific token with type and root defaults', function (t) {
run(t, { Keyword: { 'var': '*:', _default: '+:-' }, _default: ':)' }, '*var- a = +new- Test();')
run(t, { Keyword: { 'var': ':-', _default: '*:+' }, _default: '(:' }, '*var- a = *new+ Test();')
t.end()
})
t.test('all exact tokens undefined, but type default', function (t) {
run(t, { 'Boolean': { 'true': undefined, 'false': undefined, _default: '+:-' } }, 'return +true- || +false-;', 'return true || false;')
t.end()
})
t.end()
})
+77
View File
@@ -0,0 +1,77 @@
'use strict';
/*jshint asi: true*/
var test = require('tap').test
, util = require('util')
, redeyed = require('..')
function inspect (obj) {
return util.inspect(obj, false, 5, true)
}
test('adding custom asserts ... ', function (t) {
t.constructor.prototype.assertSurrounds = function (code, opts, expected) {
var optsi = inspect(opts);
var result = redeyed(code, opts).code
this.equals( result
, expected
, util.format('%s: %s => %s', optsi, inspect(code), inspect(expected))
)
return this;
}
t.end()
})
test('types', function (t) {
t.test('\n# Boolean', function (t) {
t.assertSurrounds('return true;', { 'Boolean': { _default: '$:%' } }, 'return $true%;')
t.assertSurrounds( 'return true; return false;'
, { 'Boolean': { 'false': '#:', _default: '$:%' } }
, 'return $true%; return #false%;')
t.end()
})
t.test('\n# Identifier', function (t) {
t.assertSurrounds('var a = 1;', { 'Identifier': { _default: '$:%' } }, 'var $a% = 1;')
t.assertSurrounds( 'var a = 1; const b = 2;'
, { 'Identifier': { 'b': '#:', _default: '$:%' } }
, 'var $a% = 1; const #b% = 2;')
t.end()
})
t.test('\n# Null', function (t) {
t.assertSurrounds('return null;', { 'Null': { _default: '$:%' } }, 'return $null%;').end()
})
t.test('\n# Numeric', function (t) {
t.assertSurrounds('return 1;', { 'Numeric': { _default: '$:%' } }, 'return $1%;')
t.assertSurrounds( 'return 1; return 2;'
, { 'Numeric': { '2': '#:', _default: '$:%' } }
, 'return $1%; return #2%;')
t.end()
})
t.test('\n# Punctuator', function (t) {
var punctuator = { 'Punctuator': { _default: '$:%' } };
t.assertSurrounds('return 2 * 2;', punctuator, 'return 2 $*% 2$;%')
t.assertSurrounds( 'return 2 * 2;'
, { 'Punctuator': {'*': '#:', _default: '$:%' } }
, 'return 2 #*% 2$;%')
t.assertSurrounds('var {op, lhs, rhs} = getASTNode()', punctuator, 'var ${%op$,% lhs$,% rhs$}% $=% getASTNode$(%$)%')
t.assertSurrounds('function f(x, y=12) { return x + y;}', punctuator, 'function f$(%x$,% y$=%12$)% ${% return x $+% y$;%$}%')
t.assertSurrounds('function f(x, ...y) { return x * y.length;}', punctuator, 'function f$(%x$,% $...%y$)% ${% return x $*% y$.%length$;%$}%')
t.end()
})
t.test('\n# String', function (t) {
t.assertSurrounds('return "hello";', { 'String': { _default: '$:%' } }, 'return $"hello"%;')
t.assertSurrounds( 'return "hello"; return "world";'
, { 'String': { '"world"': '#:', _default: '$:%' } }
, 'return $"hello"%; return #"world"%;')
t.end()
})
t.end()
})
+46
View File
@@ -0,0 +1,46 @@
'use strict';
/*jshint asi: true*/
var test = require('tap').test
, util = require('util')
, redeyed = require('..')
function inspect (obj) {
return util.inspect(obj, false, 5, true)
}
test('adding custom asserts ... ', function (t) {
t.constructor.prototype.assertSurrounds = function (code, opts, expected) {
var optsi = inspect(opts);
var result = redeyed(code, opts).code
this.equals( result
, expected
, util.format('%s: %s => %s', optsi, inspect(code), inspect(expected))
)
return this;
}
t.end()
})
test('upcoming syntax: rest and spread properties', function (t) {
t.test('\n# Punctuator', function (t) {
var punctuator = { 'Punctuator': { _default: '$:%' } };
t.assertSurrounds('{a,...b} = c', punctuator, '${%a$,%$...%b$}% $=% c')
t.assertSurrounds('x={y,...z}', punctuator, 'x$=%${%y$,%$...%z$}%')
t.assertSurrounds('x ** y', punctuator, 'x $**% y');
t.assertSurrounds('x **= y', punctuator, 'x $**=% y');
t.end()
})
t.test('\n# Identifier', function (t) {
var identifier = { 'Identifier': { _default: '$:%' } };
t.assertSurrounds('{a,...b} = c', identifier, '{$a%,...$b%} = $c%')
t.assertSurrounds('x={y,...z}', identifier, '$x%={$y%,...$z%}')
t.end()
})
t.end()
})