babel-plugin-htmlbars-inline-precompile
Advanced tools
Comparing version 0.2.0 to 0.2.1
54
index.js
@@ -34,35 +34,37 @@ 'use strict'; | ||
Identifier: function(path, state) { | ||
if (path.referencesImport('htmlbars-inline-precompile', 'default')) { | ||
let parent = path.parentPath; | ||
TaggedTemplateExpression(path, state) { | ||
let tagPath = path.get('tag'); | ||
if (!tagPath.referencesImport('htmlbars-inline-precompile', 'default')) { | ||
return; | ||
} | ||
let template; | ||
if (parent.isCallExpression({ callee: path.node })) { | ||
let argumentErrorMsg = "hbs should be invoked with a single argument: the template string"; | ||
if (parent.node.arguments.length !== 1) { | ||
throw parent.buildCodeFrameError(argumentErrorMsg); | ||
} | ||
if (path.node.quasi.expressions.length) { | ||
throw path.buildCodeFrameError("placeholders inside a tagged template string are not supported"); | ||
} | ||
template = parent.node.arguments[0].value; | ||
if (typeof template !== "string") { | ||
throw parent.buildCodeFrameError(argumentErrorMsg); | ||
} | ||
let template = path.node.quasi.quasis.map(quasi => quasi.value.cooked).join(''); | ||
let compiledTemplateString = `Ember.HTMLBars.template(${state.opts.precompile(template)})`; | ||
} else if (parent.isTaggedTemplateExpression({ tag: path.node })) { | ||
if (parent.node.quasi.expressions.length) { | ||
throw parent.buildCodeFrameError("placeholders inside a tagged template string are not supported"); | ||
} | ||
path.replaceWithSourceString(compiledTemplateString); | ||
}, | ||
template = parent.node.quasi.quasis.map(function(quasi) { | ||
return quasi.value.cooked; | ||
}).join(""); | ||
CallExpression(path, state) { | ||
let calleePath = path.get('callee'); | ||
if (!calleePath.referencesImport('htmlbars-inline-precompile', 'default')) { | ||
return; | ||
} | ||
} else { | ||
return; | ||
} | ||
let argumentErrorMsg = "hbs should be invoked with a single argument: the template string"; | ||
if (path.node.arguments.length !== 1) { | ||
throw path.buildCodeFrameError(argumentErrorMsg); | ||
} | ||
let compiledTemplateString = `Ember.HTMLBars.template(${state.opts.precompile(template)})`; | ||
let template = path.node.arguments[0].value; | ||
if (typeof template !== "string") { | ||
throw path.buildCodeFrameError(argumentErrorMsg); | ||
} | ||
parent.replaceWithSourceString(compiledTemplateString); | ||
} | ||
let compiledTemplateString = `Ember.HTMLBars.template(${state.opts.precompile(template)})`; | ||
path.replaceWithSourceString(compiledTemplateString); | ||
}, | ||
@@ -69,0 +71,0 @@ } |
{ | ||
"name": "babel-plugin-htmlbars-inline-precompile", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "Babel plugin to replace tagged template strings with precompiled HTMLBars templates", | ||
@@ -16,4 +16,5 @@ "scripts": { | ||
"babel-core": "^6.7.4", | ||
"babel-plugin-transform-es2015-template-literals": "^6.22.0", | ||
"mocha": "^3.0.2" | ||
} | ||
} |
@@ -8,12 +8,25 @@ 'use strict'; | ||
const HTMLBarsInlinePrecompile = require('../index'); | ||
const TransformTemplateLiterals = require('babel-plugin-transform-es2015-template-literals'); | ||
function transform(code, precompile) { | ||
return babel.transform(code, { | ||
plugins: [ | ||
[HTMLBarsInlinePrecompile, {precompile: precompile}], | ||
], | ||
}).code.trim(); | ||
} | ||
describe("htmlbars-inline-precompile", function() { | ||
let precompile, plugins; | ||
describe("htmlbars-inline-precompile", function() { | ||
function transform(code) { | ||
return babel.transform(code, { | ||
plugins | ||
}).code.trim(); | ||
} | ||
beforeEach(function() { | ||
precompile = (template) => template.toUpperCase(); | ||
plugins = [ | ||
[HTMLBarsInlinePrecompile, { | ||
precompile: function() { | ||
return precompile.apply(this, arguments); | ||
} | ||
}], | ||
]; | ||
}); | ||
it("strips import statement for 'htmlbars-inline-precompile' module", function() { | ||
@@ -36,16 +49,22 @@ let transformed = transform("import hbs from 'htmlbars-inline-precompile';\nimport Ember from 'ember';"); | ||
it("replaces tagged template expressions with precompiled version", function() { | ||
let transformed = transform("import hbs from 'htmlbars-inline-precompile';\nvar compiled = hbs`hello`;", function(template) { | ||
return "precompiled(" + template + ")"; | ||
}); | ||
precompile = template => `precompiled(${template})`; | ||
let transformed = transform("import hbs from 'htmlbars-inline-precompile';\nvar compiled = hbs`hello`;"); | ||
assert.equal(transformed, "var compiled = Ember.HTMLBars.template(precompiled(hello));", "tagged template is replaced"); | ||
}); | ||
it("replaces tagged template expressions when before babel-plugin-transform-es2015-template-literals", function() { | ||
plugins.push([TransformTemplateLiterals]); | ||
let transformed = transform("import hbs from 'htmlbars-inline-precompile';\nvar compiled = hbs`hello`;"); | ||
assert.equal(transformed, "var compiled = Ember.HTMLBars.template(HELLO);", "tagged template is replaced"); | ||
}); | ||
it("doesn't replace unrelated tagged template strings", function() { | ||
let transformed = transform('import hbs from "htmlbars-inline-precompile";\nvar compiled = anotherTag`hello`;', function(template) { | ||
return "precompiled(" + template + ")"; | ||
}); | ||
precompile = template => `precompiled(${template})`; | ||
let transformed = transform('import hbs from "htmlbars-inline-precompile";\nvar compiled = anotherTag`hello`;'); | ||
assert.equal(transformed, "var compiled = anotherTag`hello`;", "other tagged template strings are not touched"); | ||
@@ -68,6 +87,6 @@ }); | ||
it("works with a plain string as parameter hbs('string')", function() { | ||
let transformed = transform("import hbs from 'htmlbars-inline-precompile';\nvar compiled = hbs('hello');", function(template) { | ||
return "precompiled(" + template + ")"; | ||
}); | ||
precompile = template => `precompiled(${template})`; | ||
let transformed = transform("import hbs from 'htmlbars-inline-precompile';\nvar compiled = hbs('hello');"); | ||
assert.equal(transformed, "var compiled = Ember.HTMLBars.template(precompiled(hello));", "tagged template is replaced"); | ||
@@ -74,0 +93,0 @@ }); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
26723
144
0
3