Skip to content

Commit

Permalink
indenting uniformity via jsbeautifier
Browse files Browse the repository at this point in the history
  • Loading branch information
nmccready committed Oct 13, 2016
1 parent 5e97199 commit 8db077c
Show file tree
Hide file tree
Showing 6 changed files with 835 additions and 902 deletions.
10 changes: 10 additions & 0 deletions .jsbeautifyrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"js":{
"indent_level": 0,
"indent_with_tabs": false,
"preserve_newlines": true,
"max_preserve_newlines": 2,
"jslint_happy": true,
"indent_handlebars": true
}
}
45 changes: 26 additions & 19 deletions src/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ var utils = require('./utils'),
SourceMapGenerator = require('source-map').SourceMapGenerator,
css = require('css');


/**
* Initialize source mapping chain
*/
Expand All @@ -33,9 +32,11 @@ function init(options) {
}

if (options === undefined) {
options = {};
options = {};
}
debug(function () {return options;});
debug(function() {
return options;
});

var fileContent = file.contents.toString();
var sourceMap;
Expand All @@ -61,7 +62,7 @@ function init(options) {
if (mapComment) {
mapFile = path.resolve(path.dirname(file.path), mapComment[1] || mapComment[2]);
fileContent = convert.removeMapFileComments(fileContent);
// if no comment try map file with same name as source file
// if no comment try map file with same name as source file
} else {
mapFile = file.path + '.map';
}
Expand All @@ -71,7 +72,7 @@ function init(options) {

try {
sourceMap = JSON.parse(stripBom(fs.readFileSync(mapFile, 'utf8')));
} catch(e) {}
} catch (e) {}
}

// fix source paths and sourceContent for imported source map
Expand Down Expand Up @@ -99,7 +100,7 @@ function init(options) {
if (absPath === file.path) {
sourceContent = fileContent;

// else load content from file
// else load content from file
} else {
try {
if (options.debug)
Expand All @@ -108,8 +109,8 @@ function init(options) {
} catch (e) {
if (options.debug)
debug('warn: source file not found: ' + absPath);
}
}
}
sourceMap.sourcesContent[i] = sourceContent;
}
});
Expand All @@ -120,7 +121,9 @@ function init(options) {
}

if (!sourceMap && options.identityMap) {
debug(function () { return 'identityMap'; });
debug(function() {
return 'identityMap';
});
var fileType = path.extname(file.path);
var source = unixStylePath(file.relative);
var generator = new SourceMapGenerator({file: source});
Expand All @@ -134,7 +137,7 @@ function init(options) {
var mapping = {
original: token.loc.start,
generated: token.loc.start,
source: source,
source: source
};
if (token.type.label === 'name') {
mapping.name = token.value;
Expand All @@ -147,19 +150,21 @@ function init(options) {
} else if (fileType === '.css') {
debug('css');
var ast = css.parse(fileContent, {silent: true});
debug(function () { return ast;});
debug(function() {
return ast;
});
var registerTokens = function(ast) {
if (ast.position) {
generator.addMapping({
original: ast.position.start,
generated: ast.position.start,
source: source,
});
generator.addMapping({original: ast.position.start, generated: ast.position.start, source: source});
}

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

for (var key in ast) {
Expand All @@ -168,7 +173,9 @@ function init(options) {
if (Object.prototype.toString.call(ast[key]) === '[object Object]') {
registerTokens(ast[key]);
} else if (Array.isArray(ast[key])) {
debug(function () { return "@@@@ ast[key] isArray @@@@";});
debug(function() {
return "@@@@ ast[key] isArray @@@@";
});
for (var i = 0; i < ast[key].length; i++) {
registerTokens(ast[key][i]);
}
Expand All @@ -185,7 +192,7 @@ function init(options) {
if (!sourceMap) {
// Make an empty source map
sourceMap = {
version : 3,
version: 3,
names: [],
mappings: '',
sources: [unixStylePath(file.relative)],
Expand Down
4 changes: 0 additions & 4 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
'use strict';
var path = require('path');


function unixStylePath(filePath) {
return filePath.split(path.sep).join('/');
}


var PLUGIN_NAME = require('../package.json').name;


var urlRegex = /^(https?|webpack(-[^:]+)?):\/\//;


module.exports = {
unixStylePath: unixStylePath,
PLUGIN_NAME: PLUGIN_NAME,
Expand Down
44 changes: 31 additions & 13 deletions src/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ var utils = require('./utils'),
stripBom = require('strip-bom'),
detectNewline = require('detect-newline');


/**
* Write the source map
*
Expand All @@ -33,7 +32,10 @@ function write(destPath, options) {
if (options.charset === undefined)
options.charset = "utf8";

debug(function() {return options;});
debug(function() {
return options;
});

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

Expand Down Expand Up @@ -66,7 +68,7 @@ function write(destPath, options) {
sourceMap.sourceRoot = options.sourceRoot;
}
if (sourceMap.sourceRoot === null) {
sourceMap.sourceRoot = undefined;
sourceMap.sourceRoot = undefined;
}

if (options.includeContent) {
Expand All @@ -83,8 +85,8 @@ function write(destPath, options) {
} catch (e) {
if (options.debug)
debug('source file not found: ' + sourcePath);
}
}
}
}
} else {
delete sourceMap.sourcesContent;
Expand All @@ -107,7 +109,9 @@ function write(destPath, options) {
break;
default:
/* jshint ignore:start */
commentFormatter = function(url) { return ""; };
commentFormatter = function(url) {
return "";
};
/* jshint ignore:end */
}

Expand Down Expand Up @@ -151,13 +155,27 @@ function write(destPath, options) {
path: sourceMapPath,
contents: new Buffer(JSON.stringify(sourceMap)),
stat: {
isFile: function () { return true; },
isDirectory: function () { return false; },
isBlockDevice: function () { return false; },
isCharacterDevice: function () { return false; },
isSymbolicLink: function () { return false; },
isFIFO: function () { return false; },
isSocket: function () { return false; }
isFile: function() {
return true;
},
isDirectory: function() {
return false;
},
isBlockDevice: function() {
return false;
},
isCharacterDevice: function() {
return false;
},
isSymbolicLink: function() {
return false;
},
isFIFO: function() {
return false;
},
isSocket: function() {
return false;
}
}
});
this.push(sourceMapFile);
Expand All @@ -171,7 +189,7 @@ function write(destPath, options) {
} else {
prefix = options.sourceMappingURLPrefix;
}
sourceMapPathRelative = prefix+path.join('/', sourceMapPathRelative);
sourceMapPathRelative = prefix + path.join('/', sourceMapPathRelative);
}
comment = commentFormatter(unixStylePath(sourceMapPathRelative));

Expand Down
Loading

0 comments on commit 8db077c

Please sign in to comment.