Initial commit

This commit is contained in:
Developer
2025-04-21 16:03:20 +02:00
commit 2832896157
22874 changed files with 3092801 additions and 0 deletions

View File

@@ -0,0 +1,2 @@
node_modules/
test/results

View File

@@ -0,0 +1,4 @@
language: node_js
node_js:
- "0.11"
- "0.10"

View File

@@ -0,0 +1,13 @@
Copyright 2014 Square Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

View File

@@ -0,0 +1,125 @@
# es6-templates
Compiles JavaScript written using template strings to use ES5-compatible
syntax. For example, this:
```js
var name = "Nicholas",
msg = `Hello, ${name}!`;
console.log(msg); // "Hello, Nicholas!"
```
compiles to this:
```js
var name = "Nicholas",
msg = "Hello, " + name + "!";
console.log(msg); // "Hello, Nicholas!"
```
For more information about the proposed syntax, see the [TC39 wiki page on
template strings](http://tc39wiki.calculist.org/es6/template-strings/).
## Install
```
$ npm install es6-templates
```
## Usage
```js
$ node
> var compile = require('es6-templates').compile;
> compile('`Hey, ${name}!`')
{ 'code': ..., 'map': ... }
```
Without interpolation:
```js
`Hey!`
// becomes
'"Hey!"'
```
With interpolation:
```js
`Hey, ${name}!`
// becomes
"Hey, " + name + "!"
```
With a tag expression:
```js
escape `<a href="${href}">${text}</a>`
// becomes
escape(function() {
var strings = ["\u003Ca href=\"", "\"\u003E", "\u003C/a\u003E"];
strings.raw = ["\u003Ca href=\"", "\"\u003E", "\u003C/a\u003E"];
return strings;
}(), href, text);
```
Or work directly with the AST:
```js
$ node
> var transform = require('es6-templates').transform;
> transform(inputAST)
```
Transforming ASTs is best done using [recast][recast] to preserve formatting
where possible and for generating source maps.
## Browserify
Browserify support is built in.
```
$ npm install es6-templates # install local dependency
$ browserify -t es6-templates $file
```
## Contributing
[![Build Status](https://travis-ci.org/esnext/es6-templates.svg?branch=master)](https://travis-ci.org/esnext/es6-templates)
### Setup
First, install the development dependencies:
```
$ npm install
```
Then, try running the tests:
```
$ npm test
```
### Pull Requests
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request
Any contributors to the master es6-templates repository must sign the
[Individual Contributor License Agreement (CLA)][cla]. It's a short form that
covers our bases and makes sure you're eligible to contribute.
[cla]: https://spreadsheets.google.com/spreadsheet/viewform?formkey=dDViT2xzUHAwRkI3X3k5Z0lQM091OGc6MQ&ndplr=1
When you have a change you'd like to see in the master repository, [send a pull
request](https://github.com/esnext/es6-templates/pulls). Before we merge
your request, we'll make sure you're in the list of people who have signed a
CLA.
[recast]: https://github.com/benjamn/recast

View File

@@ -0,0 +1,23 @@
{
"name": "es6-templates",
"version": "0.2.3",
"homepage": "https://github.com/square/es6-templates",
"authors": [
"Brian Donovan <donovan@squareup.com>"
],
"description": "ES6 template strings compiled to ES5.",
"main": "lib/index.js",
"keywords": [
"es6",
"template string",
"string",
"interpolation",
"tagged template"
],
"license": "Apache 2",
"ignore": [
"**/.*",
"node_modules",
"test"
]
}

View File

@@ -0,0 +1,57 @@
var through = require('through');
var Visitor = require('./visitor');
var recast = require('recast');
var types = recast.types;
/**
* Transform an Esprima AST generated from ES6 by replacing all template string
* nodes with the equivalent ES5.
*
* NOTE: The argument may be modified by this function. To prevent modification
* of your AST, pass a copy instead of a direct reference:
*
* // instead of transform(ast), pass a copy
* transform(JSON.parse(JSON.stringify(ast));
*
* @param {Object} ast
* @return {Object}
*/
function transform(ast) {
return types.visit(ast, Visitor.visitor);
}
/**
* Transform JavaScript written using ES6 by replacing all template string
* usages with the equivalent ES5.
*
* compile('`Hey, ${name}!'); // '"Hey, " + name + "!"'
*
* @param {string} source
* @param {{sourceFileName: string, sourceMapName: string}} mapOptions
* @return {string}
*/
function compile(source, mapOptions) {
mapOptions = mapOptions || {};
var recastOptions = {
sourceFileName: mapOptions.sourceFileName,
sourceMapName: mapOptions.sourceMapName
};
var ast = recast.parse(source, recastOptions);
return recast.print(transform(ast), recastOptions);
}
module.exports = function() {
var data = '';
return through(write, end);
function write(buf) { data += buf; }
function end() {
this.queue(module.exports.compile(data).code);
this.queue(null);
}
};
module.exports.compile = compile;
module.exports.transform = transform;

View File

@@ -0,0 +1,102 @@
var assert = require('assert');
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
);
};
Visitor.visitor = new Visitor();
module.exports = Visitor;

View File

@@ -0,0 +1,56 @@
{
"_from": "es6-templates@^0.2.2",
"_id": "es6-templates@0.2.3",
"_inBundle": false,
"_integrity": "sha1-XLmsn7He1usSOTQrgdeSu7QHjuQ=",
"_location": "/es6-templates",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "es6-templates@^0.2.2",
"name": "es6-templates",
"escapedName": "es6-templates",
"rawSpec": "^0.2.2",
"saveSpec": null,
"fetchSpec": "^0.2.2"
},
"_requiredBy": [
"/html-loader"
],
"_resolved": "https://registry.npmjs.org/es6-templates/-/es6-templates-0.2.3.tgz",
"_shasum": "5cb9ac9fb1ded6eb1239342b81d792bbb4078ee4",
"_spec": "es6-templates@^0.2.2",
"_where": "D:\\developments\\teaser-inertia\\nova-components\\NovaLeader\\node_modules\\html-loader",
"author": {
"name": "Square, Inc."
},
"bugs": {
"url": "https://github.com/esnext/es6-templates/issues"
},
"bundleDependencies": false,
"dependencies": {
"recast": "~0.11.12",
"through": "~2.3.6"
},
"deprecated": false,
"description": "ES6 template strings compiled to ES5.",
"devDependencies": {
"example-runner": "~0.2.0"
},
"directories": {
"test": "test"
},
"homepage": "https://github.com/esnext/es6-templates#readme",
"license": "Apache 2",
"main": "lib/index.js",
"name": "es6-templates",
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/esnext/es6-templates.git"
},
"scripts": {
"test": "node test/runner.js"
},
"version": "0.2.3"
}

