Skip to content

Commit

Permalink
Merge pull request #234 from realtymaps/1.X
Browse files Browse the repository at this point in the history
fix(array checking css): issue #232
  • Loading branch information
nmccready authored Oct 12, 2016
2 parents dcee23d + f51b931 commit 096f882
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 67 deletions.
5 changes: 3 additions & 2 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"node": true,
"strict": true
}
"strict": true,
"esversion": 6
}
28 changes: 23 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ var urlRegex = /^(https?|webpack(-[^:]+)?):\/\//;
* Initialize source mapping chain
*/
module.exports.init = function init(options) {
var debug = require('debug-fabulous')()(PLUGIN_NAME + ':init');

function sourceMapInit(file, encoding, callback) {
/*jshint validthis:true */

Expand All @@ -33,11 +35,13 @@ module.exports.init = function init(options) {
if (options === undefined) {
options = {};
}
debug(() => {return options;});

var fileContent = file.contents.toString();
var sourceMap;

if (options.loadMaps) {
debug('loadMaps');
var sourcePath = ''; //root path for the sources in the map

// Try to read inline source map
Expand Down Expand Up @@ -99,11 +103,11 @@ module.exports.init = function init(options) {
} else {
try {
if (options.debug)
console.log(PLUGIN_NAME + '-init: No source content for "' + source + '". Loading from file.');
debug('No source content for "' + source + '". Loading from file.');
sourceContent = stripBom(fs.readFileSync(absPath, 'utf8'));
} catch (e) {
if (options.debug)
console.warn(PLUGIN_NAME + '-init: source file not found: ' + absPath);
debug('warn: source file not found: ' + absPath);
}
}
sourceMap.sourcesContent[i] = sourceContent;
Expand All @@ -116,6 +120,7 @@ module.exports.init = function init(options) {
}

if (!sourceMap && options.identityMap) {
debug(() => { return 'identityMap'; });
var fileType = path.extname(file.path);
var source = unixStylePath(file.relative);
var generator = new SourceMapGenerator({file: source});
Expand All @@ -140,7 +145,9 @@ module.exports.init = function init(options) {
sourceMap = generator.toJSON();

} else if (fileType === '.css') {
debug('css');
var ast = css.parse(fileContent, {silent: true});
debug(() => { return ast;});
var registerTokens = function(ast) {
if (ast.position) {
generator.addMapping({
Expand All @@ -149,11 +156,19 @@ module.exports.init = function init(options) {
source: source,
});
}

function logAst(key, ast) {
debug(() => { return 'key: ' + key;});
debug(() => { return ast[key];});
}

for (var key in ast) {
logAst(key, ast);
if (key !== "position") {
if (Object.prototype.toString.call(ast[key]) === '[object Object]') {
registerTokens(ast[key]);
} else if (ast[key].constructor === Array) {
} else if (Array.isArray(ast[key])) {
debug(() => { return "@@@@ ast[key] isArray @@@@";});
for (var i = 0; i < ast[key].length; i++) {
registerTokens(ast[key][i]);
}
Expand Down Expand Up @@ -195,6 +210,8 @@ module.exports.init = function init(options) {
*
*/
module.exports.write = function write(destPath, options) {
var debug = require('debug-fabulous')()(PLUGIN_NAME + ':write');

if (options === undefined && Object.prototype.toString.call(destPath) === '[object Object]') {
options = destPath;
destPath = undefined;
Expand All @@ -209,6 +226,7 @@ module.exports.write = function write(destPath, options) {
if (options.charset === undefined)
options.charset = "utf8";

debug(()=> {return options;});
function sourceMapWrite(file, encoding, callback) {
/*jshint validthis:true */

Expand Down Expand Up @@ -253,11 +271,11 @@ module.exports.write = function write(destPath, options) {
var sourcePath = path.resolve(sourceMap.sourceRoot || file.base, sourceMap.sources[i]);
try {
if (options.debug)
console.log(PLUGIN_NAME + '-write: No source content for "' + sourceMap.sources[i] + '". Loading from file.');
debug('No source content for "' + sourceMap.sources[i] + '". Loading from file.');
sourceMap.sourcesContent[i] = stripBom(fs.readFileSync(sourcePath, 'utf8'));
} catch (e) {
if (options.debug)
console.warn(PLUGIN_NAME + '-write: source file not found: ' + sourcePath);
debug('source file not found: ' + sourcePath);
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"acorn": "4.X",
"convert-source-map": "1.X",
"css": "2.X",
"debug-fabulous": "0.0.X",
"detect-newline": "2.X",
"graceful-fs": "4.X",
"source-map": "0.X",
Expand All @@ -31,11 +32,12 @@
"vinyl": "1.X"
},
"devDependencies": {
"jshint": "2.X",
"tape": "4.X",
"istanbul": "0.X",
"coveralls": "2.X",
"faucet": "0.0.X",
"coveralls": "2.X"
"hook-std": "0.2.X",
"istanbul": "0.X",
"jshint": "2.X",
"tape": "4.X"
},
"files": [
"index.js"
Expand Down
21 changes: 0 additions & 21 deletions test/consolerecorder.js

This file was deleted.

81 changes: 64 additions & 17 deletions test/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ var File = require('vinyl');
var ReadableStream = require('stream').Readable;
var path = require('path');
var fs = require('fs');
var recordConsole = require('./consolerecorder.js');
var hookStd = require('hook-std');
var debug = require('debug-fabulous')();

var sourceContent = fs.readFileSync(path.join(__dirname, 'assets/helloworld.js')).toString();
var sourceContentCSS = fs.readFileSync(path.join(__dirname, 'assets/test.css')).toString();
Expand All @@ -20,6 +21,20 @@ function makeFile() {
});
}

function makeNullFile() {
var junkBuffer = new Buffer([]);
junkBuffer.toString = function(){
return null;
};

return new File({
cwd: __dirname,
base: path.join(__dirname, 'assets'),
path: path.join(__dirname, 'assets', 'helloworld.js'),
contents: junkBuffer
});
}

function makeStreamFile() {
return new File({
cwd: __dirname,
Expand Down Expand Up @@ -334,22 +349,6 @@ test('init: should not load source content if the path is a url', function(t) {
.write(file);
});

test('init: should output an error message if debug option is set and sourceContent is missing', function(t) {
var file = makeFile();
file.contents = new Buffer(sourceContent + '\n//# sourceMappingURL=helloworld4.js.map');

var hConsole = recordConsole();
var pipeline = sourcemaps.init({loadMaps: true, debug: true});
pipeline
.on('data', function(data) {
hConsole.restore();
t.equal(hConsole.history.log[0], 'gulp-sourcemap-init: No source content for "missingfile". Loading from file.', 'should log missing source content');
t.ok(hConsole.history.warn[0].indexOf('gulp-sourcemap-init: source file not found: ') === 0, 'should warn about missing file');
t.end();
})
.write(file);
});

test('init: should pass through when file already has a source map', function(t) {
var sourceMap = {
version : 3,
Expand All @@ -376,3 +375,51 @@ test('init: should pass through when file already has a source map', function(t)
})
.write(file);
});


test('init: handle null contents', function(t) {
var pipeline = sourcemaps.init({addComment:true});
pipeline
.on('data', function(data) {
t.ok(data, 'should pass something through');
t.ok(data instanceof File, 'should pass a vinyl file through');
t.ok(data.sourceMap, 'should add a source map object');
t.equal(String(data.sourceMap.version), '3', 'should have version 3');
t.equal(data.sourceMap.sources[0], 'helloworld.js', 'should add file to sources');
t.equal(data.sourceMap.sourcesContent[0], null, 'should add file content to sourcesContent');
t.deepEqual(data.sourceMap.names, [], 'should add empty names');
t.equal(data.sourceMap.mappings, '', 'should add empty mappings');
t.end();
})
.on('error', function() {
t.fail('emitted error');
t.end();
})
.write(makeNullFile());
});

//should always be last as disabling a debug namespace does not work
test('init: should output an error message if debug option is set and sourceContent is missing', function(t) {
debug.save('gulp-sourcemap:init');
debug.enable(debug.load());

var file = makeFile();
file.contents = new Buffer(sourceContent + '\n//# sourceMappingURL=helloworld4.js.map');

var history = [];
console.log('HOOKING');
var unhook = hookStd.stderr((s) => history.push(s));
var pipeline = sourcemaps.init({loadMaps: true, debug: true});

pipeline
.on('data', function() {
unhook();
debug.save(null);
t.ok(history.length == 4, 'history len');
t.ok(history[2].match(/No source content for \"missingfile\". Loading from file./), 'should log missing source content');
t.ok(history[3].match(/source file not found: /), 'should warn about missing file');
t.end();
})
.write(file);

});
44 changes: 26 additions & 18 deletions test/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ var File = require('vinyl');
var ReadableStream = require('stream').Readable;
var path = require('path');
var fs = require('fs');
var recordConsole = require('./consolerecorder.js');
var hookStd = require('hook-std');
var debug = require('debug-fabulous')();

var sourceContent = fs.readFileSync(path.join(__dirname, 'assets/helloworld.js')).toString();

Expand Down Expand Up @@ -554,23 +555,6 @@ test('write: should invoke sourceMappingURLPrefix every time', function(t) {
.write(makeFile());
});

test('write: should output an error message if debug option is set and sourceContent is missing', function(t) {
var file = makeFile();
file.sourceMap.sources[0] += '.invalid';
delete file.sourceMap.sourcesContent;

var hConsole = recordConsole();
var pipeline = sourcemaps.write({debug: true});
pipeline
.on('data', function(data) {
hConsole.restore();
t.equal(hConsole.history.log[0], 'gulp-sourcemap-write: No source content for "helloworld.js.invalid". Loading from file.', 'should log missing source content');
t.ok(hConsole.history.warn[0].indexOf('gulp-sourcemap-write: source file not found: ') === 0, 'should warn about missing file');
t.end();
})
.write(file);
});

test('write: null as sourceRoot should not set the sourceRoot', function(t) {
var file = makeFile();
var pipeline = sourcemaps.write({sourceRoot: null});
Expand Down Expand Up @@ -641,3 +625,27 @@ test('write: should allow to change sources', function(t) {
})
.write(file);
});

//should always be last as disabling a debug namespace does not work
test('write: should output an error message if debug option is set and sourceContent is missing', function(t) {
debug.save('gulp-sourcemap:write');
debug.enable(debug.load());
var file = makeFile();
file.sourceMap.sources[0] += '.invalid';
delete file.sourceMap.sourcesContent;

var history = [];
var unhook = hookStd.stderr(s => history.push(s));
var pipeline = sourcemaps.write({debug: true});
pipeline
.on('data', function(data) {
unhook();
debug.save(null);
// console.log(JSON.stringify(history))
t.ok(history.length == 3, 'history len');
t.ok(history[1].match(/No source content for "helloworld.js.invalid". Loading from file./), 'should log missing source content');
t.ok(history[2].match(/source file not found: /), 'should warn about missing file');
t.end();
})
.write(file);
});

0 comments on commit 096f882

Please sign in to comment.