Skip to content

Commit ddaed86

Browse files
author
Jackson Dean
committed
refactor: Moves initialization of block factory out of analyzer.
For the vscode language server, we need be able to pass the block factory instance that we've already instantiated.
1 parent 7c2ffe7 commit ddaed86

18 files changed

+144
-118
lines changed

packages/@css-blocks/broccoli/test/Analyze.ts

+39-14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { BlockFactory } from "@css-blocks/core";
12
import { GlimmerAnalyzer } from "@css-blocks/glimmer";
23
import * as assert from "assert";
34
import { TempDir, createBuilder, createTempDir } from "broccoli-test-helper";
@@ -37,14 +38,14 @@ describe("Broccoli Analyze Plugin Test", function () {
3738
});
3839

3940
let transport = new Transport("test-transport");
40-
let analyzer = new GlimmerAnalyzer({}, {}, {
41+
let analyzer = new GlimmerAnalyzer(new BlockFactory({}), {}, {
4142
app: { name: "test" },
4243
types: {
4344
stylesheet: { definitiveCollection: "components" },
4445
template: { definitiveCollection: "components" },
4546
},
4647
collections: {
47-
components: { group: "ui", types: [ "template", "stylesheet" ] },
48+
components: { group: "ui", types: ["template", "stylesheet"] },
4849
},
4950
});
5051
let output = createBuilder(new CSSBlocksAnalyze(
@@ -82,9 +83,15 @@ describe("Broccoli Analyze Plugin Test", function () {
8283
// Accidental modification of output directory does not make the plugin explode.
8384
fs.unlinkSync(output.path("src/ui/components/Chrisrng/template.hbs"));
8485
input.write({
85-
src: { ui: { components: { [entryComponentName]: {
86-
"stylesheet.css": `:scope { color: blue; } .foo { color: yellow; }`,
87-
}}}},
86+
src: {
87+
ui: {
88+
components: {
89+
[entryComponentName]: {
90+
"stylesheet.css": `:scope { color: blue; } .foo { color: yellow; }`,
91+
},
92+
},
93+
},
94+
},
8895
});
8996
await output.build();
9097
assert.equal(preDiff.calculatePatch(postDiff).length, 0, "Input directory unchanged after rebuild.");
@@ -93,29 +100,47 @@ describe("Broccoli Analyze Plugin Test", function () {
93100

94101
// Removal of block files trigger build but result in no tree changes.
95102
input.write({
96-
src: { ui: { components: { [entryComponentName]: {
97-
"stylesheet.css": null,
98-
}}}},
103+
src: {
104+
ui: {
105+
components: {
106+
[entryComponentName]: {
107+
"stylesheet.css": null,
108+
},
109+
},
110+
},
111+
},
99112
});
100113
await output.build();
101114
assert.equal(transport["css"], "", "Removal of block files trigger build but result in no output tree changes.");
102115
assert.deepEqual(output.changes(), {}, "Removal of block files trigger build but result in output no tree changes.");
103116

104117
// Addition of block files trigger build but result in no output tree changes.
105118
input.write({
106-
src: { ui: { components: { [entryComponentName]: {
107-
"stylesheet.css": `:scope { color: red; } .foo { color: green; }`,
108-
}}}},
119+
src: {
120+
ui: {
121+
components: {
122+
[entryComponentName]: {
123+
"stylesheet.css": `:scope { color: red; } .foo { color: green; }`,
124+
},
125+
},
126+
},
127+
},
109128
});
110129
await output.build();
111130
assert.equal(transport["css"], ".a { color: red; } .b { color: green; }", "Addition of block files trigger build but result in no output tree changes.");
112131
assert.deepEqual(output.changes(), {}, "Addition of block files trigger build but result in no output tree changes.");
113132

114133
// Modifications to non-block files are funneled through to output.
115134
input.write({
116-
src: { ui: { components: { [entryComponentName]: {
117-
"template.hbs": `<div><h1>Welcome to Glimmer!</h1></div>`,
118-
}}}},
135+
src: {
136+
ui: {
137+
components: {
138+
[entryComponentName]: {
139+
"template.hbs": `<div><h1>Welcome to Glimmer!</h1></div>`,
140+
},
141+
},
142+
},
143+
},
119144
});
120145
await output.build();
121146
assert.equal(transport["css"], ".a { color: red; }");

packages/@css-blocks/broccoli/test/Transport.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Block, StyleMapping } from "@css-blocks/core";
1+
import { Block, BlockFactory, StyleMapping } from "@css-blocks/core";
22
import { GlimmerAnalyzer } from "@css-blocks/glimmer";
33
import * as assert from "assert";
44

@@ -14,7 +14,10 @@ describe("Broccoli Transport Test", () => {
1414
assert.equal(transport.mapping, undefined);
1515

1616
transport.css = "foobar";
17-
transport.analyzer = new GlimmerAnalyzer({}, {}, { types: {}, collections: {} });
17+
transport.analyzer = new GlimmerAnalyzer(new BlockFactory({}), {}, {
18+
types: {},
19+
collections: {},
20+
});
1821
transport.blocks.add(new Block("foo", "bar"));
1922
transport.mapping = {} as StyleMapping<"GlimmerTemplates.ResolvedFile">;
2023

packages/@css-blocks/core/src/Analyzer/Analyzer.ts

+5-10
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@ import * as debugGenerator from "debug";
88

99
import { BlockFactory } from "../BlockParser";
1010
import { Block, Style } from "../BlockTree";
11-
import {
12-
Options,
13-
ResolvedConfiguration,
14-
resolveConfiguration,
15-
} from "../configuration";
11+
import { ResolvedConfiguration } from "../configuration";
1612

1713
import { Analysis, SerializedAnalysis } from "./Analysis";
1814
import { TemplateValidatorOptions } from "./validations";
@@ -39,22 +35,21 @@ export abstract class Analyzer<K extends keyof TemplateTypes> {
3935
protected dynamicStyles: MultiMap<Style, Analysis<K>>;
4036

4137
constructor (
42-
options?: Options,
38+
blockFactory: BlockFactory,
4339
analysisOpts?: AnalysisOptions,
4440
) {
4541

4642
// TODO: Remove April 2019 when Node.js 6 is EOL'd
4743
if (parseInt(process.versions.node) <= 6) {
4844
throw new Error("CSS Blocks does not support Node.js <= 6");
4945
}
50-
51-
this.cssBlocksOptions = resolveConfiguration(options);
46+
this.blockFactory = blockFactory;
47+
this.cssBlocksOptions = blockFactory.configuration;
5248
this.validatorOptions = analysisOpts && analysisOpts.validations || {};
53-
this.blockFactory = new BlockFactory(this.cssBlocksOptions);
5449
this.analysisMap = new Map();
5550
this.staticStyles = new MultiMap();
5651
this.dynamicStyles = new MultiMap();
57-
}
52+
}
5853

5954
abstract analyze(dir: string, entryPoints: string[]): Promise<Analyzer<K>>;
6055
abstract get optimizationOptions(): TemplateIntegrationOptions;

packages/@css-blocks/core/test/opticss-test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { postcss } from "opticss";
1616
import { Analysis, Analyzer } from "../src/Analyzer";
1717
import { ElementAnalysis } from "../src/Analyzer";
1818
import { BlockCompiler } from "../src/BlockCompiler";
19+
import { BlockFactory } from "../src/BlockParser";
1920
import { AttrValue, Block, BlockClass } from "../src/BlockTree";
2021
import { resolveConfiguration } from "../src/configuration";
2122
import { StyleMapping } from "../src/TemplateRewriter/StyleMapping";
@@ -93,7 +94,7 @@ export class TemplateAnalysisTests {
9394
};
9495
}
9596
}
96-
let analyzer = new TestAnalyzer();
97+
let analyzer = new TestAnalyzer(new BlockFactory(config));
9798
return analyzer.analyze().then(async (analyzer: TestAnalyzer) => {
9899
let compiler = new BlockCompiler(postcss, config);
99100
let optimizer = new Optimizer({}, { rewriteIdents: { id: false, class: true} });

packages/@css-blocks/core/test/template-analysis-test.ts

+28-27
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class AnalysisTests {
3232
@test "can add styles from a block"() {
3333
let config = resolveConfiguration({});
3434
let info = new Template("templates/my-template.hbs");
35-
let analyzer = new TestAnalyzer();
35+
let analyzer = new TestAnalyzer(new BlockFactory(config));
3636
let analysis = analyzer.newAnalysis(info);
3737
let css = `
3838
:scope { color: blue; }
@@ -64,7 +64,7 @@ export class AnalysisTests {
6464
@test "can add dynamic styles from a block"() {
6565
let config = resolveConfiguration({});
6666
let info = new Template("templates/my-template.hbs");
67-
let analyzer = new TestAnalyzer();
67+
let analyzer = new TestAnalyzer(new BlockFactory(config));
6868
let analysis = analyzer.newAnalysis(info);
6969
let css = `
7070
:scope { color: blue; }
@@ -98,7 +98,7 @@ export class AnalysisTests {
9898
@test "can correlate styles"() {
9999
let config = resolveConfiguration({});
100100
let info = new Template("templates/my-template.hbs");
101-
let analyzer = new TestAnalyzer();
101+
let analyzer = new TestAnalyzer(new BlockFactory(config));
102102
let analysis = analyzer.newAnalysis(info);
103103
let css = `
104104
:scope { color: blue; }
@@ -139,7 +139,7 @@ export class AnalysisTests {
139139
@test "can add styles from two blocks"() {
140140
let config = resolveConfiguration({});
141141
let info = new Template("templates/my-template.hbs");
142-
let analyzer = new TestAnalyzer();
142+
let analyzer = new TestAnalyzer(new BlockFactory(config));
143143
let analysis = analyzer.newAnalysis(info);
144144
let css = `
145145
:scope { color: blue; }
@@ -183,7 +183,7 @@ export class AnalysisTests {
183183

184184
@test "adding dynamic styles enumerates correlation in analysis"() {
185185
let info = new Template("templates/my-template.hbs");
186-
let analyzer = new TestAnalyzer();
186+
let analyzer = new TestAnalyzer(new BlockFactory({}));
187187
let analysis = analyzer.newAnalysis(info);
188188
let { imports, config } = setupImporting();
189189

@@ -234,7 +234,7 @@ export class AnalysisTests {
234234

235235
@test "multiple dynamic values added using `addExclusiveStyles` enumerate correlations correctly in analysis"() {
236236
let info = new Template("templates/my-template.hbs");
237-
let analyzer = new TestAnalyzer();
237+
let analyzer = new TestAnalyzer(new BlockFactory({}));
238238
let analysis = analyzer.newAnalysis(info);
239239
let { config } = setupImporting();
240240

@@ -273,7 +273,7 @@ export class AnalysisTests {
273273

274274
@test "multiple exclusive dynamic values added using enumerate correlations correctly in analysis"() {
275275
let info = new Template("templates/my-template.hbs");
276-
let analyzer = new TestAnalyzer();
276+
let analyzer = new TestAnalyzer(new BlockFactory({}));
277277
let analysis = analyzer.newAnalysis(info);
278278
let { config } = setupImporting();
279279

@@ -338,7 +338,7 @@ export class AnalysisTests {
338338

339339
@test "toggling between two classes with states of the same name"() {
340340
let info = new Template("templates/my-template.hbs");
341-
let analyzer = new TestAnalyzer();
341+
let analyzer = new TestAnalyzer(new BlockFactory({}));
342342
let analysis = analyzer.newAnalysis(info);
343343
let { imports, config } = setupImporting();
344344

@@ -392,7 +392,7 @@ export class AnalysisTests {
392392

393393
@test "addExclusiveStyles generates correct correlations when `alwaysPresent` is true"() {
394394
let info = new Template("templates/my-template.hbs");
395-
let analyzer = new TestAnalyzer();
395+
let analyzer = new TestAnalyzer(new BlockFactory({}));
396396
let analysis = analyzer.newAnalysis(info);
397397
let { imports, config } = setupImporting();
398398
imports.registerSource(
@@ -442,7 +442,7 @@ export class AnalysisTests {
442442

443443
@test "addExclusiveStyles generates correct correlations when `alwaysPresent` is false"() {
444444
let info = new Template("templates/my-template.hbs");
445-
let analyzer = new TestAnalyzer();
445+
let analyzer = new TestAnalyzer(new BlockFactory({}));
446446
let analysis = analyzer.newAnalysis(info);
447447
let { imports, config } = setupImporting();
448448
imports.registerSource(
@@ -488,7 +488,7 @@ export class AnalysisTests {
488488
}
489489
@test "can generate an analysis for the optimizer"() {
490490
let info = new Template("templates/my-template.hbs");
491-
let analyzer = new TestAnalyzer();
491+
let analyzer = new TestAnalyzer(new BlockFactory({}));
492492
let analysis = analyzer.newAnalysis(info);
493493
let { imports, config } = setupImporting();
494494
imports.registerSource(
@@ -563,9 +563,9 @@ export class AnalysisTests {
563563

564564
@test "correlating two classes from the same block on the same element throws an error"() {
565565
let info = new Template("templates/my-template.hbs");
566-
let analyzer = new TestAnalyzer();
567-
let analysis = analyzer.newAnalysis(info);
568566
let config = resolveConfiguration({});
567+
let analyzer = new TestAnalyzer(new BlockFactory(config));
568+
let analysis = analyzer.newAnalysis(info);
569569

570570
let css = `
571571
:scope { color: blue; }
@@ -590,9 +590,10 @@ export class AnalysisTests {
590590

591591
@test "built-in template validators may be configured with boolean values"() {
592592
let info = new Template("templates/my-template.hbs");
593-
let analyzer = new TestAnalyzer({}, { validations: { "no-class-pairs": false }});
594-
let analysis = analyzer.newAnalysis(info);
595593
let config = resolveConfiguration({});
594+
let blockFactory = new BlockFactory(config);
595+
let analyzer = new TestAnalyzer(blockFactory, { validations: { "no-class-pairs": false }});
596+
let analysis = analyzer.newAnalysis(info);
596597

597598
let css = `
598599
:scope { color: blue; }
@@ -613,9 +614,9 @@ export class AnalysisTests {
613614

614615
@test "custom template validators may be passed to analysis"() {
615616
let info = new Template("templates/my-template.hbs");
616-
let analyzer = new TestAnalyzer({}, { validations: { customValidator(data, _a, err) { if (data) err("CUSTOM ERROR"); } } });
617-
let analysis = analyzer.newAnalysis(info);
618617
let config = resolveConfiguration({});
618+
let analyzer = new TestAnalyzer(new BlockFactory(config), { validations: { customValidator(data, _a, err) { if (data) err("CUSTOM ERROR"); } } });
619+
let analysis = analyzer.newAnalysis(info);
619620

620621
let css = `
621622
:scope { color: blue; }
@@ -633,9 +634,9 @@ export class AnalysisTests {
633634

634635
@test "adding both root and a class from the same block to the same elment throws an error"() {
635636
let info = new Template("templates/my-template.hbs");
636-
let analyzer = new TestAnalyzer();
637-
let analysis = analyzer.newAnalysis(info);
638637
let config = resolveConfiguration({});
638+
let analyzer = new TestAnalyzer(new BlockFactory(config));
639+
let analysis = analyzer.newAnalysis(info);
639640

640641
let css = `
641642
:scope { color: blue; }
@@ -661,9 +662,9 @@ export class AnalysisTests {
661662

662663
@test "adding both root and a state from the same block to the same element is allowed"() {
663664
let info = new Template("templates/my-template.hbs");
664-
let analyzer = new TestAnalyzer();
665-
let analysis = analyzer.newAnalysis(info);
666665
let config = resolveConfiguration({});
666+
let analyzer = new TestAnalyzer(new BlockFactory(config));
667+
let analysis = analyzer.newAnalysis(info);
667668

668669
let css = `
669670
:scope { color: blue; }
@@ -685,7 +686,7 @@ export class AnalysisTests {
685686

686687
@test "classes from other blocks may be added to the root element"() {
687688
let info = new Template("templates/my-template.hbs");
688-
let analyzer = new TestAnalyzer();
689+
let analyzer = new TestAnalyzer(new BlockFactory({}));
689690
let analysis = analyzer.newAnalysis(info);
690691
let { imports, config } = setupImporting();
691692

@@ -733,7 +734,7 @@ export class AnalysisTests {
733734

734735
@test "throws when states are applied without their parent root"() {
735736
let info = new Template("templates/my-template.hbs");
736-
let analyzer = new TestAnalyzer();
737+
let analyzer = new TestAnalyzer(new BlockFactory({}));
737738
let analysis = analyzer.newAnalysis(info);
738739
let { config } = setupImporting();
739740

@@ -755,7 +756,7 @@ export class AnalysisTests {
755756

756757
@test "throws when states are applied without their parent BlockClass"() {
757758
let info = new Template("templates/my-template.hbs");
758-
let analyzer = new TestAnalyzer();
759+
let analyzer = new TestAnalyzer(new BlockFactory({}));
759760
let analysis = analyzer.newAnalysis(info);
760761
let { config } = setupImporting();
761762

@@ -780,7 +781,7 @@ export class AnalysisTests {
780781

781782
@test "Throws when inherited states are applied without their root"() {
782783
let info = new Template("templates/my-template.hbs");
783-
let analyzer = new TestAnalyzer();
784+
let analyzer = new TestAnalyzer(new BlockFactory({}));
784785
let analysis = analyzer.newAnalysis(info);
785786
let { imports, config } = setupImporting();
786787

@@ -822,7 +823,7 @@ export class AnalysisTests {
822823

823824
@test "Inherited states pass validation when applied with their root"() {
824825
let info = new Template("templates/my-template.hbs");
825-
let analyzer = new TestAnalyzer();
826+
let analyzer = new TestAnalyzer(new BlockFactory({}));
826827
let analysis = analyzer.newAnalysis(info);
827828
let { imports, config } = setupImporting();
828829

@@ -863,7 +864,7 @@ export class AnalysisTests {
863864

864865
@test "composition test"() {
865866
let info = new Template("templates/my-template.hbs");
866-
let analyzer = new TestAnalyzer();
867+
let analyzer = new TestAnalyzer(new BlockFactory({}));
867868
let analysis = analyzer.newAnalysis(info);
868869
let { imports, config } = setupImporting();
869870

0 commit comments

Comments
 (0)