Skip to content

Commit 0d9286a

Browse files
committed
fix(jsx): Better error messages for stray references.
Closes #170.
1 parent 156ab43 commit 0d9286a

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

packages/@css-blocks/jsx/src/Analyzer/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export class CSSBlocksJSXAnalyzer extends Analyzer<TEMPLATE_TYPE> {
6767
promises.push(this.parseFile(entryPoint));
6868
}
6969
await Promise.all(promises);
70+
debug(`Found ${this.analysisPromises.size} analysis promises`);
7071
return this;
7172
}
7273

packages/@css-blocks/jsx/src/transformer/babel.ts

+4
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,11 @@ function detectStrayReferenceToImport(
211211
if (ref.type === "Identifier"
212212
&& (<Identifier>ref.node).name === specifier.local.name
213213
&& !isRemoved(ref)) {
214+
if (!filename.endsWith("x")) {
215+
console.warn(`WARNING: For performance reasons, only jsx and tsx files are properly analyzed for block dependencies and rewritten. Consider renaming ${filename} to ${filename}x as well as any leading to importing it from the entry point.`);
216+
}
214217
console.warn(`WARNING: Stray reference to block import (${specifier.local.name}). Imports are removed during rewrite so this will probably be a runtime error. (${filename}:${ref.node.loc.start.line}:${ref.node.loc.start.column})`);
218+
console.warn(`WARNING: This usually happens when a style reference is incorrectly used outside a jsx expression. But sometimes when an application is improperly configured. Be sure that only files ending in jsx or tsx are involved in the importing of components using css blocks.`);
215219
// throw new TemplateAnalysisError(`Stray reference to block import (${specifier.local.name}). Imports are removed during rewrite.`, {filename, ...ref.node.loc.start});
216220
}
217221
}

packages/@css-blocks/jsx/test/transformer/transformer-test.ts

+35-2
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ export class Test {
457457
});
458458
}
459459

460-
@test "Left over references to the block are an error"() {
460+
@test "Left over references to the block are a warning"() {
461461
mock({
462462
"bar.block.css": ":scope { color: red; } .foo { color: blue; }",
463463
"foo.block.css": ":scope { font-family: sans-serif; } .big { font-size: 28px; }",
@@ -482,8 +482,41 @@ export class Test {
482482
return transform(code, analysis.getAnalysis(0)).then(_res => {
483483
let result = stderr.output;
484484
stderr.restore();
485-
assert.deepEqual(result.length, 1);
485+
assert.deepEqual(result.length, 2);
486486
assert.deepEqual(result[0].trim(), "WARNING: Stray reference to block import (foo). Imports are removed during rewrite so this will probably be a runtime error. (test.tsx:10:11)");
487+
assert.deepEqual(result[1].trim(), "WARNING: This usually happens when a style reference is incorrectly used outside a jsx expression. But sometimes when an application is improperly configured. Be sure that only files ending in jsx or tsx are involved in the importing of components using css blocks.");
488+
});
489+
});
490+
}
491+
@test "Left over references in a js file to the block are a warning"() {
492+
mock({
493+
"bar.block.css": ":scope { color: red; } .foo { color: blue; }",
494+
"foo.block.css": ":scope { font-family: sans-serif; } .big { font-size: 28px; }",
495+
});
496+
497+
let code = `
498+
import foo from 'foo.block.css'
499+
import objstr from 'obj-str';
500+
501+
function render(){
502+
let style = objstr({
503+
[foo]: true,
504+
});
505+
let unusedStyle = objstr({
506+
[foo.big]: true,
507+
});
508+
return ( <div class={style}></div> );
509+
}`;
510+
511+
return parse(code, "test.js").then((analysis: Analyzer) => {
512+
let stderr = testConsole.stderr.inspect();
513+
return transform(code, analysis.getAnalysis(0)).then(_res => {
514+
let result = stderr.output;
515+
stderr.restore();
516+
assert.deepEqual(result.length, 3);
517+
assert.deepEqual(result[0].trim(), "WARNING: For performance reasons, only jsx and tsx files are properly analyzed for block dependencies and rewritten. Consider renaming test.js to test.jsx as well as any leading to importing it from the entry point.");
518+
assert.deepEqual(result[1].trim(), "WARNING: Stray reference to block import (foo). Imports are removed during rewrite so this will probably be a runtime error. (test.js:10:11)");
519+
assert.deepEqual(result[2].trim(), "WARNING: This usually happens when a style reference is incorrectly used outside a jsx expression. But sometimes when an application is improperly configured. Be sure that only files ending in jsx or tsx are involved in the importing of components using css blocks.");
487520
});
488521
});
489522
}

0 commit comments

Comments
 (0)