Added body only if tests and migration path

This commit is contained in:
Andrew Gliga 2019-05-15 13:06:20 -07:00
parent 6e30730d11
commit 5981fc3ef0
No known key found for this signature in database
GPG Key ID: A33D8B74708F2A93
9 changed files with 65 additions and 37 deletions

View File

@ -1,31 +1,6 @@
"use strict";
var coreAttrHandlers = [
[
"body-only-if",
function(attr, node, el) {
var argument = attr.argument;
if (!argument) {
return false;
}
// context.deprecate(
// 'The "body-only-if" attribute is deprecated. Please use the "<${dynamicTag}/>" tag instead. See: https://github.com/marko-js/marko/wiki/Deprecation:(body-only-if)'
// );
var test;
try {
test = this.builder.parseExpression(argument);
} catch (e) {
test = this.builder.literalFalse();
this.addError(
"Invalid expression for body-only-if statement:\n" +
e.message
);
}
el.setBodyOnlyIf(test);
}
],
[
"marko-preserve-whitespace",
function(attr, node, el) {

View File

@ -194,18 +194,6 @@
]
},
"<*>": {
"@body-only-if": {
"type": "statement",
"autocomplete": [
{
"snippet": "body-only-if(${1:condition})",
"descriptionMoreURL": "http://markojs.com/docs/marko/language-guide/#body-only-if"
},
{
"descriptionMoreURL": "http://markojs.com/docs/marko/language-guide/#body-only-if"
}
]
},
"transformer": {
"path": "./core-transformer",
"priority": 0

View File

@ -0,0 +1,39 @@
const printJS = require("../util/printJS");
const importTag = require("../util/import-tag");
module.exports = function migrator(node, context) {
const argument = node.getAttribute("body-only-if");
if (argument) {
context.deprecate(
'The "body-only-if(x)" tag is deprecated. Please use "<${test ? null : tag>" instead. See: https://github.com/marko-js/marko/wiki/Deprecation:-body-only-if-tag'
);
const builder = context.builder;
node.removeAttribute("body-only-if");
const attributes = node.attributes;
const tagDef = node.tagDef || {};
const dynamicTag = builder.htmlElement(
undefined,
attributes,
node.body
);
let tagName =
node.rawTagNameExpression || builder.literal(node.tagName);
// If tag is dynamic and not a normal HTML tag, then render dynamic version of tagName
if (!tagDef.html) {
tagName = importTag(tagDef.renderer || tagDef.template, context);
}
dynamicTag.rawTagNameExpression = printJS(
builder.conditionalExpression(
builder.expression(argument.argument),
builder.literalNull(),
tagName
),
context
);
node.replaceWith(dynamicTag);
}
};

View File

@ -1,4 +1,5 @@
const commonMigrators = [
require("./body-only-if"),
require("./control-flow-directives"),
require("./dynamic-attributes"),
require("./include-directive"),

View File

@ -0,0 +1,3 @@
<div>
Component
</div>

View File

@ -0,0 +1,7 @@
<!-- test/migrate/fixtures/body-only-if-dynamic/template.marko -->
import MyComponent_1 from "PROJECT_ROOT/test/migrate/fixtures/body-only-if-dynamic/components/MyComponent.marko"
<div>
<${input.test ? null : MyComponent_1}>Blah</>
</div>

View File

@ -0,0 +1,5 @@
<div>
<MyComponent body-only-if(input.test)>
Blah
</MyComponent>
</div>

View File

@ -0,0 +1,5 @@
<!-- test/migrate/fixtures/body-only-if/template.marko -->
<div>
<${input.test ? null : "span"}>Blah</>
</div>

View File

@ -0,0 +1,5 @@
<div>
<span body-only-if(input.test)>
Blah
</span>
</div>