Deprecate and add migrator for top level named classes

This commit is contained in:
Dylan Piercey 2019-07-02 09:14:10 -07:00
parent 535daf9451
commit 6d70aa18da
No known key found for this signature in database
GPG Key ID: DA54E25D5BF13DBE
12 changed files with 55 additions and 14 deletions

View 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;
};

View 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"
);
}
};

View File

@ -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"
}

View File

@ -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;
};

View File

@ -1,8 +1,4 @@
{
"<class>": {
"code-generator": "./class-tag",
"open-tag-only": true
},
"<else>": {
"node-factory": "./else-tag",
"attributes": {},

View 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");
};

View File

@ -217,5 +217,9 @@
"<script>": {
"@marko-init": "boolean",
"@template-helpers": "boolean"
},
"<class>": {
"migrator": "./class-tag",
"open-tag-only": true
}
}

View File

@ -1,4 +1,4 @@
class MyComponent {
class {
onCreate() {
this.state = {
count: 0

View File

@ -0,0 +1,5 @@
<!-- test/migrate/fixtures/class-tag-named/template.marko -->
class {
y() {}
}

View File

@ -0,0 +1,5 @@
class abc {
y() {
}
}