Skip to content

Commit 7d5dab4

Browse files
committed
feat: support declared ts class members, remove extra export in stripTypes mode
1 parent cceab7d commit 7d5dab4

File tree

14 files changed

+185
-12
lines changed

14 files changed

+185
-12
lines changed

.changeset/selfish-parrots-turn.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@marko/compiler": patch
3+
"marko": patch
4+
"@marko/translator-default": patch
5+
---
6+
7+
Avoid adding `export {}` (from "@babel/plugin-transform-typescript") when outputing a template with the types stripped.

.changeset/small-donuts-sip.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
---
66

77
Reduce script parsing restrictions added by Babel.
8-
This was causing Babel to error when parsing partion scripts.
8+
This was causing Babel to error when parsing partial scripts.
99

1010
```marko
1111
static const x = 1;

.changeset/smooth-dingos-grow.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@marko/compiler": patch
3+
"marko": patch
4+
"@marko/translator-default": patch
5+
---
6+
7+
Avoid outputing a `declare`'d type on a `class`.

packages/compiler/src/babel-plugin/index.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export default (api, markoOpts) => {
7373
taglibConfig.fs = markoOpts.fileSystem;
7474
curOpts = undefined;
7575
try {
76-
if (markoOpts.output === "source" || markoOpts.output === "migrate") {
76+
if (isMarkoOutput(markoOpts.output)) {
7777
return file;
7878
}
7979

@@ -114,7 +114,21 @@ export default (api, markoOpts) => {
114114
} finally {
115115
taglibConfig.fs = prevFS;
116116
}
117-
}
117+
},
118+
visitor:
119+
markoOpts.stripTypes && isMarkoOutput(markoOpts.output)
120+
? {
121+
ExportNamedDeclaration: {
122+
exit(path) {
123+
// The babel typescript plugin will add an empty export declaration
124+
// if there are no other imports/exports in the file.
125+
// This is not needed for Marko file outputs since there is always
126+
// a default export.
127+
if (path.node.specifiers.length === 0) path.remove();
128+
}
129+
}
130+
}
131+
: undefined
118132
};
119133
};
120134

@@ -333,3 +347,7 @@ function addPlugin(meta, arr, plugin) {
333347
function unique(item, i, list) {
334348
return list.indexOf(item) === i;
335349
}
350+
351+
function isMarkoOutput(output) {
352+
return output === "source" || output === "migrate";
353+
}

packages/compiler/src/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path from "path";
33
import * as babel from "@babel/core";
44
import cjsPlugin from "@babel/plugin-transform-modules-commonjs";
55
import tsSyntaxPlugin from "@babel/plugin-syntax-typescript";
6-
import tsPlugin from "@babel/plugin-transform-typescript";
6+
import tsTransformPlugin from "@babel/plugin-transform-typescript";
77
import corePlugin from "./babel-plugin";
88
import defaultConfig from "./config";
99
import * as taglib from "./taglib";
@@ -67,10 +67,11 @@ function loadBabelConfig(filename, config) {
6767
const requiredPlugins = [
6868
[corePlugin, markoConfig],
6969
[
70-
markoConfig.stripTypes ? tsPlugin : tsSyntaxPlugin,
70+
markoConfig.stripTypes ? tsTransformPlugin : tsSyntaxPlugin,
7171
{
7272
isTSX: false,
7373
allowNamespaces: true,
74+
allowDeclareFields: true,
7475
optimizeConstEnums: true,
7576
onlyRemoveTypeImports: true,
7677
disallowAmbiguousJSXLike: false

packages/translator-default/src/class.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ export default function (path) {
2525
delete prop.loc;
2626
return prop;
2727
} else if (t.isClassProperty(prop) && !prop.static) {
28-
classProperties.push(
29-
t.assignmentExpression(
30-
"=",
31-
t.memberExpression(t.thisExpression(), prop.key, prop.computed),
32-
prop.value || t.unaryExpression("void", t.numericLiteral(0))
33-
)
34-
);
28+
if (!prop.declare) {
29+
classProperties.push(
30+
t.assignmentExpression(
31+
"=",
32+
t.memberExpression(t.thisExpression(), prop.key, prop.computed),
33+
prop.value || t.unaryExpression("void", t.numericLiteral(0))
34+
)
35+
);
36+
}
3537

3638
return undefined;
3739
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"use strict";
2+
3+
exports.__esModule = true;
4+
exports.default = void 0;
5+
var _index = require("marko/src/runtime/html/index.js");
6+
var _renderer = _interopRequireDefault(require("marko/src/runtime/components/renderer.js"));
7+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
8+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
9+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
10+
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
11+
const _marko_componentType = "packages/translator-default/test/fixtures/declared-class-member/template.marko",
12+
_marko_template = (0, _index.t)(_marko_componentType);
13+
var _default = _marko_template;
14+
exports.default = _default;
15+
class MyClass {
16+
constructor() {
17+
_defineProperty(this, "y", 2);
18+
}
19+
}
20+
const _marko_component = {
21+
onCreate() {
22+
this.y = 2
23+
}
24+
};
25+
_marko_template._ = (0, _renderer.default)(function (input, out, _componentDef, _component, state, $global) {}, {
26+
t: _marko_componentType,
27+
d: true
28+
}, _marko_component);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class {
2+
declare x: string;
3+
y = 2;
4+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
2+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
3+
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
4+
import { t as _t } from "marko/src/runtime/html/index.js";
5+
const _marko_componentType = "packages/translator-default/test/fixtures/declared-class-member/template.marko",
6+
_marko_template = _t(_marko_componentType);
7+
export default _marko_template;
8+
class MyClass {
9+
constructor() {
10+
_defineProperty(this, "y", 2);
11+
}
12+
}
13+
import _marko_renderer from "marko/src/runtime/components/renderer.js";
14+
const _marko_component = {
15+
onCreate() {
16+
this.y = 2
17+
}
18+
};
19+
_marko_template._ = _marko_renderer(function (input, out, _componentDef, _component, state, $global) {}, {
20+
t: _marko_componentType,
21+
d: true
22+
}, _marko_component);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
2+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
3+
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
4+
import { t as _t } from "marko/dist/runtime/html/index.js";
5+
const _marko_componentType = "xmfoovwT",
6+
_marko_template = _t(_marko_componentType);
7+
export default _marko_template;
8+
class MyClass {
9+
constructor() {
10+
_defineProperty(this, "y", 2);
11+
}
12+
}
13+
import _marko_renderer from "marko/dist/runtime/components/renderer.js";
14+
const _marko_component = {
15+
onCreate() {
16+
this.y = 2
17+
}
18+
};
19+
_marko_template._ = _marko_renderer(function (input, out, _componentDef, _component, state, $global) {}, {
20+
t: _marko_componentType
21+
}, _marko_component);

0 commit comments

Comments
 (0)