mirror of
https://github.com/marko-js/marko.git
synced 2026-02-01 16:07:13 +00:00
Added body only if tests and migration path
This commit is contained in:
parent
6e30730d11
commit
5981fc3ef0
@ -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) {
|
||||
|
||||
@ -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
|
||||
|
||||
39
src/core-tags/migrate/all-tags/body-only-if.js
Normal file
39
src/core-tags/migrate/all-tags/body-only-if.js
Normal 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);
|
||||
}
|
||||
};
|
||||
@ -1,4 +1,5 @@
|
||||
const commonMigrators = [
|
||||
require("./body-only-if"),
|
||||
require("./control-flow-directives"),
|
||||
require("./dynamic-attributes"),
|
||||
require("./include-directive"),
|
||||
|
||||
@ -0,0 +1,3 @@
|
||||
<div>
|
||||
Component
|
||||
</div>
|
||||
@ -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>
|
||||
@ -0,0 +1,5 @@
|
||||
<div>
|
||||
<MyComponent body-only-if(input.test)>
|
||||
Blah
|
||||
</MyComponent>
|
||||
</div>
|
||||
@ -0,0 +1,5 @@
|
||||
<!-- test/migrate/fixtures/body-only-if/template.marko -->
|
||||
|
||||
<div>
|
||||
<${input.test ? null : "span"}>Blah</>
|
||||
</div>
|
||||
5
test/migrate/fixtures/body-only-if/template.marko
Normal file
5
test/migrate/fixtures/body-only-if/template.marko
Normal file
@ -0,0 +1,5 @@
|
||||
<div>
|
||||
<span body-only-if(input.test)>
|
||||
Blah
|
||||
</span>
|
||||
</div>
|
||||
Loading…
x
Reference in New Issue
Block a user