forked from massive-oss/haxe-react
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathReactMacro.hx
More file actions
758 lines (652 loc) · 21.1 KB
/
ReactMacro.hx
File metadata and controls
758 lines (652 loc) · 21.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
package react;
#if macro
import haxe.ds.Option;
import haxe.macro.Compiler;
import haxe.macro.Context;
import haxe.macro.Expr;
import haxe.macro.Type;
import haxe.macro.TypeTools;
import tink.hxx.Node;
import tink.hxx.Parser;
import tink.hxx.StringAt;
import react.jsx.AriaAttributes;
import react.jsx.JsxLiteral;
import react.jsx.JsxPropsBuilder;
import react.jsx.JsxStaticMacro;
import react.macro.MacroUtil;
import react.macro.PropsValidator;
import react.macro.ReactMeta;
import react.macro.ReactComponentMacro;
using react.macro.MacroUtil;
using tink.MacroApi;
using StringTools;
#if (haxe_ver < 4)
private typedef ObjectField = {field:String, expr:Expr};
#end
typedef ComponentReflection = {
children:Void->ComplexType,
propsType:Null<Expr>,
neededAttrs:Array<String>,
typeChecker:StringAt->Expr->Expr
};
#end
/**
Provides a simple macro for parsing jsx into Haxe expressions.
**/
@:dce
class ReactMacro
{
static public macro function jsx(expr:ExprOf<String>):Expr {
return _jsx(expr);
}
#if macro
static var REACT_FRAGMENT_CT = macro :react.ReactComponent.ReactFragment;
public static function _jsx(expr:Expr):Expr {
function children(c:Children)
return switch c.value {
case [v]: child(v);
case []: expr.reject('empty jsx');
default: expr.reject('only one node allowed here');
};
try {
return children(tink.hxx.Parser.parseRoot(
expr,
{
fragment: 'react.Fragment',
defaultExtension: 'html',
treatNested: function(c) return children.bind(c).bounce()
}
));
} catch (e:HxxEscape) {
return e.expr;
}
}
static function children(c:tink.hxx.Children, getChildrenType:Void->ComplexType) {
var lazyType:ComplexType = null;
var exprs = switch (c) {
case null | { value: null }: [];
default:
lazyType = getChildrenType();
if (lazyType == REACT_FRAGMENT_CT)
[for (c in tink.hxx.Generator.normalize(c.value)) macro (${child(c)}:$lazyType)];
else
[for (c in tink.hxx.Generator.normalize(c.value)) macro ${child(c)}];
};
return {
individual: exprs,
compound: switch (exprs) {
case []: null;
case [v]:
if (lazyType == null) lazyType = getChildrenType();
macro (${v}:$lazyType);
case a: macro ($a{a} :Array<$REACT_FRAGMENT_CT>);
}
};
}
static function child(c:Child)
{
return switch (c.value) {
case CText(s):
#if (react_diagnostics || jsx_warn_for_constant_text)
if (~/([^a-zA-Z]+)/g.replace(s.value, '').length > 2) {
var localClass = Context.getLocalClass();
if (localClass == null || !localClass.get().meta.has(':jsxIgnoreConstantText')) {
Context.warning('Constant text detected in jsx', s.pos);
}
}
#end
macro @:pos(s.pos) $v{s.value};
case CExpr(e): e;
case CNode(n):
var type = switch (n.name.value.replace('$', '').split('.')) {
case [tag] if (tag.charAt(0) == tag.charAt(0).toLowerCase()):
macro @:pos(n.name.pos) $v{tag};
case parts:
macro @:pos(n.name.pos) ${MacroUtil.toFieldExpr(parts, n.name.pos)};
};
var isHtml = type.getString().isSuccess(); //TODO: this is a little awkward
function handleTagDisplay(pos) {
if (isHtml) {
// Try to resolve corresponding html element
// (note all html elements have a dedicated js.html.*Element class)
var typeStr = type.getString().sure().toLowerCase();
var typeExpr = switch (typeStr) {
case "p": macro js.html.ParagraphElement;
case "h1" | "h2" | "h3" | "h4" | "h5" | "h6": macro js.html.HeadingElement;
// TODO: other special cases
case _:
var tagClass = typeStr.charAt(0).toUpperCase() + typeStr.substr(1) + "Element";
var ct = macro :js.html.$tagClass;
try {
Context.typeof(macro (null:$ct));
macro js.html.$tagClass;
} catch (_) {
macro js.html.Element;
}
};
typeExpr.pos = pos;
throw new HxxEscape({pos: pos, expr: EDisplay(typeExpr, DKMarked)});
}
throw new HxxEscape({
pos: n.name.pos,
expr: EDisplay(macro @:pos(n.name.pos) $e{type}, DKMarked)
});
}
if (Context.containsDisplayPosition(n.name.pos)) {
handleTagDisplay(n.name.pos);
} else if (n.closing != null && Context.containsDisplayPosition(n.closing)) {
handleTagDisplay(n.closing);
}
if (!isHtml) JsxStaticMacro.handleJsxStaticProxy(type);
var component = componentReflection(type, n.name.pos, isHtml);
var checkProp = component.typeChecker;
var childrenType = component.children;
var neededAttrs = component.neededAttrs.copy();
var attrs = new Array<ExtendedObjectField>();
var spread = [];
var key = null;
var ref = null;
function add(name:StringAt, e:Expr)
{
attrs.push({
field: name.value,
expr: checkProp(name, e),
isConstant: switch (e.expr) {
case EConst(_), EParenthesis({expr: EConst(_)}): true;
case _: false;
}
});
}
for (attr in n.attributes)
{
switch (attr)
{
case Splat(e):
spread.push(e);
// Spread is not handled, so we assume every needed prop is passed
neededAttrs = [];
case Empty(invalid = { value: 'key' | 'ref'}):
invalid.pos.error('attribute ${invalid.value} must have a value');
case Empty(name):
if (Context.containsDisplayPosition(name.pos)) {
if (component.propsType != null) {
var prop = name.value;
throw new HxxEscape({
pos: name.pos,
expr: EDisplay(macro @:pos(name.pos) $e{component.propsType}.$prop, DKMarked)
});
}
}
neededAttrs.remove(name.value);
add(name, macro @:pos(name.pos) true);
case Regular(name, value):
if (Context.containsDisplayPosition(name.pos)) {
if (component.propsType != null) {
var prop = name.value;
throw new HxxEscape({
pos: name.pos,
expr: EDisplay(macro @:pos(name.pos) $e{component.propsType}.$prop, DKMarked)
});
}
}
if (Context.containsDisplayPosition(value.pos)) {
// TODO: recurse to pinpoint the most precise expression possible
// TODO: handle recursive jsx here.. somehow..
throw new HxxEscape({pos: value.pos, expr: EDisplay(value, DKMarked)});
}
neededAttrs.remove(name.value);
var expr = value.getString()
.map(function (s) return haxe.macro.MacroStringTools.formatString(s, value.pos))
.orUse(value);
switch (name.value)
{
case 'key': key = expr;
case 'ref' if (n.name.value != 'react.Fragment'): ref = expr;
default: add(name, expr);
}
}
}
// TODO: uh well need to be sure we're not inside a value either..
if (Context.containsDisplayPosition(n.opening) && !Context.getDisplayMode().match(Hover)) {
if (component.propsType != null) {
// TODO: make an util out of this
var rawPos = Compiler.getDisplayPos();
var pos = haxe.macro.PositionTools.make({file: rawPos.file, min: rawPos.pos, max: rawPos.pos});
// TODO: delay that process and only give missing props
throw new HxxEscape({
pos: pos,
expr: EDisplay({pos: pos, expr: EField(component.propsType, "sa")}, DKDot)
});
}
}
// parse children
var children = children(n.children, childrenType);
if (children.compound != null) neededAttrs.remove('children');
for (attr in neededAttrs)
Context.warning(
'Missing prop `$attr` for component `${n.name.value}`',
c.pos
);
// inline declaration or createElement?
var typeInfo = ReactComponentMacro.getComponentInfo(type);
JsxStaticMacro.injectDisplayNames(type);
var useLiteral = JsxLiteral.canUseLiteral(typeInfo, ref);
// TODO: find a way to restrict to ReactType
// TODO: handle html tags too
// TODO: check hxx parser for <| completion requests
if (Context.containsDisplayPosition(c.pos)) {
// TODO: make an util out of this
var rawPos = Compiler.getDisplayPos();
var pos = haxe.macro.PositionTools.make({file: rawPos.file, min: rawPos.pos, max: rawPos.pos});
throw new HxxEscape({
pos: pos,
expr: EDisplay(macro @:pos(pos) (null:react.ReactType), DKMarked)
});
}
if (useLiteral)
{
if (children.compound != null)
{
attrs.push({field:'children', expr: children.compound });
}
var applyDefaultProps:Expr->Expr = function(e) return e;
if (!isHtml)
{
var defaultProps = ReactComponentMacro.getDefaultProps(typeInfo, attrs);
if (defaultProps != null) {
// Reproduce react's way of applying defaultProps to
// make sure we get consistent behavior between
// debug/non-debug/react_no_inline
// See https://github.com/facebook/react/blob/d3622d0/packages/react/src/ReactElement.js#L210
applyDefaultProps = function(e:Expr) {
var tprops = typeInfo.tprops;
return macro {
var __props = $e{tprops != null ? macro ($e :$tprops) : e};
@:mergeBlock $b{[for (defaultProp in defaultProps) {
var name = defaultProp.field;
macro {
#if haxe4
if (js.Syntax.code('{0} === undefined', __props.$name))
#else
if (untyped __js__('{0} === undefined', __props.$name))
#end
__props.$name = @:privateAccess $e{typeInfo.ref}.defaultProps.$name;
};
}]};
__props;
}
}
}
}
var pos = (macro null).pos;
var props = JsxPropsBuilder.makeProps(spread, attrs, pos);
props = applyDefaultProps(props);
JsxLiteral.genLiteral(type, props, ref, key, pos);
}
else
{
if (ref != null) attrs.unshift({field:'ref', expr:ref});
if (key != null) attrs.unshift({field:'key', expr:key});
var pos = (macro null).pos;
var props = JsxPropsBuilder.makeProps(spread, attrs, pos);
var args = [type, props].concat(children.individual);
macro react.React.createElement($a{args});
}
case CSplat(_):
c.pos.error('jsx does not support child splats');
case CIf(cond, cons, alt):
macro @:pos(cond.pos) if ($cond) ${body(cons)} else ${body(alt)};
case CFor(head, expr):
macro @:pos(head.pos) ([for ($head) ${body.bind(expr).bounce()}]:Array<$REACT_FRAGMENT_CT>);
case CSwitch(target, cases):
ESwitch(target, [for (c in cases) {
guard: c.guard,
values: c.values,
expr: body.bind(c.children).bounce()
}], null).at(target.pos);
case CLet(defs, c):
var vars:Array<Var> = [];
function add(name, value)
vars.push({
name: name,
type: null,
expr: value,
});
for (d in defs) switch d {
case Empty(a): a.pos.error('empty attributes not allowed on <let>');
case Regular(a, v):
add(a.value, v);
case Splat(e):
var tmp = MacroApi.tempName();
add(tmp, e);
for (f in e.typeof().sure().getFields().sure())
if (f.isPublic && !f.kind.match(FMethod(MethMacro)))
add(f.name, macro @:pos(e.pos) $p{[tmp, f.name]});
}
[EVars(vars).at(c.pos), body.bind(c).bounce()].toBlock(c.pos);
}
}
static function body(c:Children)
{
if (c == null) return macro null;
var childrenArr = children(c, function() return REACT_FRAGMENT_CT).individual;
return switch (childrenArr.length) {
case 0: macro null;
case 1: childrenArr[0];
default:
macro react.React.createElement($a{
[macro react.Fragment, macro null].concat(childrenArr)
});
};
}
static function componentReflection(
type:Expr,
nodePos:Position,
isHtml:Bool
):ComponentReflection {
function propsFor(placeholder:Expr):StringAt->Expr->Expr {
placeholder = Context.storeTypedExpr(Context.typeExpr(placeholder));
var isTMono = switch (Context.typeof(placeholder)) {
case TMono(_.get() => null): true;
default: false;
};
return function (name:StringAt, value:Expr) {
var field = name.value;
// Position is used for invalid prop name
var target = macro @:pos(name.pos) $placeholder.$field;
// Handle components accepting more than their own props
try {
Context.typeof(target);
} catch (e:Dynamic) {
var reg = new EReg('has no field $field($| \\(Suggestion: \\w+\\))', '');
if (reg.match(Std.string(e))) {
switch (Context.typeof(type)) {
case TType(_.get().type => TAnonymous(_.get() => {
status: AClassStatics(_.get() => t)
}), _) if (t.meta.has(ReactMeta.AcceptsMoreProps)):
var validators = t.meta.extract(ReactMeta.AcceptsMoreProps);
if (validators[0].params.length > 0) {
for (v in validators[0].params) {
var k = MacroUtil.extractMetaString(v);
if (k == null) {
Context.error(
'Unexpected argument. Expected no argument or '
+ 'an identifier to a registered props validator',
v.pos
);
}
var validator = PropsValidator.get(k);
if (validator == null) {
Context.error(
'Error: cannot find props validator "$k"',
v.pos
);
} else {
var validatedValue = validator(field, value);
if (validatedValue != null) return validatedValue;
}
}
} else {
return value;
}
case TFun([{t: TType(_.toString() => "react.ACCEPTS_MORE_PROPS", _)}], _):
return value;
default:
#if !react_jsx_no_data_for_components
// Always allow data- props
if (StringTools.startsWith(field, "data-")) return value;
#end
#if (!react_jsx_no_aria || !react_jsx_no_aria_for_components)
// Always allow valid aria- attributes
if (StringTools.startsWith(field, "aria-")) {
var ct = AriaAttributes.map[field];
if (ct != null) return macro @:pos(value.pos) (${value} :$ct);
}
#end
}
}
}
// Support "import as"
function deepFollow(t:Type) {
if (t == null) return null;
return TypeTools.map(Context.follow(t), deepFollow);
}
var t = deepFollow(Context.typeof(macro {
var __pseudo = $target;
// Position used for value type mismatch
@:pos(value.pos) __pseudo = $value;
}));
if (isTMono) {
#if !react_ignore_failed_props_inference
Context.warning(
'Type checking failed: unable to infer '
+ 'needed props for ${ExprTools.toString(type)}',
type.pos
);
#end
return value;
}
var ct = TypeTools.toComplexType(t);
if (ct == null) return value;
var typedExpr = try {
Context.typeExpr(macro @:pos(value.pos) ($value :$ct));
} catch (e:haxe.macro.Error) {
if (StringTools.startsWith(e.message, "Type not found")) {
var t1 = MacroUtil.tryFollow(t);
if (t1 == null) t1 = MacroUtil.tryMapFollow(t);
if (t1 != null) t = t1;
try {
var ct = TypeTools.toComplexType(t);
Context.typeExpr(macro @:pos(value.pos) ($value :$ct));
} catch (e:haxe.macro.Error) {
Context.error(e.message, e.pos.or(value.pos));
}
} else {
Context.error(e.message, e.pos.or(value.pos));
}
};
return Context.storeTypedExpr(typedExpr);
}
}
var t = type.typeof().sure();
try {
if (!Context.unify(t, Context.getType('react.ReactType')))
{
Context.error(
'JSX error: invalid node "${ExprTools.toString(type)}"',
nodePos
);
}
} catch (e:Dynamic) {
Context.error(
'JSX error: invalid node "${ExprTools.toString(type)}"',
nodePos
);
}
function resolveHtmlTag(tag:String):Null<Expr> {
for (kind in ["normal", "opaque", "void"]) {
try {
var e = macro (null:tink.domspec.Tags).$kind.$tag;
Context.typeof(e);
return e;
} catch (_) {}
}
return null;
}
return isHtml
? {
children: function() return REACT_FRAGMENT_CT,
#if tink_domspec
// See below in typeChecker about needed special handling
propsType: resolveHtmlTag(type.getString().sure()),
#else
propsType: null,
#end
neededAttrs: [],
typeChecker: function(name:StringAt, value:Expr) {
var prop = name.value;
// data-* attributes are all possible, with (iirc) String type?
if (StringTools.startsWith(prop, "data-")) return macro @:pos(value.pos) ($value :String);
#if !react_jsx_no_aria
// Type valid aria- attributes
// TODO: consider displaying warning for unknown `aria-` props
if (StringTools.startsWith(prop, "aria-")) {
var ct = AriaAttributes.map[prop];
if (ct != null) return macro @:pos(value.pos) (${value} :$ct);
}
#end
#if (css_types && !react_jsx_no_css_types)
if (name.value == "style")
return macro @:pos(value.pos) (${value} :haxe.extern.EitherType<css.Properties, String>);
#end
#if tink_domspec
var tagExpr = resolveHtmlTag(type.getString().sure());
if (tagExpr != null) {
// Be forgiving about compatibility with tink_domspec, because:
// - some things don't work the same (event handlers, react-specific attributes, etc.)
// - some attributes are still missing (input.form, a.name, ...)
// TODO: add special handling for dangerouslSetInnerHTML etc.
try {
var prop = name.value;
Context.typeof(macro $tagExpr.$prop);
var check = () -> {
Context.typeof(macro {
var o = null;
o = $tagExpr.$prop;
// Position used for value type mismatch
@:pos(value.pos) o = $value;
$value;
});
return macro {};
};
return macro {
${check.bounce()}
$value;
};
} catch (e) {
#if react.debugDomspec
Context.warning(e.message, name.pos);
#end
}
}
#end
return value;
}
}
: switch (t) {
case TAbstract(_.toString() => "react.ReactTypeOf", [tProps])
| TAbstract(_.toString() => "Null", [TAbstract(_.toString() => "react.ReactTypeOf", [tProps])]):
var ctProps = TypeTools.toComplexType(tProps);
{
children: extractChildrenType(macro @:pos(nodePos) (null:$ctProps).children),
propsType: macro (null:$ctProps),
neededAttrs: extractNeededAttrs(tProps),
typeChecker: propsFor(macro (null:$ctProps))
};
case TFun(args, ret):
switch (args) {
case []:
{
children: function() return macro :react.Empty,
propsType: macro (null:react.Empty),
neededAttrs: [],
typeChecker: function (_, e:Expr) {
e.reject('no props allowed here');
return e;
}
};
case [v]:
var propsType = macro {
var o = null;
$type(o);
o;
};
{
children: extractChildrenType(macro @:pos(nodePos) {
var o = null;
$type(o);
o.children;
}),
neededAttrs: extractNeededAttrs(v.t),
propsType: propsType,
typeChecker: propsFor(propsType)
};
case v:
throw 'assert'; //TODO: do something meaningful here
}
case TInst(_.toString() => "String", []):
{
propsType: null,
children: function() return macro :react.Empty,
neededAttrs: [],
typeChecker: function(_, e:Expr) return macro $e
};
default:
var typeExpr = macro {
function get<T>(c:Class<T>):T return null;
@:privateAccess get($type).props;
};
{
propsType: typeExpr,
children: extractChildrenType(macro @:pos(nodePos) {
function get<T>(c:Class<T>):T return null;
@:privateAccess get($type).props.children;
}),
neededAttrs: extractNeededAttrs(Context.typeof(typeExpr)),
typeChecker: propsFor(typeExpr)
};
}
}
static function extractChildrenType(type:Expr):Void->ComplexType {
return function() {
try {
var t = Context.typeof(type);
return TypeTools.toComplexType(t);
} catch (e:Dynamic) {}
return REACT_FRAGMENT_CT;
}
}
static var neededAttrsCache:Map<String, Array<String>> = new Map();
static function extractNeededAttrs(type:Type) {
var key = Std.string(type);
if (neededAttrsCache.exists(key)) return neededAttrsCache.get(key);
var neededAttrs = [];
var skipCache = false;
function hasEmptyAttrs(name:String):Bool {
return switch (name) {
case "react.Empty": true;
case "react.BasePropsWithoutChildren": true;
case "react.BasePropsWithOptChild": true;
case "react.BasePropsWithOptChildren": true;
case _: false;
};
}
function hasOnlyChildren(name:String):Bool {
return switch (name) {
case "react.BasePropsWithChild": true;
case "react.BasePropsWithChildren": true;
case _: false;
};
}
switch (type) {
case TDynamic(null):
case TType(_.toString() => name, []) if (hasEmptyAttrs(name)):
case TType(_.toString() => name, []) if (hasOnlyChildren(name)):
neededAttrs.push("children");
case TAnonymous(_.get().fields => fields) |
TType(_.get() => _.type => TAnonymous(_.get().fields => fields), _):
skipCache = true;
for (f in fields) if (!f.meta.has(':optional')) neededAttrs.push(f.name);
default:
}
if (!skipCache) neededAttrsCache.set(key, neededAttrs);
return neededAttrs;
}
#end
}
#if macro
class HxxEscape {
public var expr:Expr;
public function new(expr:Expr) this.expr = expr;
}
#end