How to use prettier - 10 common examples

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 brodybits / prettierx / tests_integration / __tests__ / format.js View on Github external
test("html parser should handle CRLF correctly", () => {
  const input = "";
  expect(
    // use JSON.stringify to observe CRLF
    JSON.stringify(prettier.format(input, { parser: "html" }))
  ).toMatchSnapshot();
});
github brodybits / prettierx / tests_integration / __tests__ / format.js View on Github external
test("markdown parser should handle CRLF correctly", () => {
  const input = "```\r\n\r\n\r\n```";
  expect(
    // use JSON.stringify to observe CRLF
    JSON.stringify(prettier.format(input, { parser: "markdown" }))
  ).toMatchSnapshot();
});
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 prettier-solidity / prettier-plugin-solidity / tests_config / run_spec.js View on Github external
function parse(string, opts) {
  return prettier.__debug.parse(string, opts, /* massage */ true).ast;
}
github prettier / plugin-ruby / test / js / setupTests.js View on Github external
const { spawn, spawnSync } = require("child_process");
const path = require("path");
const prettier = require("prettier");
const readline = require("readline");

// Set RUBY_VERSION so certain tests only run for certain versions
process.env.RUBY_VERSION = spawnSync("ruby", ["-e", "puts RUBY_VERSION"])
  .stdout.toString()
  .trim();

// eslint-disable-next-line no-underscore-dangle
const { formatAST } = prettier.__debug;

const parser = spawn("ruby", ["./test/js/parser.rb"]);
afterAll(() => parser.kill());

const rl = readline.createInterface({
  input: parser.stdout,
  output: parser.stdin
});

const checkFormat = (before, after, config) =>
  new Promise(resolve => {
    const opts = Object.assign({ parser: "ruby", plugins: ["."] }, config);

    rl.question(`${before}\n---\n`, response => {
      const { formatted } = formatAST(JSON.parse(response), opts);
github dangmai / prettier-plugin-apex / tests_config / run_spec.js View on Github external
function parse(string, opts) {
  // eslint-disable-next-line no-underscore-dangle
  return prettier.__debug.parse(
    string,
    {
      apexStandaloneParser: "built-in",
      apexStandalonePort: 2117,
      apexStandaloneHost: "localhost",
      ...opts,
    },
    /* massage */ true,
  ).ast;
}
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
(syncConfig.printWidth: void | number);
}
// $ExpectError (Does not return promise)
(syncConfig: Promise);

// $ExpectError (Options should have proper types)
prettier.resolveConfig.sync("/path", { useCache: "true" });
prettier.resolveConfig.sync("/path", { useCache: true });

// $ExpectError (Must use correct name)
prettier.clearConfigCash();
prettier.clearConfigCache();

// $ExpectError (Version should be a string)
prettier.getSupportInfo(0.1);
prettier.getSupportInfo("0.1");

prettier.format(code, {
  parser() {
    // $ExpectError (Custom parser should return an ast)
    return null;
  }
});

prettier.format(code, {
  parser(text, { babylon }) {
    const ast = babylon(text);
    return ast;
  }
});

/**
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
if (syncConfig != null) {
  (syncConfig.printWidth: void | number);
}
// $ExpectError (Does not return promise)
(syncConfig: Promise);

// $ExpectError (Options should have proper types)
prettier.resolveConfig.sync("/path", { useCache: "true" });
prettier.resolveConfig.sync("/path", { useCache: true });

// $ExpectError (Must use correct name)
prettier.clearConfigCash();
prettier.clearConfigCache();

// $ExpectError (Version should be a string)
prettier.getSupportInfo(0.1);
prettier.getSupportInfo("0.1");

prettier.format(code, {
  parser() {
    // $ExpectError (Custom parser should return an ast)
    return null;
  }
});

prettier.format(code, {
  parser(text, { babylon }) {
    const ast = babylon(text);
    return ast;
  }
});
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
// $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)
github tangdrew / fhir-ts / packages / fhir-ts-codegen / scripts / download.ts View on Github external
complexTypeDefinitions.map(async structureDefinition => {
      const filename = `${structureDefinition.name.toLowerCase()}.profile.canonical.json`;
      return writeFileAsync(
        `../structure-definitions/${version}/${filename}`,
        format(JSON.stringify(structureDefinition), { parser: "json" })
      ).then(() => {
        // tslint:disable-next-line:no-console
        console.log(`Downloaded ${structureDefinition.name}`);
      });
    })
  );