Skip to content
Permalink
Browse files

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.
  • Loading branch information
jackson-dean committed Nov 26, 2019
1 parent 7c2ffe7 commit ddaed86af9200b41aadc2a951b6862a829555a5f
@@ -1,3 +1,4 @@
import { BlockFactory } from "@css-blocks/core";
import { GlimmerAnalyzer } from "@css-blocks/glimmer";
import * as assert from "assert";
import { TempDir, createBuilder, createTempDir } from "broccoli-test-helper";
@@ -37,14 +38,14 @@ describe("Broccoli Analyze Plugin Test", function () {
});

let transport = new Transport("test-transport");
let analyzer = new GlimmerAnalyzer({}, {}, {
let analyzer = new GlimmerAnalyzer(new BlockFactory({}), {}, {
app: { name: "test" },
types: {
stylesheet: { definitiveCollection: "components" },
template: { definitiveCollection: "components" },
},
collections: {
components: { group: "ui", types: [ "template", "stylesheet" ] },
components: { group: "ui", types: ["template", "stylesheet"] },
},
});
let output = createBuilder(new CSSBlocksAnalyze(
@@ -82,9 +83,15 @@ describe("Broccoli Analyze Plugin Test", function () {
// Accidental modification of output directory does not make the plugin explode.
fs.unlinkSync(output.path("src/ui/components/Chrisrng/template.hbs"));
input.write({
src: { ui: { components: { [entryComponentName]: {
"stylesheet.css": `:scope { color: blue; } .foo { color: yellow; }`,
}}}},
src: {
ui: {
components: {
[entryComponentName]: {
"stylesheet.css": `:scope { color: blue; } .foo { color: yellow; }`,
},
},
},
},
});
await output.build();
assert.equal(preDiff.calculatePatch(postDiff).length, 0, "Input directory unchanged after rebuild.");
@@ -93,29 +100,47 @@ describe("Broccoli Analyze Plugin Test", function () {

// Removal of block files trigger build but result in no tree changes.
input.write({
src: { ui: { components: { [entryComponentName]: {
"stylesheet.css": null,
}}}},
src: {
ui: {
components: {
[entryComponentName]: {
"stylesheet.css": null,
},
},
},
},
});
await output.build();
assert.equal(transport["css"], "", "Removal of block files trigger build but result in no output tree changes.");
assert.deepEqual(output.changes(), {}, "Removal of block files trigger build but result in output no tree changes.");

// Addition of block files trigger build but result in no output tree changes.
input.write({
src: { ui: { components: { [entryComponentName]: {
"stylesheet.css": `:scope { color: red; } .foo { color: green; }`,
}}}},
src: {
ui: {
components: {
[entryComponentName]: {
"stylesheet.css": `:scope { color: red; } .foo { color: green; }`,
},
},
},
},
});
await output.build();
assert.equal(transport["css"], ".a { color: red; } .b { color: green; }", "Addition of block files trigger build but result in no output tree changes.");
assert.deepEqual(output.changes(), {}, "Addition of block files trigger build but result in no output tree changes.");

// Modifications to non-block files are funneled through to output.
input.write({
src: { ui: { components: { [entryComponentName]: {
"template.hbs": `<div><h1>Welcome to Glimmer!</h1></div>`,
}}}},
src: {
ui: {
components: {
[entryComponentName]: {
"template.hbs": `<div><h1>Welcome to Glimmer!</h1></div>`,
},
},
},
},
});
await output.build();
assert.equal(transport["css"], ".a { color: red; }");
@@ -1,4 +1,4 @@
import { Block, StyleMapping } from "@css-blocks/core";
import { Block, BlockFactory, StyleMapping } from "@css-blocks/core";
import { GlimmerAnalyzer } from "@css-blocks/glimmer";
import * as assert from "assert";

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

transport.css = "foobar";
transport.analyzer = new GlimmerAnalyzer({}, {}, { types: {}, collections: {} });
transport.analyzer = new GlimmerAnalyzer(new BlockFactory({}), {}, {
types: {},
collections: {},
});
transport.blocks.add(new Block("foo", "bar"));
transport.mapping = {} as StyleMapping<"GlimmerTemplates.ResolvedFile">;

@@ -8,11 +8,7 @@ import * as debugGenerator from "debug";

import { BlockFactory } from "../BlockParser";
import { Block, Style } from "../BlockTree";
import {
Options,
ResolvedConfiguration,
resolveConfiguration,
} from "../configuration";
import { ResolvedConfiguration } from "../configuration";

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

constructor (
options?: Options,
blockFactory: BlockFactory,
analysisOpts?: AnalysisOptions,
) {

// TODO: Remove April 2019 when Node.js 6 is EOL'd
if (parseInt(process.versions.node) <= 6) {
throw new Error("CSS Blocks does not support Node.js <= 6");
}

this.cssBlocksOptions = resolveConfiguration(options);
this.blockFactory = blockFactory;
this.cssBlocksOptions = blockFactory.configuration;
this.validatorOptions = analysisOpts && analysisOpts.validations || {};
this.blockFactory = new BlockFactory(this.cssBlocksOptions);
this.analysisMap = new Map();
this.staticStyles = new MultiMap();
this.dynamicStyles = new MultiMap();
}
}

abstract analyze(dir: string, entryPoints: string[]): Promise<Analyzer<K>>;
abstract get optimizationOptions(): TemplateIntegrationOptions;
@@ -16,6 +16,7 @@ import { postcss } from "opticss";
import { Analysis, Analyzer } from "../src/Analyzer";
import { ElementAnalysis } from "../src/Analyzer";
import { BlockCompiler } from "../src/BlockCompiler";
import { BlockFactory } from "../src/BlockParser";
import { AttrValue, Block, BlockClass } from "../src/BlockTree";
import { resolveConfiguration } from "../src/configuration";
import { StyleMapping } from "../src/TemplateRewriter/StyleMapping";
@@ -93,7 +94,7 @@ export class TemplateAnalysisTests {
};
}
}
let analyzer = new TestAnalyzer();
let analyzer = new TestAnalyzer(new BlockFactory(config));
return analyzer.analyze().then(async (analyzer: TestAnalyzer) => {
let compiler = new BlockCompiler(postcss, config);
let optimizer = new Optimizer({}, { rewriteIdents: { id: false, class: true} });
@@ -32,7 +32,7 @@ export class AnalysisTests {
@test "can add styles from a block"() {
let config = resolveConfiguration({});
let info = new Template("templates/my-template.hbs");
let analyzer = new TestAnalyzer();
let analyzer = new TestAnalyzer(new BlockFactory(config));
let analysis = analyzer.newAnalysis(info);
let css = `
:scope { color: blue; }
@@ -64,7 +64,7 @@ export class AnalysisTests {
@test "can add dynamic styles from a block"() {
let config = resolveConfiguration({});
let info = new Template("templates/my-template.hbs");
let analyzer = new TestAnalyzer();
let analyzer = new TestAnalyzer(new BlockFactory(config));
let analysis = analyzer.newAnalysis(info);
let css = `
:scope { color: blue; }
@@ -98,7 +98,7 @@ export class AnalysisTests {
@test "can correlate styles"() {
let config = resolveConfiguration({});
let info = new Template("templates/my-template.hbs");
let analyzer = new TestAnalyzer();
let analyzer = new TestAnalyzer(new BlockFactory(config));
let analysis = analyzer.newAnalysis(info);
let css = `
:scope { color: blue; }
@@ -139,7 +139,7 @@ export class AnalysisTests {
@test "can add styles from two blocks"() {
let config = resolveConfiguration({});
let info = new Template("templates/my-template.hbs");
let analyzer = new TestAnalyzer();
let analyzer = new TestAnalyzer(new BlockFactory(config));
let analysis = analyzer.newAnalysis(info);
let css = `
:scope { color: blue; }
@@ -183,7 +183,7 @@ export class AnalysisTests {

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

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

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

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

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

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

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

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

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

@test "addExclusiveStyles generates correct correlations when `alwaysPresent` is false"() {
let info = new Template("templates/my-template.hbs");
let analyzer = new TestAnalyzer();
let analyzer = new TestAnalyzer(new BlockFactory({}));
let analysis = analyzer.newAnalysis(info);
let { imports, config } = setupImporting();
imports.registerSource(
@@ -488,7 +488,7 @@ export class AnalysisTests {
}
@test "can generate an analysis for the optimizer"() {
let info = new Template("templates/my-template.hbs");
let analyzer = new TestAnalyzer();
let analyzer = new TestAnalyzer(new BlockFactory({}));
let analysis = analyzer.newAnalysis(info);
let { imports, config } = setupImporting();
imports.registerSource(
@@ -563,9 +563,9 @@ export class AnalysisTests {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

@test "composition test"() {
let info = new Template("templates/my-template.hbs");
let analyzer = new TestAnalyzer();
let analyzer = new TestAnalyzer(new BlockFactory({}));
let analysis = analyzer.newAnalysis(info);
let { imports, config } = setupImporting();

0 comments on commit ddaed86

Please sign in to comment.
You can’t perform that action at this time.