Skip to content

Commit 179d3db

Browse files
author
Timothy Lindvall
committed
feat: Validate block-syntax-version.
The block-syntax version of the file is checked. There's no later version to upgrade to right now, so for now we just validate that the version is declared and that it's version 1.
1 parent a0c9c64 commit 179d3db

File tree

2 files changed

+94
-3
lines changed

2 files changed

+94
-3
lines changed

Diff for: packages/@css-blocks/core/src/BlockParser/BlockFactory.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Block } from "../BlockTree";
88
import { Options, ResolvedConfiguration, resolveConfiguration } from "../configuration";
99
import { CssBlockError } from "../errors";
1010
import { FileIdentifier, ImportedCompiledCssFile, ImportedFile, Importer } from "../importing";
11+
import { upgradeDefinitionFileSyntax } from "../PrecompiledDefinitions/block-syntax-version";
1112
import { sourceRange } from "../SourceLocation";
1213
import { PromiseQueue } from "../util/PromiseQueue";
1314

@@ -312,9 +313,8 @@ export class BlockFactory {
312313
return this.blocks[file.identifier];
313314
}
314315

315-
// NOTE: If we had to upgrade the syntax version of a definition file, here's where'd we do that.
316-
// But this isn't a thing we need to do until we have multiple syntax versions.
317-
// TODO: Actually look at the declared version - error if it's greater than 1.
316+
// Update definition data to use latest block syntax.
317+
file = upgradeDefinitionFileSyntax(file);
318318

319319
// NOTE: No need to run preprocessor - we assume that Compiled CSS has already been preprocessed.
320320
// Parse the definition file into an AST
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import { ImportedCompiledCssFile } from "../importing";
2+
import { CssBlockError } from "../errors";
3+
4+
/**
5+
* A regex to find the block-syntax-version annotation in a definition file.
6+
*/
7+
const REGEXP_BLOCK_SYNTAX_VERSION = /^@block-syntax-version (\d+);/;
8+
9+
/**
10+
* The earliest block-syntax-version supported by CSS Blocks. If this has been
11+
* bumped past version 1, that means there's a breaking change in a later version
12+
* of CSS Blocks that can't be automatically updated.
13+
*/
14+
const MIN_SUPPORTED_VERSION = 1;
15+
16+
/**
17+
* The latest block-syntax-version supported by CSS Blocks. Anytime a patch method
18+
* is introduced to transform older definition files automatically, bump this number.
19+
* If a version later than this is found in a definition file, that means it was compiled
20+
* by a later version of CSS Blocks and we can't reason about it.
21+
*/
22+
const MAX_SUPPORTED_VERSION = 1;
23+
24+
/**
25+
* Given a Compiled CSS file, determine the block-syntax-version for the
26+
* definition file.
27+
* @param file - The Compiled CSS file to look up the version number from.
28+
* @returns The version number found in the definition file.
29+
*/
30+
export function determineBlockSyntaxVersion(file: ImportedCompiledCssFile): number {
31+
const dfnId = file.definitionIdentifier;
32+
const dfnContents = file.definitionContents;
33+
const versionLookupResult = dfnContents.match(REGEXP_BLOCK_SYNTAX_VERSION);
34+
35+
if (!versionLookupResult) {
36+
throw new CssBlockError("Unable to process definition file because the file is missing a block-syntax-version declaration.", {
37+
filename: dfnId
38+
});
39+
}
40+
41+
try {
42+
const version = parseInt(versionLookupResult[1], 10);
43+
return version;
44+
} catch {
45+
throw new CssBlockError("Unable to process definition file because the declared block-syntax version isn't a number.", {
46+
filename: dfnId
47+
});
48+
}
49+
}
50+
51+
/**
52+
* Updates the definition data in a Compiled CSS file to use the current
53+
* block-syntax-version supported by this version of CSS Blocks. We attempt
54+
* to make these transformations automatically; however, there are three
55+
* scenarios we will have to throw an error on if they occur...
56+
*
57+
* 1. We can't find the block-syntax-version or parse it into a number.
58+
* 2. The version is greater than the block-syntax-version supported in
59+
* this version of CSS Blocks. (This implies that a later version of
60+
* CSS Blocks compiled this file and bumping CSS Blocks may resolve
61+
* the issue.)
62+
* 3. The version is less than the earliest block-syntax-version supported
63+
* in this version of CSS Blocks. (This means that a much earlier version
64+
* of CSS Blocks compiled the file using some syntax that can't be
65+
* automatically upgraded.)
66+
*
67+
* @param file - The Compiled CSS file to update.
68+
* @returns The ImportedCompiledCSSFile with the transformed definition file contents.
69+
*/
70+
export function upgradeDefinitionFileSyntax(file: ImportedCompiledCssFile): ImportedCompiledCssFile {
71+
const version = determineBlockSyntaxVersion(file);
72+
73+
if (version > MAX_SUPPORTED_VERSION) {
74+
throw new CssBlockError("Unable to process definition file because the syntax version of the definition file is greater than supported by CSS Blocks. You can fix this issue by upgrading CSS Blocks to the latest version.", {
75+
filename: file.definitionIdentifier
76+
});
77+
}
78+
if (version < MIN_SUPPORTED_VERSION) {
79+
throw new CssBlockError("Unable to process definition file because the syntax of the definition can't be automatically upgraded to the latest version supported by CSS Blocks. You may be able to fix this issue by upgrading the dependency or origin file this definition file was generated from. Otherwise, you'll need to use an earlier version of CSS Blocks.", {
80+
filename: file.definitionIdentifier
81+
});
82+
}
83+
84+
// NOTE: If we had to upgrade the syntax version of a definition file, here's where'd we do that.
85+
// But this isn't a thing we need to do until we have multiple syntax versions.
86+
87+
// NOTE: This should be a new ImportedCompiledCssFile that has the definitionContents upgraded to
88+
// the current supported version, but, again, we haven't upgraded anything yet.
89+
// So, for now, just echo back the file.
90+
return file;
91+
}

0 commit comments

Comments
 (0)