Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use file.cwd for default sourceRoot #4

Merged
merged 17 commits into from
Aug 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
# gulp-espower [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][depstat-image]][depstat-url]
# gulp-espower

[![NPM version][npm-image]][npm-url]
[![Build Status][travis-image]][travis-url]
[![Dependency Status][depstat-image]][depstat-url]

> A [gulp](https://github.com/wearefractal/gulp) plugin for [power-assert](https://github.com/power-assert-js/power-assert).

## Description

`gulp-espower` is a gulp plugin to instrument "Power Assert" feature into your code.


Internally, `gulp-espower` task uses `espower` module that manipulates assertion expression (JavaScript Code) represented as [Mozilla JavaScript AST](https://developer.mozilla.org/en-US/docs/SpiderMonkey/Parser_API), to instrument power-assert feature into the code. The magic is done by using [Esprima](http://esprima.org/) and [Escodegen](https://github.com/Constellation/escodegen).
Internally, `gulp-espower` task uses `espower` module that manipulates assertion expression (JavaScript Code) defined in [The ESTree Spec](https://github.com/estree/estree) (formerly known as [Mozilla SpiderMonkey Parser API](https://developer.mozilla.org/en-US/docs/SpiderMonkey/Parser_API)), to instrument power-assert feature into the code. The magic is done by using [Esprima](http://esprima.org/) and [Escodegen](https://github.com/Constellation/escodegen).


Please note that `gulp-espower` is a beta version product. Pull-requests, issue reports and patches are always welcomed. See [power-assert](https://github.com/power-assert-js/power-assert) project for more documentation.
Pull-requests, issue reports and patches are always welcomed. See [power-assert](https://github.com/power-assert-js/power-assert) project for more documentation.


## Usage
Expand Down Expand Up @@ -74,7 +79,9 @@ For more information, see [gulp-sourcemaps](https://github.com/floridoo/gulp-sou
'assert.strictEqual(actual, expected, [message])',
'assert.notStrictEqual(actual, expected, [message])',
'assert.deepEqual(actual, expected, [message])',
'assert.notDeepEqual(actual, expected, [message])'
'assert.notDeepEqual(actual, expected, [message])',
'assert.deepStrictEqual(actual, expected, [message])',
'assert.notDeepStrictEqual(actual, expected, [message])'
]
```

Expand Down
51 changes: 32 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
*/
'use strict';

var through = require('through2'),
gutil = require('gulp-util'),
extend = require('xtend'),
BufferStreams = require('bufferstreams'),
espower = require('espower'),
espowerSource = require('espower-source'),
esprima = require('esprima'),
escodegen = require('escodegen'),
applySourceMap = require('vinyl-sourcemaps-apply'),
transfer = require('multi-stage-sourcemap').transfer,
convert = require('convert-source-map');
var through = require('through2');
var gutil = require('gulp-util');
var extend = require('xtend');
var BufferStreams = require('bufferstreams');
var espower = require('espower');
var espowerSource = require('espower-source');
var esprima = require('esprima');
var escodegen = require('escodegen');
var applySourceMap = require('vinyl-sourcemaps-apply');
var transfer = require('multi-stage-sourcemap').transfer;
var convert = require('convert-source-map');

function mergeSourceMap(incomingSourceMap, outgoingSourceMap) {
if (typeof outgoingSourceMap === 'string' || outgoingSourceMap instanceof String) {
Expand All @@ -31,23 +31,30 @@ function mergeSourceMap(incomingSourceMap, outgoingSourceMap) {
return JSON.parse(transfer({fromSourceMap: outgoingSourceMap, toSourceMap: incomingSourceMap}));
}

function mergeEspowerOptions (options, file) {
return extend(espower.defaultOptions(), {
sourceRoot: file.cwd,
path: file.path
}, options, {
destructive: true
});
}

function transform (file, encoding, opt) {
var inMap = file.sourceMap;
var escodegenOptions = {};
var jsCode = file.contents.toString(encoding);

// use file.relative to keep paths relative until the end of chain
var jsAst = esprima.parse(jsCode, {tolerant: true, loc: true, source: file.relative});
var jsAst = esprima.parse(jsCode, {tolerant: true, loc: true});

var espowerOptions = extend(espower.defaultOptions(), opt, {
destructive: true,
path: file.path
});
var espowerOptions = mergeEspowerOptions(opt, file);
if (inMap) {
espowerOptions.sourceMap = inMap;
// https://github.com/floridoo/gulp-sourcemaps#plugin-developers-only-how-to-add-source-map-support-to-plugins
escodegenOptions = extend(escodegenOptions, {
// use file.relative for `file` and `sources` to keep paths relative until the end of chain
file: file.relative,
sourceMap: true,
sourceMap: file.relative,
// do not set sourceMapRoot to keep paths relative until the end of chain
// sourceMapRoot: file.base,
sourceMapWithCode: true
Expand Down Expand Up @@ -96,7 +103,13 @@ module.exports = function (opt) {
if(err) {
cb(new gutil.PluginError('gulp-espower', err, {showStack: true}));
} else {
cb(null, new Buffer(espowerSource(buf.toString(encoding), file.path, opt)));
var modifiedCode;
try {
modifiedCode = espowerSource(buf.toString(encoding), file.path, mergeEspowerOptions(opt, file));
} catch (err) {
return callback(new gutil.PluginError('gulp-espoewr', err, {showStack: true}));
}
cb(null, new Buffer(modifiedCode));
}
}));
this.push(file);
Expand Down
33 changes: 15 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@
}
],
"dependencies": {
"bufferstreams": "~0.0.2",
"convert-source-map": "~0.4.1",
"escodegen": "~1.4.1",
"espower": "~0.10.0",
"espower-source": "~0.10.0",
"esprima": "~1.2.2",
"gulp-util": "~3.0.1",
"multi-stage-sourcemap": "~0.2.1",
"through2": "~1.1.1",
"vinyl-sourcemaps-apply": "~0.1.4",
"xtend": "~4.0.0"
"bufferstreams": "^1.0.1",
"convert-source-map": "^1.1.1",
"escodegen": "^1.6.1",
"espower": "^1.0.6",
"espower-source": "^1.0.0",
"esprima": "^2.0.0",
"gulp-util": "^3.0.5",
"multi-stage-sourcemap": "^0.2.1",
"through2": "^2.0.0",
"vinyl-sourcemaps-apply": "^0.1.4",
"xtend": "^4.0.0"
},
"devDependencies": {
"event-stream": "~3.1.7",
"jshint": "~2.5.11",
"mocha": "~2.1.0"
"event-stream": "^3.3.1",
"jshint": "^2.8.0",
"mocha": "^2.2.5"
},
"engines": {
"node": ">=0.8.0",
Expand All @@ -54,10 +54,7 @@
"test",
"testing"
],
"license": {
"type": "MIT",
"url": "https://github.com/power-assert-js/gulp-espower/blob/master/LICENSE-MIT"
},
"license": "MIT",
"main": "./index.js",
"repository": {
"type": "git",
Expand Down
26 changes: 0 additions & 26 deletions test/expected/customized-with-sourcemap.js

This file was deleted.

110 changes: 65 additions & 45 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

delete require.cache[require.resolve('../')];

var fs = require("fs"),
es = require("event-stream"),
assert = require("assert"),
gutil = require("gulp-util"),
convert = require('convert-source-map'),
espower = require("../");
var fs = require("fs");
var es = require("event-stream");
var assert = require("assert");
var gutil = require("gulp-util");
var convert = require('convert-source-map');
var espower = require("../");

describe("gulp-espower", function () {

Expand All @@ -17,15 +17,15 @@ describe("gulp-espower", function () {
beforeEach(function () {
stream = espower();
srcFile = new gutil.File({
path: "test/fixtures/example.js",
cwd: "test/",
base: "test/fixtures",
path: process.cwd() + "/test/fixtures/example.js",
cwd: process.cwd(),
base: process.cwd() + "/test/fixtures",
contents: fs.readFileSync("test/fixtures/example.js")
});
expectedFile = new gutil.File({
path: "test/expected/example.js",
cwd: "test/",
base: "test/expected",
path: process.cwd() + "/test/expected/example.js",
cwd: process.cwd(),
base: process.cwd() + "/test/expected",
contents: fs.readFileSync("test/expected/example.js")
});
});
Expand Down Expand Up @@ -110,15 +110,15 @@ describe("gulp-espower", function () {
]
});
srcFile = new gutil.File({
path: "test/fixtures/customized.js",
cwd: "test/",
base: "test/fixtures",
path: process.cwd() + "/test/fixtures/customized.js",
cwd: process.cwd(),
base: process.cwd() + "/test/fixtures",
contents: fs.readFileSync("test/fixtures/customized.js")
});
expectedFile = new gutil.File({
path: "test/expected/example.js",
cwd: "test/",
base: "test/expected",
path: process.cwd() + "/test/expected/example.js",
cwd: process.cwd(),
base: process.cwd() + "/test/expected",
contents: fs.readFileSync("test/expected/customized.js")
});
});
Expand Down Expand Up @@ -198,19 +198,19 @@ describe("gulp-espower", function () {


it('should produce expected file via stream', function (done) {
var stream = espower(),
srcStream = new gutil.File({
path: 'test/fixtures/example.js',
cwd: 'test/',
base: 'test/fixtures',
contents: fs.createReadStream('test/fixtures/example.js')
}),
expectedFile = new gutil.File({
path: 'test/expected/example.js',
cwd: 'test/',
base: 'test/expected',
contents: fs.readFileSync('test/expected/example-with-sourcemap.js')
});
var stream = espower();
var srcStream = new gutil.File({
path: process.cwd() + '/test/fixtures/example.js',
cwd: process.cwd(),
base: process.cwd() + '/test/fixtures',
contents: fs.createReadStream('test/fixtures/example.js')
});
var expectedFile = new gutil.File({
path: process.cwd() + '/test/expected/example.js',
cwd: process.cwd(),
base: process.cwd() + '/test/expected',
contents: fs.readFileSync('test/expected/example-with-sourcemap.js')
});
stream.on('error', function(err) {
assert(err);
done();
Expand All @@ -220,29 +220,49 @@ describe("gulp-espower", function () {
assert(newFile.contents);
newFile.contents.pipe(es.wait(function(err, data) {
assert(!err);
assert.equal(data, String(expectedFile.contents));
assert.equal(data.toString('utf-8'), String(expectedFile.contents));
done();
}));
});
stream.write(srcStream);
stream.end();
});

it('should emit the error when the file has a syntax error', function (done) {
var stream = espower(),
srcFile = new gutil.File({
path: "test/fixtures/syntax-error.js",
cwd: "test/",
base: "test/fixtures",

describe('should emit error when the file has a syntax error', function () {
it('when file is Buffer', function (done) {
var stream = espower();
var srcFile = new gutil.File({
path: process.cwd() + "/test/fixtures/syntax-error.js",
cwd: process.cwd(),
base: process.cwd() + "/test/fixtures",
contents: fs.readFileSync("test/fixtures/syntax-error.js")
});
assert.doesNotThrow(function() {
stream.on("error", function(err) {
assert(err);
done();
});
stream.write(srcFile);
stream.end();
assert.doesNotThrow(function() {
stream.on("error", function(err) {
assert(err);
done();
});
stream.write(srcFile);
stream.end();
});
});
it('when file is Stream', function (done) {
var stream = espower();
var srcStream = new gutil.File({
path: process.cwd() + "/test/fixtures/syntax-error.js",
cwd: process.cwd(),
base: process.cwd() + "/test/fixtures",
contents: fs.createReadStream("test/fixtures/syntax-error.js")
});
assert.doesNotThrow(function() {
stream.on("error", function(err) {
assert(err);
done();
});
stream.write(srcStream);
stream.end();
});
});
});
});