Skip to content

Commit a5123fc

Browse files
committed
feat: Passing block error validation through the importer.
1 parent d5bd9c3 commit a5123fc

File tree

3 files changed

+21
-37
lines changed

3 files changed

+21
-37
lines changed

packages/@css-blocks/language-server/src/Server.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,17 @@ export class Server {
5252

5353
async onDidChangeContent(e: TextDocumentChangeEvent) {
5454
// only track incremental changes within block files
55-
// NOTE: this does seem to cause a little bit of weirdness when editing a
56-
// template with errors since the error locations do not get updated until
57-
// saving the file. We may want to validate the open template files on
58-
// every change?
55+
this.blockFactory.reset();
5956
if (isBlockFile(e.document.uri)) {
60-
const cssBlockErrors = await documentContentChange(e, this.blockParser);
57+
const cssBlockErrors = await documentContentChange(e, this.blockFactory);
6158
this.sendDiagnostics(cssBlockErrors, e.document.uri);
6259

6360
} else if (isTemplateFile(e.document.uri)) {
6461
// Validate template
62+
// NOTE: this does seem to cause a little bit of weirdness when editing a
63+
// template with errors since the error locations do not get updated until
64+
// saving the file. We may want to validate the open template files on
65+
// every change?
6566
}
6667

6768
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
1-
import { CssBlockError } from "@css-blocks/core/dist/src";
2-
import { BlockParser } from "@css-blocks/core/dist/src/BlockParser/BlockParser";
1+
import { BlockFactory, CssBlockError } from "@css-blocks/core/dist/src";
32
import { TextDocumentChangeEvent } from "vscode-languageserver";
43
import { URI } from "vscode-uri";
54

6-
import { isBlockFile, parseBlockErrors } from "../util/blockUtils";
5+
import { isBlockFile } from "../util/blockUtils";
76

8-
export async function documentContentChange(e: TextDocumentChangeEvent, parser: BlockParser): Promise<CssBlockError[]> {
7+
export async function documentContentChange(e: TextDocumentChangeEvent, blockFactory: BlockFactory): Promise<CssBlockError[]> {
98
const { uri } = e.document;
109

1110
if (isBlockFile(uri)) {
12-
return await parseBlockErrors(parser, URI.parse(uri).fsPath, e.document.getText());
11+
let errors: CssBlockError[] = [];
12+
try {
13+
// parses the block file to get the block and errors if there's a problem
14+
// along the way. The importer ensures that we're getting live contents if
15+
// the block file is opened
16+
await blockFactory.getBlockFromPath(URI.parse(uri).fsPath);
17+
} catch (error) {
18+
if (error instanceof CssBlockError) {
19+
errors = errors.concat(error);
20+
}
21+
return errors;
22+
}
1323
}
14-
1524
return [];
1625
}
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,5 @@
1-
import { CssBlockError, Syntax } from "@css-blocks/core/dist/src";
2-
import { BlockParser } from "@css-blocks/core/dist/src/BlockParser/BlockParser";
3-
import { postcss } from "opticss";
4-
import * as path from "path";
5-
61
// TODO: Currently we are only supporting css. This should eventually support all
72
// of the file types supported by css blocks
83
export function isBlockFile(uriOrFsPath: string) {
94
return uriOrFsPath.endsWith(".block.css");
105
}
11-
12-
export async function parseBlockErrors(parser: BlockParser, blockFsPath: string, sourceText: string): Promise<CssBlockError[]> {
13-
let errors: CssBlockError[] = [];
14-
15-
try {
16-
await parser.parseSource({
17-
identifier: blockFsPath,
18-
defaultName: path.parse(blockFsPath).name.replace(/\.block/, ""),
19-
originalSource: sourceText,
20-
originalSyntax: Syntax.css,
21-
parseResult: postcss.parse(sourceText, { from: blockFsPath }),
22-
dependencies: [],
23-
});
24-
} catch (error) {
25-
if (error instanceof CssBlockError) {
26-
errors = errors.concat(error);
27-
}
28-
}
29-
30-
return errors;
31-
}

0 commit comments

Comments
 (0)