Skip to content

Commit 114dd63

Browse files
committed
fix: Display the file where the error occurred.
1 parent 6faf8b4 commit 114dd63

File tree

5 files changed

+28
-11
lines changed

5 files changed

+28
-11
lines changed

packages/@css-blocks/cli/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ yarn add @css-blocks/cli
1111
or run without installation:
1212

1313
```
14-
npx @css-blocks/cli --validate blocks/*.block.css
14+
npx @css-blocks/cli validate blocks/*.block.css
1515
```
1616

1717
## Usage

packages/@css-blocks/cli/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"dist",
99
"src",
1010
"README.md",
11-
"types-local"
11+
"CHANGELOG.md"
1212
],
1313
"scripts": {
1414
"test": "yarn run test:runner",

packages/@css-blocks/cli/src/index.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,18 @@ export class CLI {
7474
for (let blockFile of blockFiles) {
7575
try {
7676
await factory.getBlockFromPath(path.resolve(blockFile));
77-
this.println(`${this.chalk.green("ok")}\t${blockFile}`);
77+
this.println(`${this.chalk.green("ok")}\t${path.relative(process.cwd(), path.resolve(blockFile))}`);
7878
} catch (e) {
7979
errorCount++;
8080
if (e instanceof CssBlockError) {
8181
let loc = e.location;
82-
if (loc) {
83-
this.println(`${this.chalk.red("error")}\t${this.chalk.whiteBright(blockFile)}:${loc.line}:${loc.column} ${e.origMessage}`);
84-
} else {
85-
this.println(`${this.chalk.red("error")}\t${this.chalk.whiteBright(blockFile)} ${e.origMessage}`);
82+
let filename = path.relative(process.cwd(), path.resolve(loc && loc.filename || blockFile));
83+
let message = `${this.chalk.red("error")}\t${this.chalk.whiteBright(filename)}`;
84+
if (loc && loc.filename && loc.line && loc.column) {
85+
message += `:${loc.line}:${loc.column}`;
8686
}
87+
message += ` ${e.origMessage}`;
88+
this.println(message);
8789
} else {
8890
console.error(e);
8991
}
@@ -93,6 +95,7 @@ export class CLI {
9395
}
9496

9597
println(...args: Array<string>) {
98+
// tslint:disable-next-line:no-console
9699
console.log(...args);
97100
}
98101

packages/@css-blocks/cli/test/cli-test.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import { TestCLI as CLI } from "./TestCLI";
66
function fixture(...relativePathSegments: Array<string>): string {
77
return path.resolve(__dirname, "..", "..", "test", "fixtures", ...relativePathSegments);
88
}
9+
function relFixture(...relativePathSegments: Array<string>): string {
10+
return path.relative(process.cwd(), fixture(...relativePathSegments));
11+
}
912

1013
function distFile(...relativePathSegments: Array<string>): string {
1114
return path.resolve(__dirname, "..", ...relativePathSegments);
@@ -15,13 +18,19 @@ describe("validate", () => {
1518
it("can check syntax for a valid block file", async () => {
1619
let cli = new CLI();
1720
await cli.run(["validate", fixture("basic/simple.block.css")]);
18-
assert.equal(cli.output, `ok\t${fixture("basic/simple.block.css")}\n`);
21+
assert.equal(cli.output, `ok\t${relFixture("basic/simple.block.css")}\n`);
1922
assert.equal(cli.exitCode, 0);
2023
});
2124
it("can check syntax for a bad block file", async () => {
2225
let cli = new CLI();
2326
await cli.run(["validate", fixture("basic/error.block.css")]);
24-
assert.equal(cli.output, `error\t${fixture("basic/error.block.css")}:1:5 Two distinct classes cannot be selected on the same element: .foo.bar\n`);
27+
assert.equal(cli.output, `error\t${relFixture("basic/error.block.css")}:1:5 Two distinct classes cannot be selected on the same element: .foo.bar\n`);
28+
assert.equal(cli.exitCode, 1);
29+
});
30+
it("correctly displays errors in referenced blocks.", async () => {
31+
let cli = new CLI();
32+
await cli.run(["validate", fixture("basic/transitive-error.block.css")]);
33+
assert.equal(cli.output, `error\t${relFixture("basic/error.block.css")}:1:5 Two distinct classes cannot be selected on the same element: .foo.bar\n`);
2534
assert.equal(cli.exitCode, 1);
2635
});
2736
});
@@ -30,13 +39,13 @@ describe("validate with preprocessors", () => {
3039
it("can check syntax for a valid block file", async () => {
3140
let cli = new CLI();
3241
await cli.run(["validate", "--preprocessors", distFile("test/preprocessors"), fixture("scss/simple.block.scss")]);
33-
assert.equal(cli.output, `ok\t${fixture("scss/simple.block.scss")}\n`);
42+
assert.equal(cli.output, `ok\t${relFixture("scss/simple.block.scss")}\n`);
3443
assert.equal(cli.exitCode, 0);
3544
});
3645
it("can check syntax for a bad block file", async () => {
3746
let cli = new CLI();
3847
await cli.run(["validate", "--preprocessors", distFile("test/preprocessors"), fixture("scss/error.block.scss")]);
39-
assert.equal(cli.output, `error\t${fixture("scss/error.block.scss")}:5:5 Two distinct classes cannot be selected on the same element: .foo.bar\n`);
48+
assert.equal(cli.output, `error\t${relFixture("scss/error.block.scss")}:5:5 Two distinct classes cannot be selected on the same element: .foo.bar\n`);
4049
assert.equal(cli.exitCode, 1);
4150
});
4251
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@block error from "./error.block.css";
2+
3+
:scope {
4+
extends: error;
5+
}

0 commit comments

Comments
 (0)