/
parser.js
49 lines (49 loc) · 1.54 KB
/
parser.js
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
import PropTypes from 'prop-types';
import React from 'react';
import { parse, print, types } from 'recast';
import ViewSrc from './Views/ViewSrc';
import { ErrorCatch } from './Views/ErrorCatch';
/**
* HOC Parse code into AST and reverse onChange
* @param {React.Component} Comp Component to augment
*/
function parsed(Comp) {
/**
* @param {{value:string, onChange:(code:string)=>void}} props Component props
*/
function Parsed(props) {
const { value, onChange, ...restProps } = props;
let error = '';
let ast;
try {
ast = parse(value);
} catch (e) {
error = e.description;
// should show code string instead of falling back to an empty program
ast = types.builders.file(types.builders.program([]));
}
return (
<ViewSrc value={value} onChange={onChange} error={error} readOnly={props.view.readOnly}>
<ErrorCatch code={value} onChange={onChange}>
<Comp
{...restProps}
code={ast.program.body}
onChange={v => {
ast.program.body = v;
onChange(print(ast).code);
}}
/>
</ErrorCatch>
</ViewSrc>
);
}
Parsed.propTypes = {
value: PropTypes.string,
onChange: PropTypes.func,
};
Parsed.defaultProps = {
value: '',
};
return Parsed;
}
export default parsed;