View File

@@ -0,0 +1,4 @@
var s = `a
b
c`;
assert.equal(s, 'a\n b\n c');

View File

@@ -0,0 +1,6 @@
/* jshint esnext:true */
assert.strictEqual(
`a${1}b${`${1+1}c`}3`,
'a1b2c3'
);

View File

@@ -0,0 +1,2 @@
var s = `str`;
assert.equal(s, 'str');

View File

@@ -0,0 +1,6 @@
function r(strings) {
assert.equal(strings.raw[0], '\\n');
return strings.raw.join('');
}
assert.equal(r `\n`, '\\n');

View File

@@ -0,0 +1,2 @@
var s = `1 + 1 = ${1 + 1}`;
assert.equal(s, '1 + 1 = 2');

View File

@@ -0,0 +1,26 @@
function tag(strings) {
var values = [].slice.call(arguments, 1);
assert.equal(strings[0], 'a');
assert.equal(strings[1], 'b');
assert.equal(values[0], 42);
return 'whatever';
}
assert.equal(tag `a${ 42 }b`, 'whatever');
function tagInterpolateFirst(strings) {
var values = [].slice.call(arguments, 1);
assert.equal(strings[0], '');
assert.equal(strings[1], 'b');
assert.equal(values[0], 42);
return 'whatever';
}
assert.equal(tagInterpolateFirst `${ 42 }b`, 'whatever');
function tagInterpolateLast(strings) {
var values = [].slice.call(arguments, 1);
assert.equal(strings[0], 'a');
assert.equal(strings[1], '');
assert.equal(values[0], 42);
return 'whatever';
}
assert.equal(tagInterpolateLast `a${ 42 }`, 'whatever');

View File

@@ -0,0 +1,27 @@
/**
* We pull in example files from test/examples/*.js. Write your assertions in
* the file alongside the ES6 class "setup" code. The node `assert` library
* will already be in the context.
*/
Error.stackTraceLimit = 20;
var compile = require('../lib').compile;
var fs = require('fs');
var path = require('path');
var RESULTS = 'test/results';
if (!fs.existsSync(RESULTS)) {
fs.mkdirSync(RESULTS);
}
require('example-runner').runCLI(function(source, testName, filename) {
var result = compile(source, {
includeRuntime: true,
sourceFileName: filename,
sourceMapName: filename + '.map'
});
fs.writeFileSync(path.join(RESULTS, testName + '.js'), result.code, 'utf8');
fs.writeFileSync(path.join(RESULTS, testName + '.js.map'), JSON.stringify(result.map), 'utf8');
return result.code;
});