Skip to content

Commit

Permalink
fix: consistent with the ESLint Formatter behavior
Browse files Browse the repository at this point in the history
- use 1-based line and 1-based column
- use absolute path instead of relative path
  • Loading branch information
ikatyang committed Jul 27, 2023
1 parent 5daa251 commit cf8541a
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 95 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
"jest-snapshot-serializer-ansi": "2.1.0",
"prettier": "3.0.0",
"standard-version": "9.5.0",
"tempy": "3.1.0",
"typescript": "5.1.6",
"vite": "4.4.6",
"vitest": "0.33.0"
Expand Down
43 changes: 1 addition & 42 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 57 additions & 0 deletions scripts/jest-snapshot-serializer-path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import * as fs from "node:fs/promises";
import * as os from "node:os";
import * as path from "node:path";

const TMP_DIR = await fs.realpath(os.tmpdir());
const ROOT_DIR = path.resolve(__dirname, "..");

function test(value: any) {
return hasString(value, (_) => _.includes(ROOT_DIR) || _.includes(TMP_DIR));
}

function print(value: unknown, serialize: (value: unknown) => string) {
return serialize(
mapString(value, (_) =>
_.replaceAll(ROOT_DIR, "<rootDir>") //
.replaceAll(TMP_DIR, "<tmpDir>"),
),
);
}

export default { test, print };

function hasString(value: any, predicate: (value: string) => boolean): boolean {
switch (typeof value) {
case "string":
return predicate(value);
case "object":
if (Array.isArray(value)) {
return value.some((_) => hasString(_, predicate));
}
if (value) {
return Object.values(value).some((_) => hasString(_, predicate));
}
return false;
default:
return false;
}
}

