mirror of
https://github.com/marko-js/marko.git
synced 2025-12-08 19:26:05 +00:00
Add migration for legacy widget with getInitialState and no getTemplateData (#1385)
* Add migration for data = state
This commit is contained in:
parent
839648f7e1
commit
f789907c7e
2439
package-lock.json
generated
2439
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -62,7 +62,7 @@
|
||||
"warp10": "^2.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@marko/migrate": "^4.1.1",
|
||||
"@marko/migrate": "^5.1.0",
|
||||
"babel-cli": "^6.24.1",
|
||||
"babel-core": "^6.24.1",
|
||||
"babel-plugin-minprops": "^2.0.1",
|
||||
|
||||
@ -56,9 +56,7 @@ function replaceMarkoDebug(path, test, consequent, alternate) {
|
||||
// If we found a condition that is a string (and isn't "MARKO_DEBUG") it's most likely a mistake.
|
||||
if (test.value !== "MARKO_DEBUG") {
|
||||
throw new Error(
|
||||
`Found if statement with StringLiteral "${
|
||||
test.value
|
||||
}", did you mean "MARKO_DEBUG".`
|
||||
`Found if statement with StringLiteral "${test.value}", did you mean "MARKO_DEBUG".`
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@ -75,9 +75,7 @@ class Normalizer {
|
||||
ROOT_TAGS.includes(node.tagName)
|
||||
) {
|
||||
context.addError(
|
||||
`"${
|
||||
node.tagName
|
||||
}" can only be used at the root of the template.`,
|
||||
`"${node.tagName}" can only be used at the root of the template.`,
|
||||
node
|
||||
);
|
||||
}
|
||||
|
||||
@ -729,17 +729,11 @@ class CustomTag extends HtmlElement {
|
||||
if (!parentCustomTag) {
|
||||
if (tagDef.parentTagName) {
|
||||
codegen.addError(
|
||||
`Invalid usage of the <${
|
||||
this.tagName
|
||||
}> nested tag. Tag not nested within a <${
|
||||
tagDef.parentTagName
|
||||
}> tag.`
|
||||
`Invalid usage of the <${this.tagName}> nested tag. Tag not nested within a <${tagDef.parentTagName}> tag.`
|
||||
);
|
||||
} else {
|
||||
codegen.addError(
|
||||
`Invalid usage of the <${
|
||||
this.tagName
|
||||
}> nested tag. Tag not nested within a custom tag.`
|
||||
`Invalid usage of the <${this.tagName}> nested tag. Tag not nested within a custom tag.`
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@ -11,9 +11,7 @@ module.exports = function migrate(el, context) {
|
||||
function enableTagParams(el, context) {
|
||||
if (el.argument) {
|
||||
context.deprecate(
|
||||
`The <${el.tagName}(param)> syntax is deprecated. Please use the <${
|
||||
el.tagName
|
||||
}|param|> syntax instead. See: https://github.com/marko-js/marko/wiki/Deprecation:-paren-params`,
|
||||
`The <${el.tagName}(param)> syntax is deprecated. Please use the <${el.tagName}|param|> syntax instead. See: https://github.com/marko-js/marko/wiki/Deprecation:-paren-params`,
|
||||
el
|
||||
);
|
||||
el.params = context.builder.parseJavaScriptParams(el.argument);
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
const commonMigrators = [
|
||||
require("./non-standard-template-literals"),
|
||||
require("./render-calls"),
|
||||
require("./widget-data-is-state"),
|
||||
require("./widget-get-template-data")
|
||||
];
|
||||
|
||||
|
||||
16
src/core-tags/migrate/all-templates/widget-data-is-state.js
Normal file
16
src/core-tags/migrate/all-templates/widget-data-is-state.js
Normal file
@ -0,0 +1,16 @@
|
||||
"use strict";
|
||||
|
||||
module.exports = function migrator(el, context) {
|
||||
context.addMigration({
|
||||
name: "dataIsState",
|
||||
description:
|
||||
"Migrate add `data = state` since only `getInitialState` was defined in widget",
|
||||
apply() {
|
||||
context.root.prependChild(
|
||||
context.builder.scriptlet({
|
||||
value: "var data = state"
|
||||
})
|
||||
);
|
||||
}
|
||||
});
|
||||
};
|
||||
@ -107,9 +107,7 @@ function getInitModule(path, components) {
|
||||
var virtualPath = path + ".init.js";
|
||||
var registrations = components.map(
|
||||
component =>
|
||||
`components.register('${component.id}', require('${
|
||||
component.path
|
||||
}'));`
|
||||
`components.register('${component.id}', require('${component.path}'));`
|
||||
);
|
||||
var code = `
|
||||
var components = require('marko/components');
|
||||
|
||||
@ -14,9 +14,11 @@ import ExampleH from "../../../hello/example-h/index.marko"
|
||||
<!-- Import: duplicated -->
|
||||
<${ExampleA} x=1/>
|
||||
<!-- Import: with attributes -->
|
||||
<${ExampleB} ...{
|
||||
a: 1
|
||||
} b=2/>
|
||||
<${ExampleB}
|
||||
...{
|
||||
a: 1
|
||||
}
|
||||
b=2/>
|
||||
<!-- Import: index -->
|
||||
<${ExampleC}/>
|
||||
<!-- Import: name conflict -->
|
||||
@ -40,11 +42,14 @@ $ const ExampleD = undefined;
|
||||
<!-- Dynamic: with attributes -->
|
||||
<if(typeof input.x === "string")>${input.x}</if>
|
||||
<else>
|
||||
<${input.x} ...{
|
||||
a: 1
|
||||
} ...{
|
||||
b: 2
|
||||
} c=3/>
|
||||
<${input.x}
|
||||
...{
|
||||
a: 1
|
||||
}
|
||||
...{
|
||||
b: 2
|
||||
}
|
||||
c=3/>
|
||||
</else>
|
||||
<!-- Dynamic: with body -->
|
||||
<if(typeof input.x === "string")>${input.x}</if>
|
||||
|
||||
@ -10,9 +10,11 @@ import ExampleD_1 from "./example-d.marko"
|
||||
<div/>
|
||||
</>
|
||||
<!-- Import: with attributes -->
|
||||
<${ExampleB} ...{
|
||||
a: 1
|
||||
} b=2>
|
||||
<${ExampleB}
|
||||
...{
|
||||
a: 1
|
||||
}
|
||||
b=2>
|
||||
<div/>
|
||||
</>
|
||||
<!-- Import: index -->
|
||||
@ -29,8 +31,10 @@ $ const ExampleD = undefined;
|
||||
<div/>
|
||||
</>
|
||||
<!-- Dynamic: with attributes -->
|
||||
<${input.x} ...{
|
||||
a: 1
|
||||
} b=2>
|
||||
<${input.x}
|
||||
...{
|
||||
a: 1
|
||||
}
|
||||
b=2>
|
||||
<div/>
|
||||
</>
|
||||
|
||||
9
test/migrate/fixtures/widget-data-is-state/index.js
Normal file
9
test/migrate/fixtures/widget-data-is-state/index.js
Normal file
@ -0,0 +1,9 @@
|
||||
module.exports = require("marko-widgets").defineComponent({
|
||||
template: require("./template"),
|
||||
getInitialState(input) {
|
||||
return {
|
||||
x: input.x,
|
||||
y: input.y
|
||||
};
|
||||
}
|
||||
});
|
||||
12
test/migrate/fixtures/widget-data-is-state/prompts.js
Normal file
12
test/migrate/fixtures/widget-data-is-state/prompts.js
Normal file
@ -0,0 +1,12 @@
|
||||
module.exports = [
|
||||
{
|
||||
question:
|
||||
"A widget file was discovered, would you like to migrate that as well?\nNote: widget migrations are not 100% safe and should be tested after migration.",
|
||||
answer: true
|
||||
},
|
||||
{
|
||||
question:
|
||||
"Would you like to rename the component file?\nNote: Marko 4 automatically discovers these files based on the naming convention, you may be able to remove them from a browser.json file after this.",
|
||||
answer: false
|
||||
}
|
||||
];
|
||||
@ -0,0 +1,10 @@
|
||||
// test/migrate/fixtures/widget-data-is-state/index.js
|
||||
|
||||
module.exports = {
|
||||
onCreate(input, out) {
|
||||
this.state = {
|
||||
x: input.x,
|
||||
y: input.y
|
||||
};
|
||||
}
|
||||
};
|
||||
@ -0,0 +1,4 @@
|
||||
<!-- test/migrate/fixtures/widget-data-is-state/template.marko -->
|
||||
|
||||
$ var data = state;
|
||||
<div/>
|
||||
@ -0,0 +1 @@
|
||||
<div w-bind/>
|
||||
Loading…
x
Reference in New Issue
Block a user