123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- /**
- * Copyright 2013-present, Facebook, Inc.
- * All rights reserved.
- *
- * This source code is licensed under the BSD-style license found in the
- * LICENSE file in the root directory of this source tree. An additional grant
- * of patent rights can be found in the PATENTS file in the same directory.
- */
- /*jslint node:true*/
- /**
- * @typechecks
- */
- 'use strict';
- var Syntax = require('esprima-fb').Syntax;
- var utils = require('../src/utils');
- /**
- * http://people.mozilla.org/~jorendorff/es6-draft.html#sec-12.1.9
- */
- function visitTemplateLiteral(traverse, node, path, state) {
- var templateElements = node.quasis;
- utils.append('(', state);
- for (var ii = 0; ii < templateElements.length; ii++) {
- var templateElement = templateElements[ii];
- if (templateElement.value.raw !== '') {
- utils.append(getCookedValue(templateElement), state);
- if (!templateElement.tail) {
- // + between element and substitution
- utils.append(' + ', state);
- }
- // maintain line numbers
- utils.move(templateElement.range[0], state);
- utils.catchupNewlines(templateElement.range[1], state);
- } else { // templateElement.value.raw === ''
- // Concatenat adjacent substitutions, e.g. `${x}${y}`. Empty templates
- // appear before the first and after the last element - nothing to add in
- // those cases.
- if (ii === 0) {
- utils.append('"" + ', state);
- }
- if (ii > 0 && !templateElement.tail) {
- // + between substitution and substitution
- utils.append(' + ', state);
- }
- }
- utils.move(templateElement.range[1], state);
- if (!templateElement.tail) {
- var substitution = node.expressions[ii];
- if (substitution.type === Syntax.Identifier ||
- substitution.type === Syntax.Literal ||
- substitution.type === Syntax.MemberExpression ||
- substitution.type === Syntax.CallExpression) {
- utils.catchup(substitution.range[1], state);
- } else {
- utils.append('(', state);
- traverse(substitution, path, state);
- utils.catchup(substitution.range[1], state);
- utils.append(')', state);
- }
- // if next templateElement isn't empty...
- if (templateElements[ii + 1].value.cooked !== '') {
- utils.append(' + ', state);
- }
- }
- }
- utils.move(node.range[1], state);
- utils.append(')', state);
- return false;
- }
- visitTemplateLiteral.test = function(node, path, state) {
- return node.type === Syntax.TemplateLiteral;
- };
- /**
- * http://people.mozilla.org/~jorendorff/es6-draft.html#sec-12.2.6
- */
- function visitTaggedTemplateExpression(traverse, node, path, state) {
- var template = node.quasi;
- var numQuasis = template.quasis.length;
- // print the tag
- utils.move(node.tag.range[0], state);
- traverse(node.tag, path, state);
- utils.catchup(node.tag.range[1], state);
- // print array of template elements
- utils.append('(function() { var siteObj = [', state);
- for (var ii = 0; ii < numQuasis; ii++) {
- utils.append(getCookedValue(template.quasis[ii]), state);
- if (ii !== numQuasis - 1) {
- utils.append(', ', state);
- }
- }
- utils.append(']; siteObj.raw = [', state);
- for (ii = 0; ii < numQuasis; ii++) {
- utils.append(getRawValue(template.quasis[ii]), state);
- if (ii !== numQuasis - 1) {
- utils.append(', ', state);
- }
- }
- utils.append(
- ']; Object.freeze(siteObj.raw); Object.freeze(siteObj); return siteObj; }()',
- state
- );
- // print substitutions
- if (numQuasis > 1) {
- for (ii = 0; ii < template.expressions.length; ii++) {
- var expression = template.expressions[ii];
- utils.append(', ', state);
- // maintain line numbers by calling catchupWhiteSpace over the whole
- // previous TemplateElement
- utils.move(template.quasis[ii].range[0], state);
- utils.catchupNewlines(template.quasis[ii].range[1], state);
- utils.move(expression.range[0], state);
- traverse(expression, path, state);
- utils.catchup(expression.range[1], state);
- }
- }
- // print blank lines to push the closing ) down to account for the final
- // TemplateElement.
- utils.catchupNewlines(node.range[1], state);
- utils.append(')', state);
- return false;
- }
- visitTaggedTemplateExpression.test = function(node, path, state) {
- return node.type === Syntax.TaggedTemplateExpression;
- };
- function getCookedValue(templateElement) {
- return JSON.stringify(templateElement.value.cooked);
- }
- function getRawValue(templateElement) {
- return JSON.stringify(templateElement.value.raw);
- }
- exports.visitorList = [
- visitTemplateLiteral,
- visitTaggedTemplateExpression
- ];
|