react / wstein / node_modules / jest-cli / node_modules / istanbul / node_modules / handlebars / lib / handlebars / compiler / code-gen.js
80713 viewsimport {isArray} from "../utils";12try {3var SourceMap = require('source-map'),4SourceNode = SourceMap.SourceNode;5} catch (err) {6/* istanbul ignore next: tested but not covered in istanbul due to dist build */7SourceNode = function(line, column, srcFile, chunks) {8this.src = '';9if (chunks) {10this.add(chunks);11}12};13/* istanbul ignore next */14SourceNode.prototype = {15add: function(chunks) {16if (isArray(chunks)) {17chunks = chunks.join('');18}19this.src += chunks;20},21prepend: function(chunks) {22if (isArray(chunks)) {23chunks = chunks.join('');24}25this.src = chunks + this.src;26},27toStringWithSourceMap: function() {28return {code: this.toString()};29},30toString: function() {31return this.src;32}33};34}353637function castChunk(chunk, codeGen, loc) {38if (isArray(chunk)) {39var ret = [];4041for (var i = 0, len = chunk.length; i < len; i++) {42ret.push(codeGen.wrap(chunk[i], loc));43}44return ret;45} else if (typeof chunk === 'boolean' || typeof chunk === 'number') {46// Handle primitives that the SourceNode will throw up on47return chunk+'';48}49return chunk;50}515253function CodeGen(srcFile) {54this.srcFile = srcFile;55this.source = [];56}5758CodeGen.prototype = {59prepend: function(source, loc) {60this.source.unshift(this.wrap(source, loc));61},62push: function(source, loc) {63this.source.push(this.wrap(source, loc));64},6566merge: function() {67var source = this.empty();68this.each(function(line) {69source.add([' ', line, '\n']);70});71return source;72},7374each: function(iter) {75for (var i = 0, len = this.source.length; i < len; i++) {76iter(this.source[i]);77}78},7980empty: function(loc) {81loc = loc || this.currentLocation || {start:{}};82return new SourceNode(loc.start.line, loc.start.column, this.srcFile);83},84wrap: function(chunk, loc) {85if (chunk instanceof SourceNode) {86return chunk;87}8889loc = loc || this.currentLocation || {start:{}};90chunk = castChunk(chunk, this, loc);9192return new SourceNode(loc.start.line, loc.start.column, this.srcFile, chunk);93},9495functionCall: function(fn, type, params) {96params = this.generateList(params);97return this.wrap([fn, type ? '.' + type + '(' : '(', params, ')']);98},99100quotedString: function(str) {101return '"' + (str + '')102.replace(/\\/g, '\\\\')103.replace(/"/g, '\\"')104.replace(/\n/g, '\\n')105.replace(/\r/g, '\\r')106.replace(/\u2028/g, '\\u2028') // Per Ecma-262 7.3 + 7.8.4107.replace(/\u2029/g, '\\u2029') + '"';108},109110objectLiteral: function(obj) {111var pairs = [];112113for (var key in obj) {114if (obj.hasOwnProperty(key)) {115var value = castChunk(obj[key], this);116if (value !== 'undefined') {117pairs.push([this.quotedString(key), ':', value]);118}119}120}121122var ret = this.generateList(pairs);123ret.prepend('{');124ret.add('}');125return ret;126},127128129generateList: function(entries, loc) {130var ret = this.empty(loc);131132for (var i = 0, len = entries.length; i < len; i++) {133if (i) {134ret.add(',');135}136137ret.add(castChunk(entries[i], this, loc));138}139140return ret;141},142143generateArray: function(entries, loc) {144var ret = this.generateList(entries, loc);145ret.prepend('[');146ret.add(']');147148return ret;149}150};151152export default CodeGen;153154155156