Skip to content

Commit

Permalink
Enable unicorn/template-indent rule (#12469)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Mar 17, 2022
1 parent 361dc14 commit ae080df
Show file tree
Hide file tree
Showing 13 changed files with 122 additions and 104 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Expand Up @@ -153,6 +153,7 @@ module.exports = {
"unicorn/prefer-string-starts-ends-with": "error",
"unicorn/prefer-switch": "error",
"unicorn/prefer-type-error": "error",
"unicorn/template-indent": "error",
},
overrides: [
{
Expand Down
4 changes: 2 additions & 2 deletions scripts/release/utils.js
Expand Up @@ -103,9 +103,9 @@ function getBlogPostInfo(version) {

function getChangelogContent({ version, previousVersion, body }) {
return outdent`
[diff](https://github.com/prettier/prettier/compare/${previousVersion}...${version})
[diff](https://github.com/prettier/prettier/compare/${previousVersion}...${version})
${body}
${body}
`;
}

Expand Down
11 changes: 6 additions & 5 deletions tests/config/install-prettier.js
Expand Up @@ -2,6 +2,7 @@

const path = require("path");
const fs = require("fs");
const { outdent } = require("outdent");
const { default: chalk } = require("../../vendors/chalk.js");
const { default: tempy } = require("../../vendors/tempy.js");
const { execaSync } = require("../../vendors/execa.js");
Expand Down Expand Up @@ -74,12 +75,12 @@ module.exports = (packageDir) => {

console.log(
chalk.green(
outdent`
Prettier installed
at ${chalk.inverse(installed)}
from ${chalk.inverse(packageDir)}
with ${chalk.inverse(client)}.
`
Prettier installed
at ${chalk.inverse(installed)}
from ${chalk.inverse(packageDir)}
with ${chalk.inverse(client)}.
`.trim()
)
);

Expand Down
12 changes: 6 additions & 6 deletions tests/config/utils/check-parsers.js
Expand Up @@ -163,13 +163,13 @@ const checkParser = ({ dirname, files }, parsers = []) => {
suggestCategories.length === 0
? ""
: outdent`
Suggest move your tests to:
${suggestCategories
.map((category) => `- ${path.join(TESTS_ROOT, category)}`)
.join("\n")}
Suggest move your tests to:
${suggestCategories
.map((category) => `- ${path.join(TESTS_ROOT, category)}`)
.join("\n")}
Or config to allow use this parser in "${__filename}".
`;
Or config to allow use this parser in "${__filename}".
`;

throw new Error(
`Parser "${parser}" should not used in "${dirname}".${
Expand Down
22 changes: 14 additions & 8 deletions tests/format/js/cursor/jsfmt.spec.js
@@ -1,6 +1,7 @@
run_spec(__dirname, ["babel", "typescript", "flow"]);

const prettier = require("prettier-local");
const { outdent } = require("outdent");

test("translates cursor correctly in basic case", () => {
expect(
Expand Down Expand Up @@ -32,17 +33,22 @@ test("keeps cursor inside formatted node", () => {
});

test("doesn't insert second placeholder for nonexistent TypeAnnotation", () => {
const code = `
foo('bar', cb => {
console.log('stuff')
})`;
const code =
"\n" +
outdent`
foo('bar', cb => {
console.log('stuff')
})
`;
expect(
prettier.formatWithCursor(code, { parser: "babel", cursorOffset: 24 })
).toMatchObject({
formatted: `foo("bar", (cb) => {
console.log("stuff");
});
`,
formatted:
outdent`
foo("bar", (cb) => {
console.log("stuff");
});
` + "\n",
cursorOffset: 25,
});
});
Expand Down
11 changes: 5 additions & 6 deletions tests/integration/__tests__/__snapshots__/format.js.snap
Expand Up @@ -12,12 +12,11 @@ exports[`html parser should handle CRLF correctly 1`] = `"\\"<!--\\\\r\\\\n tes
exports[`markdown parser should handle CRLF correctly 1`] = `"\\"\`\`\`\\\\r\\\\n\\\\r\\\\n\\\\r\\\\n\`\`\`\\\\r\\\\n\\""`;

exports[`typescript parser should throw the first error when both JSX and non-JSX mode failed 1`] = `
"Expression expected. (9:7)
7 | );
8 |
> 9 | label:
| ^
10 | "
"Expression expected. (8:7)
6 | );
7 |
> 8 | label:
| ^"
`;

exports[`yaml parser should handle CRLF correctly 1`] = `"\\"a: 123\\\\r\\\\n\\""`;
15 changes: 9 additions & 6 deletions tests/integration/__tests__/doc-trim.js
@@ -1,6 +1,7 @@
"use strict";

const prettier = require("prettier-local");
const { outdent } = require("outdent");
const docPrinter = prettier.doc.printer;
const docBuilders = prettier.doc.builders;

Expand Down Expand Up @@ -34,12 +35,14 @@ describe("trim", () => {
"}",
])
),
`function()
{
#if DEBUG
alert(42);
#endif
}`,
outdent`
function()
{
#if DEBUG
alert(42);
#endif
}
`,
],
[
"ignores trimmed characters when fitting the line",
Expand Down
15 changes: 8 additions & 7 deletions tests/integration/__tests__/format.js
@@ -1,6 +1,7 @@
"use strict";

const prettier = require("prettier-local");
const { outdent } = require("outdent");
const fooPlugin = require("../plugins/defaultOptions/plugin.js");

test("yaml parser should handle CRLF correctly", () => {
Expand All @@ -14,15 +15,15 @@ test("yaml parser should handle CRLF correctly", () => {
});

test("typescript parser should throw the first error when both JSX and non-JSX mode failed", () => {
const input = `
import React from "react";
const input = outdent`
import React from "react";
const App = () => (
<div className="App">
</div>
);
const App = () => (
<div className="App">
</div>
);
label:
label:
`;
expect(() =>
prettier.format(input, { parser: "typescript" })
Expand Down
3 changes: 1 addition & 2 deletions tests/integration/__tests__/ignore-in-subdirectories.js
Expand Up @@ -70,8 +70,7 @@ describe("formats files when executing in a subdirectory and using stdin", () =>
input: "hello_world( );",
}
).test({
stdout: `hello_world();
`,
stdout: "hello_world();\n",
status: 0,
});
});
13 changes: 8 additions & 5 deletions tests/integration/__tests__/line-suffix-boundary.js
Expand Up @@ -2,6 +2,7 @@

/** @type {import('prettier')} */
const prettier = require("prettier-local");
const { outdent } = require("outdent");

const { group, indent, line, lineSuffix, lineSuffixBoundary, softline } =
prettier.doc.builders;
Expand All @@ -24,11 +25,13 @@ describe("lineSuffixBoundary", () => {
"];",
]);

const expected = `let foo = [
item1,
item2, // comment
item3
];`;
const expected = outdent`
let foo = [
item1,
item2, // comment
item3
];
`;

expect(printDoc(doc)).toBe(expected);
});
Expand Down
25 changes: 13 additions & 12 deletions tests/integration/__tests__/load-toml.js
@@ -1,22 +1,23 @@
"use strict";

const { outdent } = require("outdent");
const loadToml = require("../../../src/utils/load-toml.js");

describe("TOML", () => {
const exampleFilePath = "example.toml";

const exampleToml = `
# This is a TOML document.
title = "TOML Example"
[owner]
name = "Tom Preston-Werner"
dob = 1979-05-27T07:32:00-08:00 # First class dates
[database]
server = "192.168.1.1"
ports = [ 8001, 8001, 8002 ]
connection_max = 5000
enabled = true
`;
const exampleToml = outdent`
# This is a TOML document.
title = "TOML Example"
[owner]
name = "Tom Preston-Werner"
dob = 1979-05-27T07:32:00-08:00 # First class dates
[database]
server = "192.168.1.1"
ports = [ 8001, 8001, 8002 ]
connection_max = 5000
enabled = true
`;

const wrongToml = "///ERROR///";

Expand Down
61 changes: 31 additions & 30 deletions tests/integration/__tests__/stdin-filepath.js
@@ -1,6 +1,7 @@
"use strict";

const { isCI } = require("ci-info");
const { outdent } = require("outdent");
const runPrettier = require("../run-prettier.js");

describe("format correctly if stdin content compatible with stdin-filepath", () => {
Expand Down Expand Up @@ -38,11 +39,11 @@ describe("apply editorconfig for stdin-filepath with nonexistent file", () => {
"cli",
["--stdin-filepath", "config/editorconfig/nonexistent.js"],
{
input: `
function f() {
console.log("should be indented with a tab");
}
`.trim(), // js
input: outdent`
function f() {
console.log("should be indented with a tab");
}
`, // js
}
).test({
status: 0,
Expand All @@ -54,11 +55,11 @@ describe("apply editorconfig for stdin-filepath with nonexistent directory", ()
"cli",
["--stdin-filepath", "config/editorconfig/nonexistent/one/two/three.js"],
{
input: `
function f() {
console.log("should be indented with a tab");
}
`.trim(), // js
input: outdent`
function f() {
console.log("should be indented with a tab");
}
`, // js
}
).test({
status: 0,
Expand All @@ -70,11 +71,11 @@ describe("apply editorconfig for stdin-filepath with a deep path", () => {
"cli",
["--stdin-filepath", "config/editorconfig/" + "a/".repeat(30) + "three.js"],
{
input: `
function f() {
console.log("should be indented with a tab");
}
`.trim(), // js
input: outdent`
function f() {
console.log("should be indented with a tab");
}
`, // js
}
).test({
status: 0,
Expand All @@ -83,11 +84,11 @@ function f() {

if (isCI) {
describe("apply editorconfig for stdin-filepath in root", () => {
const code = `
function f() {
console.log("should be indented with a tab");
}
`.trim();
const code = outdent`
function f() {
console.log("should be indented with a tab");
}
`;
runPrettier("cli", ["--stdin-filepath", "/foo.js"], {
input: code, // js
}).test({
Expand All @@ -104,11 +105,11 @@ describe("apply editorconfig for stdin-filepath with a deep path", () => {
"cli",
["--stdin-filepath", "config/editorconfig/" + "a/".repeat(30) + "three.js"],
{
input: `
function f() {
console.log("should be indented with a tab");
}
`.trim(), // js
input: outdent`
function f() {
console.log("should be indented with a tab");
}
`, // js
}
).test({
status: 0,
Expand All @@ -123,11 +124,11 @@ describe("don’t apply editorconfig outside project for stdin-filepath with non
"config/editorconfig/repo-root/nonexistent/one/two/three.js",
],
{
input: `
function f() {
console.log("should be indented with 2 spaces");
}
`.trim(), // js
input: outdent`
function f() {
console.log("should be indented with 2 spaces");
}
`, // js
}
).test({
status: 0,
Expand Down

0 comments on commit ae080df

Please sign in to comment.