Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
prettier.format(code);
// $ExpectError (Can't use unsupported options)
prettier.format(code, { printWith: 80 });
prettier.format(code, { printWidth: 80 });
// $ExpectError (Options should have proper types)
prettier.format(code, { parser: "flo" });
prettier.format(code, { parser: "flow" });
// $ExpectError (Same as above, but with explicit annotation)
const badOptions: Options = { parser: "flo" };
const goodOptions: Options = { parser: "flow" };
// $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 });
files.forEach(file => {
const { path, content } = file
process.stdout.write('\b'.repeat(10000))
process.stdout.write(chalk.grey(`INFO: check ${path}`))
const valid = prettier.check(content, { filepath: path })
file.check = valid
if (!valid && !program.lint) {
print(chalk.red(`ERROR: ${path} lint check fails`))
process.exit(1)
}
})
process.stdout.write('\b'.repeat(10000))
files.forEach(file => {
const options = prettier.resolveConfig.sync(file, {
config: prettierConfigPath,
});
try {
const fileInfo = prettier.getFileInfo.sync(file);
if (fileInfo.ignored) {
return;
}
const input = fs.readFileSync(file, 'utf8');
const withParserOptions = {
...options,
parser: fileInfo.inferredParser,
};
const isPrettier = prettier.check(input, withParserOptions);
if (!isPrettier) {
console.log(`\x1b[31m ${file} is no prettier, please use npm run prettier and git add !\x1b[0m`);
didWarn = true;
}
} catch (e) {
didError = true;
}
});
async function main() {
const paths = await getFilePathsToFormat();
const workspacePath = getWorkspacePath();
const failedPaths = [];
for (const path of paths) {
const absolutePath = `${workspacePath}/${path}`;
const config = await (prettier as any).resolveConfig(absolutePath);
const source = fs.readFileSync(absolutePath, { encoding: "utf-8" });
process.stdout.write(`${path} `);
if (
!prettier.check(source, {
...config,
filepath: path,
})
) {
process.stdout.write(chalk.red("FORMAT_ERRORS") + "\n");
failedPaths.push(path);
} else {
process.stdout.write(chalk.green("OK") + "\n");
}
}
if (failedPaths.length) {
process.stderr.write(
`\n${chalk.yellow("Some files need formatting; to fix, run:\nbazel run //tools/prettier:fix")}\n`
);
process.exit(1);
files.forEach(file => {
const options = prettier.resolveConfig.sync(file, {
config: prettierConfigPath,
});
try {
const input = fs.readFileSync(file, 'utf8');
if (shouldWrite) {
const output = prettier.format(input, options);
if (output !== input) {
fs.writeFileSync(file, output, 'utf8');
}
} else {
if (!prettier.check(input, options)) {
if (!didWarn) {
console.log(
'\n' +
chalk.red(
` This project uses prettier to format all JavaScript code.\n`
) +
chalk.dim(` Please run `) +
chalk.reset('yarn prettier-all') +
chalk.dim(
` and add changes to files listed below to your commit:`
) +
`\n\n`
);
didWarn = true;
}
console.log(file);
for (const relative of files) {
onExamineFile && onExamineFile(relative);
const file = join(directory, relative);
const options = Object.assign(
{},
prettier.resolveConfig.sync(file, {
config,
editorconfig: true
}),
{ filepath: file }
);
const input = readFileSync(file, 'utf8');
if (check) {
const isFormatted = prettier.check(input, options);
onCheckFile && onCheckFile(relative, isFormatted);
continue;
}
const output = prettier.format(input, options);
if (output !== input) {
writeFileSync(file, output);
onWriteFile && onWriteFile(relative);
}
}
};
async function main() {
const paths = await getFilePathsToFormat();
const workspacePath = getWorkspacePath();
for (const path of paths) {
const absolutePath = `${workspacePath}/${path}`;
const config = await prettier.resolveConfig(absolutePath);
const source = fs.readFileSync(absolutePath, { encoding: "utf-8" });
const options = { ...config, filepath: path };
if (!prettier.check(source, options)) {
fs.writeFileSync(absolutePath, prettier.format(source, options));
process.stdout.write(`${path} ${chalk.blue("FIXED")}\n`);
}
}
}
files.forEach(file => {
const formatOptions = {
...options,
parser: file.endsWith('.less') ? 'less' : 'babylon',
};
const input = fs.readFileSync(file, 'utf-8');
if (!prettier.check(input, formatOptions)) {
notFormattedFiles = [...notFormattedFiles, file];
}
});
function prettierCheck(vinyl) {
const prettierOpt = Object.assign({ filepath: vinyl.path }, pkg.prettier)
vinyl.prettierIsInvalid = !prettier.check(vinyl.contents, prettierOpt)
return vinyl
}