react / wstein / node_modules / react / node_modules / envify / node_modules / jstransform / visitors / es6-template-visitors.js
80540 views/**1* Copyright 2013 Facebook, Inc.2*3* Licensed under the Apache License, Version 2.0 (the "License");4* you may not use this file except in compliance with the License.5* You may obtain a copy of the License at6*7* http://www.apache.org/licenses/LICENSE-2.08*9* Unless required by applicable law or agreed to in writing, software10* distributed under the License is distributed on an "AS IS" BASIS,11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12* See the License for the specific language governing permissions and13* limitations under the License.14*/1516/*jslint node:true*/1718/**19* @typechecks20*/21'use strict';2223var Syntax = require('esprima-fb').Syntax;24var utils = require('../src/utils');2526/**27* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-12.1.928*/29function visitTemplateLiteral(traverse, node, path, state) {30var templateElements = node.quasis;3132utils.append('(', state);33for (var ii = 0; ii < templateElements.length; ii++) {34var templateElement = templateElements[ii];35if (templateElement.value.raw !== '') {36utils.append(getCookedValue(templateElement), state);37if (!templateElement.tail) {38// + between element and substitution39utils.append(' + ', state);40}41// maintain line numbers42utils.move(templateElement.range[0], state);43utils.catchupNewlines(templateElement.range[1], state);44} else { // templateElement.value.raw === ''45// Concatenat adjacent substitutions, e.g. `${x}${y}`. Empty templates46// appear before the first and after the last element - nothing to add in47// those cases.48if (ii > 0 && !templateElement.tail) {49// + between substitution and substitution50utils.append(' + ', state);51}52}5354utils.move(templateElement.range[1], state);55if (!templateElement.tail) {56var substitution = node.expressions[ii];57if (substitution.type === Syntax.Identifier ||58substitution.type === Syntax.MemberExpression ||59substitution.type === Syntax.CallExpression) {60utils.catchup(substitution.range[1], state);61} else {62utils.append('(', state);63traverse(substitution, path, state);64utils.catchup(substitution.range[1], state);65utils.append(')', state);66}67// if next templateElement isn't empty...68if (templateElements[ii + 1].value.cooked !== '') {69utils.append(' + ', state);70}71}72}73utils.move(node.range[1], state);74utils.append(')', state);75return false;76}7778visitTemplateLiteral.test = function(node, path, state) {79return node.type === Syntax.TemplateLiteral;80};8182/**83* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-12.2.684*/85function visitTaggedTemplateExpression(traverse, node, path, state) {86var template = node.quasi;87var numQuasis = template.quasis.length;8889// print the tag90utils.move(node.tag.range[0], state);91traverse(node.tag, path, state);92utils.catchup(node.tag.range[1], state);9394// print array of template elements95utils.append('(function() { var siteObj = [', state);96for (var ii = 0; ii < numQuasis; ii++) {97utils.append(getCookedValue(template.quasis[ii]), state);98if (ii !== numQuasis - 1) {99utils.append(', ', state);100}101}102utils.append(']; siteObj.raw = [', state);103for (ii = 0; ii < numQuasis; ii++) {104utils.append(getRawValue(template.quasis[ii]), state);105if (ii !== numQuasis - 1) {106utils.append(', ', state);107}108}109utils.append(110']; Object.freeze(siteObj.raw); Object.freeze(siteObj); return siteObj; }()',111state112);113114// print substitutions115if (numQuasis > 1) {116for (ii = 0; ii < template.expressions.length; ii++) {117var expression = template.expressions[ii];118utils.append(', ', state);119120// maintain line numbers by calling catchupWhiteSpace over the whole121// previous TemplateElement122utils.move(template.quasis[ii].range[0], state);123utils.catchupNewlines(template.quasis[ii].range[1], state);124125utils.move(expression.range[0], state);126traverse(expression, path, state);127utils.catchup(expression.range[1], state);128}129}130131// print blank lines to push the closing ) down to account for the final132// TemplateElement.133utils.catchupNewlines(node.range[1], state);134135utils.append(')', state);136137return false;138}139140visitTaggedTemplateExpression.test = function(node, path, state) {141return node.type === Syntax.TaggedTemplateExpression;142};143144function getCookedValue(templateElement) {145return JSON.stringify(templateElement.value.cooked);146}147148function getRawValue(templateElement) {149return JSON.stringify(templateElement.value.raw);150}151152exports.visitorList = [153visitTemplateLiteral,154visitTaggedTemplateExpression155];156157158