mirror of
https://github.com/marko-js/marko.git
synced 2025-12-08 19:26:05 +00:00
Deprecate and add migrator for top level named classes
This commit is contained in:
parent
535daf9451
commit
6d70aa18da
6
src/core-tags/components/class-tag-node-factory.js
Normal file
6
src/core-tags/components/class-tag-node-factory.js
Normal file
@ -0,0 +1,6 @@
|
||||
module.exports = function nodeFactory(el) {
|
||||
// Previously `class` was a CodeGenerator.
|
||||
// CodeGenerators have their `type` overwritten and some code is relying on that for `class`.
|
||||
el.type = __filename;
|
||||
return el;
|
||||
};
|
||||
7
src/core-tags/components/class-tag-transformer.js
Normal file
7
src/core-tags/components/class-tag-transformer.js
Normal file
@ -0,0 +1,7 @@
|
||||
module.exports = function transformer(el, context) {
|
||||
if (el.parentNode.type !== "TemplateRoot") {
|
||||
context.addError(
|
||||
"class is a static tag and can only be declared at the template root"
|
||||
);
|
||||
}
|
||||
};
|
||||
@ -123,5 +123,13 @@
|
||||
"<body>": {
|
||||
"transformer": "./body-transformer.js"
|
||||
},
|
||||
"<class>": {
|
||||
"node-factory": "./class-tag-node-factory.js",
|
||||
"transformer": {
|
||||
"path": "./class-tag-transformer.js",
|
||||
"priority": -1
|
||||
},
|
||||
"open-tag-only": true
|
||||
},
|
||||
"transformer": "./components-transformer.js"
|
||||
}
|
||||
|
||||
@ -1,9 +0,0 @@
|
||||
module.exports = function functionCodeGenerator(el, codegen) {
|
||||
if (el.parentNode.type !== "TemplateRoot") {
|
||||
codegen.addError(
|
||||
"class is a static tag and can only be declared at the template root"
|
||||
);
|
||||
}
|
||||
codegen.addStaticCode(codegen.builder.expression(el.tagString));
|
||||
return null;
|
||||
};
|
||||
@ -1,8 +1,4 @@
|
||||
{
|
||||
"<class>": {
|
||||
"code-generator": "./class-tag",
|
||||
"open-tag-only": true
|
||||
},
|
||||
"<else>": {
|
||||
"node-factory": "./else-tag",
|
||||
"attributes": {},
|
||||
|
||||
19
src/core-tags/migrate/class-tag.js
Normal file
19
src/core-tags/migrate/class-tag.js
Normal file
@ -0,0 +1,19 @@
|
||||
const classNamedRegexp = /^(<?\s*class\s*)(?!extends)[^\s]+(\s*{)/;
|
||||
module.exports = function migrator(el, context) {
|
||||
if (!el.tagString || !classNamedRegexp.test(el.tagString)) {
|
||||
// Check for a named class
|
||||
return;
|
||||
}
|
||||
|
||||
if (el.parentNode.type !== "TemplateRoot") {
|
||||
context.addError(
|
||||
"class is a static tag and can only be declared at the template root"
|
||||
);
|
||||
}
|
||||
|
||||
context.deprecate(
|
||||
"Having a named class at the top level of a file is deprecated. Use `class {...}` without a name instead."
|
||||
);
|
||||
|
||||
el.tagString = el.tagString.replace(classNamedRegexp, "$1$2");
|
||||
};
|
||||
@ -217,5 +217,9 @@
|
||||
"<script>": {
|
||||
"@marko-init": "boolean",
|
||||
"@template-helpers": "boolean"
|
||||
},
|
||||
"<class>": {
|
||||
"migrator": "./class-tag",
|
||||
"open-tag-only": true
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
class MyComponent {
|
||||
class {
|
||||
onCreate() {
|
||||
this.state = {
|
||||
count: 0
|
||||
|
||||
@ -0,0 +1,5 @@
|
||||
<!-- test/migrate/fixtures/class-tag-named/template.marko -->
|
||||
|
||||
class {
|
||||
y() {}
|
||||
}
|
||||
5
test/migrate/fixtures/class-tag-named/template.marko
Normal file
5
test/migrate/fixtures/class-tag-named/template.marko
Normal file
@ -0,0 +1,5 @@
|
||||
class abc {
|
||||
y() {
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user