Skip to content

Commit

Permalink
fix: allow duplicate macros with a warning instead of an error
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Jun 20, 2024
1 parent e491e45 commit 339c28d
Show file tree
Hide file tree
Showing 23 changed files with 140 additions and 64 deletions.
11 changes: 11 additions & 0 deletions .changeset/nice-eels-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"@marko/translator-default": patch
"@marko/babel-utils": patch
"@marko/compiler": patch
"marko": patch
"@marko/runtime-tags": patch
"@marko/translator-interop-class-tags": patch
"@marko/translator-tags": patch
---

Allow diagnostic fixes in parse stage.
5 changes: 5 additions & 0 deletions .changeset/old-doors-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@marko/translator-default": patch
---

Reduce duplicated macros from an error to a warning.
5 changes: 5 additions & 0 deletions .changeset/real-apes-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@marko/translator-default": patch
---

Add auto fix for named inline component classes
12 changes: 8 additions & 4 deletions packages/babel-utils/src/diagnostics.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ function add(type, path, options) {
let fix = false;

if (rawFix) {
if (file.___compileStage !== "migrate") {
throw new Error(
"Diagnostic fixes can only be registered during the migrate stage.",
);
switch (file.___compileStage) {
case "parse":
case "migrate":
break;
default:
throw new Error(
"Diagnostic fixes can only be registered up to and including the migrate stage.",
);
}

const { applyFixes } = file.markoOpts;
Expand Down
10 changes: 7 additions & 3 deletions packages/babel-utils/src/tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { basename, dirname, relative, resolve, join } from "path";
import { types as t } from "@marko/compiler";
import { getRootDir } from "lasso-package-root";
import resolveFrom from "resolve-from";
import { diagnosticWarn } from "./diagnostics";
import { resolveRelativePath } from "./imports";
import { getTagDefForTagName } from "./taglib";

Expand Down Expand Up @@ -63,9 +64,12 @@ export function registerMacro(path, name) {

if (macroNames) {
if (macroNames[name]) {
throw path.buildCodeFrameError(
`A macro with the name "${name}" already exists.`,
);
diagnosticWarn(path, {
label: `A macro with the name "${name}" already exists.`,
fix() {
findParentTag(path).remove();
},
});
}
macroNames[name] = true;
} else {
Expand Down
3 changes: 3 additions & 0 deletions packages/translator-default/src/taglib/core/parse-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ export default function (path) {
diagnosticDeprecate(path, {
label: "Component class should not have a name.",
loc: parsed.id.loc,
fix() {
parsed.id = null;
},
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
deprecation(1:7-1:11): Component class should not have a name.
deprecation[fixable](1:7-1:11): Component class should not have a name.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"use strict";

exports.__esModule = true;
exports.default = void 0;
var _index = require("marko/src/runtime/html/index.js");
var _renderer = _interopRequireDefault(require("marko/src/runtime/components/renderer.js"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const _marko_componentType = "packages/translator-default/test/fixtures/warn-macro-duplicate/template.marko",
_marko_template = (0, _index.t)(_marko_componentType);
var _default = exports.default = _marko_template;
const _marko_component = {};
_marko_template._ = (0, _renderer.default)(function (input, out, _componentDef, _component, state, $global) {
function _thing(out, stuff) {
out.w("<div>");
out.w("a");
out.w("</div>");
}
}, {
t: _marko_componentType,
i: true,
d: true
}, _marko_component);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
warning[fixable](5:15-5:27): A macro with the name "thing" already exists.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<macro|stuff| name="thing">
<div>
a
</div>
</macro>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { t as _t } from "marko/src/runtime/html/index.js";
const _marko_componentType = "packages/translator-default/test/fixtures/warn-macro-duplicate/template.marko",
_marko_template = _t(_marko_componentType);
export default _marko_template;
import _marko_renderer from "marko/src/runtime/components/renderer.js";
const _marko_component = {};
_marko_template._ = _marko_renderer(function (input, out, _componentDef, _component, state, $global) {
function _thing(out, stuff) {
out.w("<div>");
out.w("a");
out.w("</div>");
}
}, {
t: _marko_componentType,
i: true,
d: true
}, _marko_component);
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { t as _t } from "marko/dist/runtime/html/index.js";
const _marko_componentType = "IOFxoILb",
_marko_template = _t(_marko_componentType);
export default _marko_template;
import _marko_renderer from "marko/dist/runtime/components/renderer.js";
const _marko_component = {};
_marko_template._ = _marko_renderer(function (input, out, _componentDef, _component, state, $global) {
function _thing(out, stuff) {
out.w("<div>a</div>");
}
}, {
t: _marko_componentType,
i: true
}, _marko_component);
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { t as _t } from "marko/src/runtime/vdom/index.js";
const _marko_componentType = "packages/translator-default/test/fixtures/warn-macro-duplicate/template.marko",
_marko_template = _t(_marko_componentType);
export default _marko_template;
import _marko_renderer from "marko/src/runtime/components/renderer.js";
import { r as _marko_registerComponent } from "marko/src/runtime/components/registry.js";
_marko_registerComponent(_marko_componentType, () => _marko_template);
const _marko_component = {};
_marko_template._ = _marko_renderer(function (input, out, _componentDef, _component, state, $global) {
function _thing(out, stuff) {
out.be("div", null, "1", _component, null, 0);
out.t("a", _component);
out.ee();
}
}, {
t: _marko_componentType,
i: true,
d: true
}, _marko_component);
import _marko_defineComponent from "marko/src/runtime/components/defineComponent.js";
_marko_template.Component = _marko_defineComponent(_marko_component, _marko_template._);
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { t as _t } from "marko/dist/runtime/vdom/index.js";
const _marko_componentType = "IOFxoILb",
_marko_template = _t(_marko_componentType);
export default _marko_template;
import _marko_constElement from "marko/dist/runtime/vdom/helpers/const-element.js";
const _marko_node = _marko_constElement("div", null, 1).t("a");
import _marko_renderer from "marko/dist/runtime/components/renderer.js";
import { r as _marko_registerComponent } from "marko/dist/runtime/components/registry.js";
_marko_registerComponent(_marko_componentType, () => _marko_template);
const _marko_component = {};
_marko_template._ = _marko_renderer(function (input, out, _componentDef, _component, state, $global) {
function _thing(out, stuff) {
out.n(_marko_node, _component);
}
}, {
t: _marko_componentType,
i: true
}, _marko_component);
import _marko_defineComponent from "marko/dist/runtime/components/defineComponent.js";
_marko_template.Component = _marko_defineComponent(_marko_component, _marko_template._);

0 comments on commit 339c28d

Please sign in to comment.