Skip to content

Commit

Permalink
feat(espower): use process.cwd() for espower sourceRoot option value
Browse files Browse the repository at this point in the history
  • Loading branch information
twada committed May 25, 2015
1 parent 337cdfd commit 7101643
Show file tree
Hide file tree
Showing 24 changed files with 145 additions and 129 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ $ ./node_modules/.bin/mocha /path/to/build/demo_test.js
CUSTOMIZE
---------------------------------------

### var plugin = createEspowerPlugin(babel, [options])

You can customize assertion patterns via [Babel API](http://babeljs.io/docs/usage/api/),

```javascript
Expand Down Expand Up @@ -237,6 +239,14 @@ require('babel-core/register')({
});
```

#### options

| type | default value |
|:---------|:--------------|
| `object` | (return value of `espower.defaultOptions()` with default `path`, `sourceRoot` and `sourceMap`) |

Configuration options for internal `espower` module. If not passed, default options will be used (return value of `espower.defaultOptions()` with default `path`, `sourceRoot` and `sourceMap`. `path` is filename passed to babel. `sourceRoot` is be return value of `process.cwd()`, `sourceMap` is babel's internal SourceMap object).


CHANGELOG
---------------------------------------
Expand Down
13 changes: 7 additions & 6 deletions lib/create-espower-visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var escallmatch = require('escallmatch');
var extend = require('xtend');
var find = require('array-find');

function enterTraversalPath (traversalPath, currentNode, parentNode, scope, file, matchers, esTreePath) {
function enterTraversalPath (traversalPath, currentNode, parentNode, scope, file, matchers, esTreePath, options) {
var treePath;
var currentKey = traversalPath.key;
var assertionVisitor = file.get('espowerAssertionVisitor');
Expand All @@ -25,10 +25,11 @@ function enterTraversalPath (traversalPath, currentNode, parentNode, scope, file
var matcher = find(matchers, function (m) { return m.test(currentNode); });
if (matcher) {
// entering assertion
var espowerOptions = {
var espowerOptions = extend(espower.defaultOptions(), {
path: file.opts.filename, // or opts.sourceFileName?
sourceRoot: process.cwd(),
sourceMap: file.opts.inputSourceMap
};
}, options);
treePath = esTreePath(traversalPath);
assertionVisitor = new espower.AssertionVisitor(matcher, treePath, espowerOptions);
assertionVisitor.enter(currentNode, parentNode);
Expand All @@ -37,7 +38,7 @@ function enterTraversalPath (traversalPath, currentNode, parentNode, scope, file
}
}

function exitTraversalPath (traversalPath, currentNode, parentNode, scope, file, esTreePath) {
function exitTraversalPath (traversalPath, currentNode, parentNode, scope, file, esTreePath, options) {
var currentKey = traversalPath.key;
var resultTree = currentNode;
var isTreeModified = false;
Expand Down Expand Up @@ -82,10 +83,10 @@ module.exports = function createEspowerVisitor (babel, options) {
return Object.keys(babel.types.VISITOR_KEYS).reduce(function (handlers, nodeType) {
handlers[nodeType] = {
enter: function (currentNode, parentNode, scope, file) {
enterTraversalPath(this, currentNode, parentNode, scope, file, matchers, esTreePath);
enterTraversalPath(this, currentNode, parentNode, scope, file, matchers, esTreePath, options);
},
exit: function (currentNode, parentNode, scope, file) {
exitTraversalPath(this, currentNode, parentNode, scope, file, esTreePath);
exitTraversalPath(this, currentNode, parentNode, scope, file, esTreePath, options);
}
};
return handlers;
Expand Down
8 changes: 4 additions & 4 deletions test/fixtures/ArrayExpression/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@

assert(assert._expr(assert._capt([assert._capt(foo, 'arguments/0/elements/0'), assert._capt(bar, 'arguments/0/elements/1')], 'arguments/0'), {
content: 'assert([foo,bar])',
filepath: 'fixtures/ArrayExpression/fixture.js',
filepath: 'test/fixtures/ArrayExpression/fixture.js',
line: 3
}));

assert(assert._expr(assert._capt(assert._capt(typeof assert._capt([assert._capt([assert._capt(assert._capt(foo, 'arguments/0/left/argument/elements/0/elements/0/object').bar, 'arguments/0/left/argument/elements/0/elements/0'), assert._capt(baz(assert._capt(moo, 'arguments/0/left/argument/elements/0/elements/1/arguments/0')), 'arguments/0/left/argument/elements/0/elements/1')], 'arguments/0/left/argument/elements/0'), assert._capt(+assert._capt(fourStr, 'arguments/0/left/argument/elements/1/argument'), 'arguments/0/left/argument/elements/1')], 'arguments/0/left/argument'), 'arguments/0/left') === 'number', 'arguments/0'), {
content: 'assert(typeof [[foo.bar,baz(moo)],+fourStr] === \'number\')',
filepath: 'fixtures/ArrayExpression/fixture.js',
filepath: 'test/fixtures/ArrayExpression/fixture.js',
line: 5
}));

assert.notDeepEqual(assert._expr(assert._capt([assert._capt(foo, 'arguments/0/elements/0'), assert._capt(bar, 'arguments/0/elements/1')], 'arguments/0'), {
content: 'assert.notDeepEqual([foo,bar], [hoge,fuga,piyo])',
filepath: 'fixtures/ArrayExpression/fixture.js',
filepath: 'test/fixtures/ArrayExpression/fixture.js',
line: 7
}), assert._expr(assert._capt([assert._capt(hoge, 'arguments/1/elements/0'), assert._capt(fuga, 'arguments/1/elements/1'), assert._capt(piyo, 'arguments/1/elements/2')], 'arguments/1'), {
content: 'assert.notDeepEqual([foo,bar], [hoge,fuga,piyo])',
filepath: 'fixtures/ArrayExpression/fixture.js',
filepath: 'test/fixtures/ArrayExpression/fixture.js',
line: 7
}));
2 changes: 1 addition & 1 deletion test/fixtures/ArrowFunctionExpression/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ assert(assert._expr(assert._capt(assert._capt(seven, 'arguments/0/left') === ass
return v + i;
})(assert._capt(four, 'arguments/0/right/arguments/0'), assert._capt(five, 'arguments/0/right/arguments/1')), 'arguments/0/right'), 'arguments/0'), {
content: 'assert(seven === ((v, i) => v + i)(four, five))',
filepath: 'fixtures/ArrowFunctionExpression/fixture.js',
filepath: 'test/fixtures/ArrowFunctionExpression/fixture.js',
line: 9
}));
14 changes: 7 additions & 7 deletions test/fixtures/AssignmentExpression/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,40 @@ function _slicedToArray(arr, i) { if (Array.isArray(arr)) { return arr; } else i

assert(assert._expr(assert._capt(counter += 1, 'arguments/0'), {
content: 'assert(counter += 1)',
filepath: 'fixtures/AssignmentExpression/fixture.js',
filepath: 'test/fixtures/AssignmentExpression/fixture.js',
line: 3
}));

assert(assert._expr(assert._capt(dog.age += 1, 'arguments/0'), {
content: 'assert(dog.age += 1)',
filepath: 'fixtures/AssignmentExpression/fixture.js',
filepath: 'test/fixtures/AssignmentExpression/fixture.js',
line: 5
}));

assert(assert._expr(assert._capt(dog.age += 1, 'arguments/0'), {
content: 'assert(dog.age += 1)',
filepath: 'fixtures/AssignmentExpression/fixture.js',
filepath: 'test/fixtures/AssignmentExpression/fixture.js',
line: 7
}));

assert.strictEqual(assert._expr(assert._capt(dog.age += 1, 'arguments/0'), {
content: 'assert.strictEqual(dog.age += 1, three)',
filepath: 'fixtures/AssignmentExpression/fixture.js',
filepath: 'test/fixtures/AssignmentExpression/fixture.js',
line: 9
}), assert._expr(assert._capt(three, 'arguments/1'), {
content: 'assert.strictEqual(dog.age += 1, three)',
filepath: 'fixtures/AssignmentExpression/fixture.js',
filepath: 'test/fixtures/AssignmentExpression/fixture.js',
line: 9
}));

assert(assert._expr(assert._capt((_temp = assert._capt([3], 'arguments/0/right'), _temp2 = _slicedToArray(_temp, 1), x = _temp2[0], _temp), 'arguments/0'), {
content: 'assert([x] = [3])',
filepath: 'fixtures/AssignmentExpression/fixture.js',
filepath: 'test/fixtures/AssignmentExpression/fixture.js',
line: 11
}));

assert(assert._expr(assert._capt((_temp3 = assert._capt([assert._capt(foo, 'arguments/0/right/elements/0')], 'arguments/0/right'), _temp32 = _slicedToArray(_temp3, 1), x = _temp32[0], _temp3), 'arguments/0'), {
content: 'assert([x] = [foo])',
filepath: 'fixtures/AssignmentExpression/fixture.js',
filepath: 'test/fixtures/AssignmentExpression/fixture.js',
line: 13
}));
16 changes: 8 additions & 8 deletions test/fixtures/BinaryExpression/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,48 @@

assert(assert._expr(assert._capt(4 !== 4, 'arguments/0'), {
content: 'assert(4 !== 4)',
filepath: 'fixtures/BinaryExpression/fixture.js',
filepath: 'test/fixtures/BinaryExpression/fixture.js',
line: 3
}));

assert(assert._expr(assert._capt(assert._capt(fuga, 'arguments/0/left') !== 4, 'arguments/0'), {
content: 'assert(fuga !== 4)',
filepath: 'fixtures/BinaryExpression/fixture.js',
filepath: 'test/fixtures/BinaryExpression/fixture.js',
line: 5
}));

assert(assert._expr(assert._capt(assert._capt(fuga, 'arguments/0/left') === assert._capt(piyo, 'arguments/0/right'), 'arguments/0'), {
content: 'assert(fuga === piyo)',
filepath: 'fixtures/BinaryExpression/fixture.js',
filepath: 'test/fixtures/BinaryExpression/fixture.js',
line: 7
}));

assert(assert._expr(assert._capt(assert._capt(fuga, 'arguments/0/left') === assert._capt(piyo, 'arguments/0/right'), 'arguments/0'), {
content: 'assert(fuga === piyo)',
filepath: 'fixtures/BinaryExpression/fixture.js',
filepath: 'test/fixtures/BinaryExpression/fixture.js',
line: 9
}));

assert(assert._expr(assert._capt(assert._capt(fuga, 'arguments/0/left') === assert._capt(piyo, 'arguments/0/right'), 'arguments/0'), {
content: 'assert(fuga === piyo)',
filepath: 'fixtures/BinaryExpression/fixture.js',
filepath: 'test/fixtures/BinaryExpression/fixture.js',
line: 13
}));

assert(assert._expr(assert._capt(assert._capt(fuga, 'arguments/0/left') !== assert._capt(piyo, 'arguments/0/right'), 'arguments/0'), {
content: 'assert(fuga !== piyo)',
filepath: 'fixtures/BinaryExpression/fixture.js',
filepath: 'test/fixtures/BinaryExpression/fixture.js',
line: 15
}));

assert.ok(assert._expr(assert._capt(assert._capt(hoge, 'arguments/0/left') === assert._capt(fuga, 'arguments/0/right'), 'arguments/0'), {
content: 'assert.ok(hoge === fuga, \'comment\')',
filepath: 'fixtures/BinaryExpression/fixture.js',
filepath: 'test/fixtures/BinaryExpression/fixture.js',
line: 17
}), 'comment');

assert(assert._expr(assert._capt(assert._capt(assert._capt(ary1, 'arguments/0/left/object').length, 'arguments/0/left') === assert._capt(assert._capt(ary2, 'arguments/0/right/object').length, 'arguments/0/right'), 'arguments/0'), {
content: 'assert(ary1.length === ary2.length)',
filepath: 'fixtures/BinaryExpression/fixture.js',
filepath: 'test/fixtures/BinaryExpression/fixture.js',
line: 19
}));
24 changes: 12 additions & 12 deletions test/fixtures/CallExpression/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,70 +2,70 @@

assert(assert._expr(assert._capt(func(), 'arguments/0'), {
content: 'assert(func())',
filepath: 'fixtures/CallExpression/fixture.js',
filepath: 'test/fixtures/CallExpression/fixture.js',
line: 3
}));

assert(assert._expr(assert._capt(assert._capt(obj, 'arguments/0/callee/object').age(), 'arguments/0'), {
content: 'assert(obj.age())',
filepath: 'fixtures/CallExpression/fixture.js',
filepath: 'test/fixtures/CallExpression/fixture.js',
line: 5
}));

assert(assert._expr(assert._capt(isFalsy(assert._capt(positiveInt, 'arguments/0/arguments/0')), 'arguments/0'), {
content: 'assert(isFalsy(positiveInt))',
filepath: 'fixtures/CallExpression/fixture.js',
filepath: 'test/fixtures/CallExpression/fixture.js',
line: 7
}));

assert(assert._expr(assert._capt(assert._capt(foo, 'arguments/0/callee/object')[assert._capt(propName, 'arguments/0/callee/property')](), 'arguments/0'), {
content: 'assert(foo[propName]())',
filepath: 'fixtures/CallExpression/fixture.js',
filepath: 'test/fixtures/CallExpression/fixture.js',
line: 9
}));

assert(assert._expr(assert._capt(assert._capt(foo, 'arguments/0/callee/object')[assert._capt(assert._capt(hoge, 'arguments/0/callee/property/object')[assert._capt(assert._capt(fuga, 'arguments/0/callee/property/property/object')[assert._capt(piyo, 'arguments/0/callee/property/property/property')], 'arguments/0/callee/property/property')], 'arguments/0/callee/property')](), 'arguments/0'), {
content: 'assert(foo[hoge[fuga[piyo]]]())',
filepath: 'fixtures/CallExpression/fixture.js',
filepath: 'test/fixtures/CallExpression/fixture.js',
line: 11
}));

assert(assert._expr(assert._capt(assert._capt(sum(assert._capt(one, 'arguments/0/left/arguments/0'), assert._capt(two, 'arguments/0/left/arguments/1'), assert._capt(three, 'arguments/0/left/arguments/2')), 'arguments/0/left') === assert._capt(seven, 'arguments/0/right'), 'arguments/0'), {
content: 'assert(sum(one, two, three) === seven)',
filepath: 'fixtures/CallExpression/fixture.js',
filepath: 'test/fixtures/CallExpression/fixture.js',
line: 13
}));

assert(assert._expr(assert._capt(assert._capt(sum(assert._capt(sum(assert._capt(one, 'arguments/0/left/arguments/0/arguments/0'), assert._capt(two, 'arguments/0/left/arguments/0/arguments/1')), 'arguments/0/left/arguments/0'), assert._capt(three, 'arguments/0/left/arguments/1')), 'arguments/0/left') === assert._capt(sum(assert._capt(sum(assert._capt(two, 'arguments/0/right/arguments/0/arguments/0'), assert._capt(three, 'arguments/0/right/arguments/0/arguments/1')), 'arguments/0/right/arguments/0'), assert._capt(seven, 'arguments/0/right/arguments/1')), 'arguments/0/right'), 'arguments/0'), {
content: 'assert(sum(sum(one, two), three) === sum(sum(two, three), seven))',
filepath: 'fixtures/CallExpression/fixture.js',
filepath: 'test/fixtures/CallExpression/fixture.js',
line: 15
}));

assert(assert._expr(assert._capt(assert._capt(assert._capt(assert._capt(math, 'arguments/0/left/callee/object/object').calc, 'arguments/0/left/callee/object').sum(assert._capt(one, 'arguments/0/left/arguments/0'), assert._capt(two, 'arguments/0/left/arguments/1'), assert._capt(three, 'arguments/0/left/arguments/2')), 'arguments/0/left') === assert._capt(seven, 'arguments/0/right'), 'arguments/0'), {
content: 'assert(math.calc.sum(one, two, three) === seven)',
filepath: 'fixtures/CallExpression/fixture.js',
filepath: 'test/fixtures/CallExpression/fixture.js',
line: 17
}));

assert(assert._expr(assert._capt(assert._capt(assert._capt(three, 'arguments/0/left/left') * assert._capt(assert._capt(seven, 'arguments/0/left/right/left') * assert._capt(ten, 'arguments/0/left/right/right'), 'arguments/0/left/right'), 'arguments/0/left') === assert._capt(three, 'arguments/0/right'), 'arguments/0'), {
content: 'assert(three * (seven * ten) === three)',
filepath: 'fixtures/CallExpression/fixture.js',
filepath: 'test/fixtures/CallExpression/fixture.js',
line: 19
}));

assert(assert._expr(assert._capt(!assert._capt(concat(assert._capt(fuga, 'arguments/0/argument/arguments/0'), assert._capt(piyo, 'arguments/0/argument/arguments/1')), 'arguments/0/argument'), 'arguments/0'), {
content: 'assert(!concat(fuga, piyo))',
filepath: 'fixtures/CallExpression/fixture.js',
filepath: 'test/fixtures/CallExpression/fixture.js',
line: 21
}));

assert.strictEqual(assert._expr(assert._capt(assert._capt(three, 'arguments/0/left') * assert._capt(assert._capt(seven, 'arguments/0/right/left') * assert._capt(ten, 'arguments/0/right/right'), 'arguments/0/right'), 'arguments/0'), {
content: 'assert.strictEqual(three * (seven * ten), math.calc.sum(one, two, three))',
filepath: 'fixtures/CallExpression/fixture.js',
filepath: 'test/fixtures/CallExpression/fixture.js',
line: 23
}), assert._expr(assert._capt(assert._capt(assert._capt(math, 'arguments/1/callee/object/object').calc, 'arguments/1/callee/object').sum(assert._capt(one, 'arguments/1/arguments/0'), assert._capt(two, 'arguments/1/arguments/1'), assert._capt(three, 'arguments/1/arguments/2')), 'arguments/1'), {
content: 'assert.strictEqual(three * (seven * ten), math.calc.sum(one, two, three))',
filepath: 'fixtures/CallExpression/fixture.js',
filepath: 'test/fixtures/CallExpression/fixture.js',
line: 23
}));
10 changes: 5 additions & 5 deletions test/fixtures/ConditionalExpression/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@

assert(assert._expr(assert._capt(foo, 'arguments/0/test') ? assert._capt(bar, 'arguments/0/consequent') : assert._capt(baz, 'arguments/0/alternate'), {
content: 'assert(foo ? bar : baz)',
filepath: 'fixtures/ConditionalExpression/fixture.js',
filepath: 'test/fixtures/ConditionalExpression/fixture.js',
line: 3
}));

assert(assert._expr(assert._capt(falsy, 'arguments/0/test') ? assert._capt(truthy, 'arguments/0/consequent') : assert._capt(truthy, 'arguments/0/alternate/test') ? assert._capt(anotherFalsy, 'arguments/0/alternate/consequent') : assert._capt(truthy, 'arguments/0/alternate/alternate'), {
content: 'assert(falsy ? truthy : truthy ? anotherFalsy : truthy)',
filepath: 'fixtures/ConditionalExpression/fixture.js',
filepath: 'test/fixtures/ConditionalExpression/fixture.js',
line: 5
}));

assert(assert._expr(assert._capt(foo(), 'arguments/0/test') ? assert._capt(assert._capt(bar, 'arguments/0/consequent/object').baz, 'arguments/0/consequent') : assert._capt(typeof goo, 'arguments/0/alternate'), {
content: 'assert(foo() ? bar.baz : typeof goo)',
filepath: 'fixtures/ConditionalExpression/fixture.js',
filepath: 'test/fixtures/ConditionalExpression/fixture.js',
line: 7
}));

assert.equal(assert._expr(assert._capt(foo, 'arguments/0/test') ? assert._capt(bar, 'arguments/0/consequent') : assert._capt(baz, 'arguments/0/alternate'), {
content: 'assert.equal(foo ? bar : baz, falsy ? truthy : truthy ? anotherFalsy : truthy)',
filepath: 'fixtures/ConditionalExpression/fixture.js',
filepath: 'test/fixtures/ConditionalExpression/fixture.js',
line: 9
}), assert._expr(assert._capt(falsy, 'arguments/1/test') ? assert._capt(truthy, 'arguments/1/consequent') : assert._capt(truthy, 'arguments/1/alternate/test') ? assert._capt(anotherFalsy, 'arguments/1/alternate/consequent') : assert._capt(truthy, 'arguments/1/alternate/alternate'), {
content: 'assert.equal(foo ? bar : baz, falsy ? truthy : truthy ? anotherFalsy : truthy)',
filepath: 'fixtures/ConditionalExpression/fixture.js',
filepath: 'test/fixtures/ConditionalExpression/fixture.js',
line: 9
}));
2 changes: 1 addition & 1 deletion test/fixtures/FunctionExpression/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ assert(assert._expr(assert._capt(assert._capt(baz, 'arguments/0/left') === asser
return a + b;
})(assert._capt(foo, 'arguments/0/right/arguments/0'), assert._capt(bar, 'arguments/0/right/arguments/1')), 'arguments/0/right'), 'arguments/0'), {
content: 'assert(baz === function (a, b) {return a + b;}(foo, bar))',
filepath: 'fixtures/FunctionExpression/fixture.js',
filepath: 'test/fixtures/FunctionExpression/fixture.js',
line: 5
}));
Loading

0 comments on commit 7101643

Please sign in to comment.