included doc debug

master
Lucas Duailibe 2018-04-12 11:51:49 -03:00
parent 9e1a0d2e72
commit 871ae13bb6
3 changed files with 53 additions and 22 deletions

View File

@ -52,7 +52,11 @@ class Playground extends React.Component {
this.handleOptionValueChange = this.handleOptionValueChange.bind(this); this.handleOptionValueChange = this.handleOptionValueChange.bind(this);
this.setContent = content => this.setState({ content }); this.setContent = content => this.setState({ content });
this.clearContent = () => this.setState({ content: "" }); this.clearContent = this.setContent.bind(this, "");
this.resetOptions = () =>
this.setState(state => ({
options: getDefaultOptions(state.availableOptions)
}));
} }
componentDidMount() { componentDidMount() {
@ -62,12 +66,10 @@ class Playground extends React.Component {
.postMessage({ type: "meta" }) .postMessage({ type: "meta" })
.then(({ supportInfo, version }) => { .then(({ supportInfo, version }) => {
const availableOptions = parsePrettierOptions(supportInfo); const availableOptions = parsePrettierOptions(supportInfo);
const options = const options = Object.assign(
this.state.options || getDefaultOptions(availableOptions),
availableOptions.reduce((acc, option) => { this.state.options
acc[option.name] = option.default; );
return acc;
}, {});
this.setState({ loaded: true, availableOptions, options, version }); this.setState({ loaded: true, availableOptions, options, version });
}); });
@ -107,15 +109,24 @@ class Playground extends React.Component {
checked={editorState.showAst} checked={editorState.showAst}
onChange={editorState.toggleAst} onChange={editorState.toggleAst}
/> />
<Checkbox
label="show doc"
checked={editorState.showDoc}
onChange={editorState.toggleDoc}
/>
</SidebarCategory> </SidebarCategory>
<div className="sub-options">
<Button onClick={this.resetOptions}>Reset to defaults</Button>
</div>
</Sidebar> </Sidebar>
<PrettierFormat <PrettierFormat
worker={this._worker} worker={this._worker}
code={content} code={content}
options={options} options={options}
ast={editorState.showAst} debugAst={editorState.showAst}
debugDoc={editorState.showDoc}
> >
{({ ast: codeAst, formatted }) => ( {({ formatted, debugAst, debugDoc }) => (
<div className="editors"> <div className="editors">
<InputPanel <InputPanel
mode={getCodemirrorMode(options.parser)} mode={getCodemirrorMode(options.parser)}
@ -124,10 +135,10 @@ class Playground extends React.Component {
onChange={this.setContent} onChange={this.setContent}
/> />
{editorState.showAst ? ( {editorState.showAst ? (
<DebugPanel value={codeAst} /> <DebugPanel value={debugAst} />
) : null} ) : null}
{editorState.showDoc ? ( {editorState.showDoc ? (
<DebugPanel value={"doc here"} /> <DebugPanel value={debugDoc} />
) : null} ) : null}
<OutputPanel <OutputPanel
mode={getCodemirrorMode(options.parser)} mode={getCodemirrorMode(options.parser)}
@ -158,6 +169,13 @@ class Playground extends React.Component {
} }
} }
function getDefaultOptions(availableOptions) {
return availableOptions.reduce((acc, option) => {
acc[option.name] = option.default;
return acc;
}, {});
}
function getCodemirrorMode(parser) { function getCodemirrorMode(parser) {
switch (parser) { switch (parser) {
case "css": case "css":

View File

@ -11,20 +11,21 @@ export default class extends React.Component {
} }
componentDidUpdate(prevProps) { componentDidUpdate(prevProps) {
const { code, options, ast } = this.props; const { code, options, debugAst, debugDoc } = this.props;
if ( if (
prevProps.code !== code || prevProps.code !== code ||
prevProps.options !== options || prevProps.options !== options ||
prevProps.ast !== ast prevProps.debugAst !== debugAst ||
prevProps.debugDoc !== debugDoc
) { ) {
this.format(); this.format();
} }
} }
format() { format() {
const { code, options, worker, ast } = this.props; const { code, options, worker, debugAst, debugDoc } = this.props;
worker worker
.postMessage({ type: "format", code, options, ast }) .postMessage({ type: "format", code, options, debugAst, debugDoc })
.then(result => this.setState(result)); .then(result => this.setState(result));
} }

View File

@ -90,16 +90,17 @@ self.onmessage = function(event) {
const response = { const response = {
formatted: formatCode(message.code, options), formatted: formatCode(message.code, options),
ast: null, debugAst: null,
doc: null debugDoc: null
}; };
if (message.ast) { if (message.debugAst) {
var actualAst; var ast;
var errored = false; var errored = false;
try { try {
actualAst = prettier.__debug.parse(message.code, options).ast; ast = JSON.stringify(
ast = JSON.stringify(actualAst); prettier.__debug.parse(message.code, options).ast
);
} catch (e) { } catch (e) {
errored = true; errored = true;
ast = String(e); ast = String(e);
@ -112,7 +113,18 @@ self.onmessage = function(event) {
ast = JSON.stringify(actualAst, null, 2); ast = JSON.stringify(actualAst, null, 2);
} }
} }
response.ast = ast; response.debugAst = ast;
}
if (message.debugDoc) {
try {
response.debugDoc = prettier.__debug.formatDoc(
prettier.__debug.printToDoc(message.code, options),
{ parser: "babylon" }
);
} catch (e) {
response.debugDoc = String(e);
}
} }
self.postMessage({ self.postMessage({