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

Generate Source Maps #902

Merged
merged 4 commits into from
Nov 9, 2014
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
4 changes: 4 additions & 0 deletions bin/handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ var optimist = require('optimist')
'description': 'Output File',
'alias': 'output'
},
'map': {
'type': 'string',
'description': 'Source Map File'
},
'a': {
'type': 'boolean',
'description': 'Exports amd style (require.js)',
Expand Down
151 changes: 151 additions & 0 deletions lib/handlebars/compiler/code-gen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import {isArray} from "../utils";

try {
var SourceMap = require('source-map'),
SourceNode = SourceMap.SourceNode;
} catch (err) {
/* istanbul ignore next: tested but not covered in istanbul due to dist build */
SourceNode = function(line, column, srcFile, chunks) {
this.src = '';
if (chunks) {
this.add(chunks);
}
};
/* istanbul ignore next */
SourceNode.prototype = {
add: function(chunks) {
if (isArray(chunks)) {
chunks = chunks.join('');
}
this.src += chunks;
},
prepend: function(chunks) {
if (isArray(chunks)) {
chunks = chunks.join('');
}
this.src = chunks + this.src;
},
toStringWithSourceMap: function() {
return {code: this.toString()};
},
toString: function() {
return this.src;
}
};
}


function castChunk(chunk, codeGen, loc) {
if (isArray(chunk)) {
var ret = [];

for (var i = 0, len = chunk.length; i < len; i++) {
ret.push(codeGen.wrap(chunk[i], loc));
}
return ret;
} else if (typeof chunk === 'boolean' || typeof chunk === 'number') {
// Handle primitives that the SourceNode will throw up on
return chunk+'';
}
return chunk;
}


function CodeGen(srcFile) {
this.srcFile = srcFile;
this.source = [];
}

CodeGen.prototype = {
prepend: function(source, loc) {
this.source.unshift(this.wrap(source, loc));
},
push: function(source, loc) {
this.source.push(this.wrap(source, loc));
},

merge: function() {
var source = this.empty();
this.each(function(line) {
source.add([' ', line, '\n']);
});
return source;
},

each: function(iter) {
for (var i = 0, len = this.source.length; i < len; i++) {
iter(this.source[i]);
}
},

empty: function(loc) {
loc = loc || this.currentLocation || {};
return new SourceNode(loc.firstLine, loc.firstColumn, this.srcFile);
},
wrap: function(chunk, loc) {
if (chunk instanceof SourceNode) {
return chunk;
}

loc = loc || this.currentLocation || {};
chunk = castChunk(chunk, this, loc);

return new SourceNode(loc.firstLine, loc.firstColumn, this.srcFile, chunk);
},

functionCall: function(fn, type, params) {
params = this.generateList(params);
return this.wrap([fn, type ? '.' + type + '(' : '(', params, ')']);
},

quotedString: function(str) {
return '"' + str
.replace(/\\/g, '\\\\')
.replace(/"/g, '\\"')
.replace(/\n/g, '\\n')
.replace(/\r/g, '\\r')
.replace(/\u2028/g, '\\u2028') // Per Ecma-262 7.3 + 7.8.4
.replace(/\u2029/g, '\\u2029') + '"';
},

objectLiteral: function(obj) {
var pairs = [];

for (var key in obj) {
if (obj.hasOwnProperty(key)) {
pairs.push([this.quotedString(key), ':', castChunk(obj[key], this)]);
}
}

var ret = this.generateList(pairs);
ret.prepend('{');
ret.add('}');
return ret;
},


generateList: function(entries, loc) {
var ret = this.empty(loc);

for (var i = 0, len = entries.length; i < len; i++) {
if (i) {
ret.add(',');
}

ret.add(castChunk(entries[i], this, loc));
}

return ret;
},

generateArray: function(entries, loc) {
var ret = this.generateList(entries, loc);
ret.prepend('[');
ret.add(']');

return ret;
}
};

export default CodeGen;

Loading