How to use the prettier.check function in prettier

To help you get started, we’ve selected a few prettier examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github flow-typed / flow-typed / definitions / npm / prettier_v1.x.x / flow_v0.56.x-v0.83.x / test_prettier_v1.x.x.js View on Github external
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 });
github dongyuanxin / blog / bin / prettier.js View on Github external
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))
github umijs / umi-blocks / _scripts / lint-prettier.js View on Github external
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;
  }
});
github buildbuddy-io / buildbuddy / tools / prettier / check.ts View on Github external
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);
github facebook / react / scripts / prettier / index.js View on Github external
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);
github walrusjs / walrus / packages / walrus-plugin-prettier / src / process-files.ts View on Github external
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);
    }
  }
};
github buildbuddy-io / buildbuddy / tools / prettier / fix.ts View on Github external
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`);
    }
  }
}
github busyorg / busy / scripts / prettier / index.js View on Github external
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];
    }
  });
github mirabeau-nl / frontend-boilerplate / tasks / codestyle.js View on Github external
function prettierCheck(vinyl) {
  const prettierOpt = Object.assign({ filepath: vinyl.path }, pkg.prettier)
  vinyl.prettierIsInvalid = !prettier.check(vinyl.contents, prettierOpt)
  return vinyl
}