function mapString<T>(value: T, transform: (value: string) => string): T {
switch (typeof value) {
case "string":
return transform(value) as T;
case "object":
if (Array.isArray(value)) {
return value.map((_) => mapString(_, transform)) as T;
}
if (value) {
return Object.fromEntries(
Object.entries(value).map(([k, v]) => [k, mapString(v, transform)]),
) as T;
}
return value;
default:
return value;
}
}
12 changes: 6 additions & 6 deletions src/__snapshots__/cli.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ exports[`fix: true 1`] = `

exports[`format: default 1`] = `
"
unformatted.js:0:7
0:7 Replace = with ·=· prettier/prettier
unformatted.js:1:8
1:8 Replace = with ·=· prettier/prettier
syntax-error.js:1:7
✖ 1:7 Unexpected token (1:7) prettier/prettier
Expand All @@ -24,7 +24,7 @@ exports[`ignorePath: ".ignore" 1`] = `
[
{
"errorCount": 0,
"filePath": "non-ignored.js",
"filePath": "<tmpDir>/src_cli_test_ts___ignorePath____ignore_/non-ignored.js",
"messages": [],
"source": "",
"warningCount": 0,
Expand All @@ -36,14 +36,14 @@ exports[`ignorePath: default, .prettierignore does not exist 1`] = `
[
{
"errorCount": 0,
"filePath": "folder/non-ignored.js",
"filePath": "<tmpDir>/src_cli_test_ts___ignorePath__default___prettierignore_does_not_exist/folder/non-ignored.js",
"messages": [],
"source": "",
"warningCount": 0,
},
{
"errorCount": 0,
"filePath": "non-ignored.js",
"filePath": "<tmpDir>/src_cli_test_ts___ignorePath__default___prettierignore_does_not_exist/non-ignored.js",
"messages": [],
"source": "",
"warningCount": 0,
Expand All @@ -55,7 +55,7 @@ exports[`ignorePath: default, .prettierignore exists 1`] = `
[
{
"errorCount": 0,
"filePath": "non-ignored.js",
"filePath": "<tmpDir>/src_cli_test_ts___ignorePath__default___prettierignore_exists/non-ignored.js",
"messages": [],
"source": "",
"warningCount": 0,
Expand Down
24 changes: 12 additions & 12 deletions src/__snapshots__/diagnose.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ exports[`operation: delete 1`] = `
{
"deleteText": " ",
"end": {
"column": 9,
"line": 0,
"column": 10,
"line": 1,
"offset": 9,
},
"insertText": "",
"message": "Delete \`·\`",
"operation": 0,
"start": {
"column": 8,
"line": 0,
"column": 9,
"line": 1,
"offset": 8,
},
},
Expand All @@ -28,16 +28,16 @@ exports[`operation: insert 1`] = `
{
"deleteText": "",
"end": {
"column": 7,
"line": 0,
"column": 8,
"line": 1,
"offset": 7,
},
"insertText": " ",
"message": "Insert \`·\`",
"operation": 1,
"start": {
"column": 7,
"line": 0,
"column": 8,
"line": 1,
"offset": 7,
},
},
Expand All @@ -49,16 +49,16 @@ exports[`operation: replace 1`] = `
{
"deleteText": "'a'",
"end": {
"column": 13,
"line": 0,
"column": 14,
"line": 1,
"offset": 13,
},
"insertText": "\\"a\\"",
"message": "Replace \`'a'\` with \`\\"a\\"\`",
"operation": 2,
"start": {
"column": 10,
"line": 0,
"column": 11,
"line": 1,
"offset": 10,
},
},
Expand Down
48 changes: 25 additions & 23 deletions src/cli.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import * as fs from "node:fs/promises";
import * as os from "node:os";
import * as path from "node:path";
import { copy } from "fs-extra";
import serializerAnsi from "jest-snapshot-serializer-ansi";
import { temporaryDirectoryTask } from "tempy";
import { copy, remove } from "fs-extra";
import { expect, test } from "vitest";
import { run } from "./cli.js";

expect.addSnapshotSerializer(serializerAnsi);

const TEMP_DIR = await fs.realpath(os.tmpdir());
const FIXTURES_DIRNAME = path.resolve(__dirname, "../fixtures");

test("ignorePath: default, .prettierignore exists", async () => {
Expand Down Expand Up @@ -61,25 +59,29 @@ async function runCli(fixtureName: string, argv: string[]) {
const originalConsoleLog = console.log;
const originalCwd = process.cwd();
const originalExitCode = process.exitCode;
const dirname = path.join(
TEMP_DIR,
expect.getState().currentTestName!.replace(/[^0-9a-zA-Z]/g, "_"),
);
try {
return await temporaryDirectoryTask(async (dirname) => {
const stdout: string[] = [];
await copy(path.join(FIXTURES_DIRNAME, fixtureName), dirname);
process.chdir(dirname);
console.log = (...messages) => {
if (messages.length) {
stdout.push(messages.join(" "));
}
};
const results = await run(argv);
const tree = await getTree(dirname);
return {
tree,
stdout: stdout.length === 0 ? undefined : stdout.join("\n"),
results,
exitCode: process.exitCode ?? 0,
};
});
await fs.mkdir(dirname, { recursive: true });
const stdout: string[] = [];
await copy(path.join(FIXTURES_DIRNAME, fixtureName), dirname);
process.chdir(dirname);
console.log = (...messages) => {
if (messages.length) {
stdout.push(messages.join(" "));
}
};
const results = await run(argv);
const tree = await getTree(dirname);
await remove(dirname);
return {
tree,
stdout: stdout.length === 0 ? undefined : stdout.join("\n"),
results,
exitCode: process.exitCode ?? 0,
};
} finally {
process.chdir(originalCwd);
process.exitCode = originalExitCode;
Expand Down
4 changes: 2 additions & 2 deletions src/diagnose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export enum Operation {
export interface Location {
/** 1-based */
line: number;
/** 0-based */
/** 1-based */
column: number;
/** 0-based */
offset: number;
Expand Down Expand Up @@ -65,7 +65,7 @@ export function diagnose(input: string, output: string) {
const locator = new LinesAndColumns(input);
const getLocation = (offset: number): Location => {
const { line, column } = locator.locationForIndex(offset)!;
return { line, column, offset };
return { line: line + 1, column: column + 1, offset };
};

return {
Expand Down
8 changes: 3 additions & 5 deletions src/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { FormatResult } from "./vendor/format.js";
// https://eslint.org/docs/developer-guide/working-with-custom-formatters#description-of-the-results
export interface LintResult {
/**
* The path to the file relative to the current working directory (the path from which eslint was executed).
* The absolute path to the file that was linted.
*/
filePath: string;
/**
Expand Down Expand Up @@ -63,8 +63,6 @@ export interface LintMessage {
}

export function lint(result: FormatResult): LintResult {
const relativeFilename = path.relative(".", result.filename);

const nodeType = "Unknown";
const ruleId = "prettier/prettier";

Expand All @@ -83,7 +81,7 @@ export function lint(result: FormatResult): LintResult {
);

return {
filePath: relativeFilename,
filePath: result.filename,
source: input,
...(messages.length === 0 ? {} : { output }),
messages,
Expand Down Expand Up @@ -111,7 +109,7 @@ export function lint(result: FormatResult): LintResult {
}) /* c8 ignore stop */,
};
return {
filePath: relativeFilename,
filePath: result.filename,
source: input,
messages: [message],
errorCount: 1,
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"include": ["bin/**/*", "src/**/*"],
"include": ["bin/**/*", "src/**/*", "scripts/**/*", "*"],
"compilerOptions": {
"strict": true,
"target": "ES2021",
Expand Down
Loading

0 comments on commit cf8541a

Please sign in to comment.