Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// $ExpectError (Must pass in some source code)
prettier.check();
prettier.check(code);
// $ExpectError (Can't use unsupported options)
prettier.check(code, { sem: true });
prettier.check(code, { semi: true });
// $ExpectError (Options should have proper types)
prettier.check(code, { singleQuote: "true" });
prettier.check(code, { singleQuote: true });
// $ExpectError (Must include CursorOptions)
prettier.formatWithCursor(code);
// $ExpectError (CursorOptions must have cursorOffset)
prettier.formatWithCursor(code, {});
// $ExpectError (CursorOptions cannot have rangeStart or rangeEnd)
prettier.formatWithCursor(code, { cursorOffset: 10, rangeStart: 1 });
prettier.formatWithCursor(code, { cursorOffset: 10 });
// $ExpectError (Must include filePath)
prettier.resolveConfig();
prettier.resolveConfig("/path");
const asyncConfig = prettier.resolveConfig("/path");
if (asyncConfig != null) {
// $ExpectError (Returns promise)
(asyncConfig.printWidth: number);
}
(prettier.resolveConfig("/path"): Promise);
// $ExpectError (Options should have proper types)
function prettyprint(src, options) {
const result = prettier.formatWithCursor(src, options);
if (options.cursorOffset >= 0) {
result.formatted =
result.formatted.slice(0, result.cursorOffset) +
'<|>' +
result.formatted.slice(result.cursorOffset);
}
return result.formatted;
}
}
});
/**
* Test return types
*/
let result;
result = prettier.format(code);
(result: string);
result = prettier.check(code);
(result: boolean);
result = prettier.formatWithCursor(code, { cursorOffset: 1 });
(result.formatted: string);
(result.cursorOffset: number);
result = prettier.resolveConfig("/");
(result.then: Function);
result = prettier.clearConfigCache();
(result: void);
result = prettier.getSupportInfo("1.0");
(result.languages: Array<*>);
(result.options: Array<*>);
.then(options => {
try {
const formatted = prettier.formatWithCursor(
content,
Object.assign({ filepath: file, cursorOffset: req.body.cursorOffset }, options || DEFAULT_PRETTIER_OPTIONS)
);
res.write(JSON.stringify({ content: formatted }));
} catch (err) {
console.log('Failed to format code: ', err);
res.write(JSON.stringify({ content, error: 'Failed to format code.', err }));
}
res.end();
})
.catch(err => {