Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

es6-templates

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

es6-templates - npm Package Compare versions

Comparing version 0.2.0 to 0.2.1

lib/visitor.js

2

bower.json
{
"name": "es6-templates",
"version": "0.2.0",
"version": "0.2.1",
"homepage": "https://github.com/square/es6-templates",

@@ -5,0 +5,0 @@ "authors": [

@@ -1,105 +0,7 @@

var assert = require('assert');
var through = require('through');
var Visitor = require('./visitor');
var recast = require('recast');
var types = recast.types;
var PathVisitor = types.PathVisitor;
var n = types.namedTypes;
var b = types.builders;
function Visitor() {
PathVisitor.apply(this, arguments);
}
Visitor.prototype = Object.create(PathVisitor.prototype);
Visitor.prototype.constructor = Visitor;
/**
* Visits a template literal, replacing it with a series of string
* concatenations. For example, given:
*
* ```js
* `1 + 1 = ${1 + 1}`
* ```
*
* The following output will be generated:
*
* ```js
* "1 + 1 = " + (1 + 1)
* ```
*
* @param {NodePath} path
* @returns {AST.Literal|AST.BinaryExpression}
*/
Visitor.prototype.visitTemplateLiteral = function(path) {
var node = path.node;
var replacement = b.literal(node.quasis[0].value.cooked);
for (var i = 1, length = node.quasis.length; i < length; i++) {
replacement = b.binaryExpression(
'+',
b.binaryExpression(
'+',
replacement,
node.expressions[i - 1]
),
b.literal(node.quasis[i].value.cooked)
);
}
return replacement;
};
/**
* Visits the path wrapping a TaggedTemplateExpression node, which has the form
*
* ```js
* htmlEncode `<span id=${id}>${text}</span>`
* ```
*
* @param {NodePath} path
* @returns {AST.CallExpression}
*/
Visitor.prototype.visitTaggedTemplateExpression = function(path) {
var node = path.node;
var args = [];
var strings = b.callExpression(
b.functionExpression(
null,
[],
b.blockStatement([
b.variableDeclaration(
'var',
[
b.variableDeclarator(
b.identifier('strings'),
b.arrayExpression(node.quasi.quasis.map(function(quasi) {
return b.literal(quasi.value.cooked);
}))
)
]
),
b.expressionStatement(b.assignmentExpression(
'=',
b.memberExpression(b.identifier('strings'), b.identifier('raw'), false),
b.arrayExpression(node.quasi.quasis.map(function(quasi) {
return b.literal(quasi.value.raw);
}))
)),
b.returnStatement(b.identifier('strings'))
])
),
[]
);
args.push(strings);
args.push.apply(args, node.quasi.expressions);
return b.callExpression(
node.tag,
args
);
};
var VISITOR = new Visitor();
/**
* Transform an Esprima AST generated from ES6 by replacing all template string

@@ -118,3 +20,3 @@ * nodes with the equivalent ES5.

function transform(ast) {
return types.visit(ast, VISITOR);
return types.visit(ast, Visitor.visitor);
}

@@ -121,0 +23,0 @@

{
"name": "es6-templates",
"version": "0.2.0",
"version": "0.2.1",
"description": "ES6 template strings compiled to ES5.",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc