Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export const runPrettier = ({ code, cursorOffset }) => {
try {
return prettier.formatWithCursor(code, {
cursorOffset,
parser: 'babel',
plugins: [babylon]
});
} catch (e) {
// Just a formatting error so we pass
return null;
}
};
const formatter: CodeBlockFormatter = ({ cursorOffset, language, source }) => {
if (getLanguage({ fallback: 'text', language, supportedLanguages }) === 'typescript') {
return formatWithCursor(source, {
cursorOffset,
plugins: [typescriptPlugin],
parser: 'typescript',
singleQuote: true,
});
}
return;
};
const formatCode = ({ parser, source, cursorOffset }: FormatCodeParams) => {
return formatWithCursor(source, {
...options,
cursorOffset,
plugins,
parser,
});
};
? spreadCursor(state.cursor)
: prevState.cursor;
let [start, end] = cursor;
const newState = { ...prevState, ...{ ...state, cursor } };
let { ast, code, cursorFromAST, lastValidAST, printWidth } = newState;
if (!state.code && state.ast) {
code = generate(state.ast, { retainLines: true }).code;
}
const newWidth = printWidth !== prevState.printWidth;
if (!ast || code !== prevState.code || newWidth) {
if (prettify) {
try {
const { formatted, cursorOffset } = prettier.formatWithCursor(code, {
parser: 'babel',
plugins: [babylon],
cursorOffset: start,
printWidth,
trailingComma: 'all'
});
ast = parse(formatted);
code = formatted;
start = end = cursorOffset;
} catch (e) {
if (!(e instanceof SyntaxError)) {
throw e;
}
if (ast) {
ast = null;
}