Skip to content

Commit 0ec1b45

Browse files
chriseppsteinamiller-gh
authored andcommitted
chore: Use ObjectDictionary where possible.
1 parent 922fa6e commit 0ec1b45

File tree

19 files changed

+54
-73
lines changed

19 files changed

+54
-73
lines changed

Diff for: css-blocks.code-workspace

+3-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
"pseudoclass",
3535
"pseudoclasses",
3636
"pseudoelement",
37-
"rulesets"
37+
"rulesets",
38+
"preprocessor",
39+
"preprocessors"
3840
],
3941
"cSpell.ignorePaths": [
4042
"**/node_modules/**",

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ObjectDictionary } from "@opticss/util";
12
import * as debugGenerator from "debug";
23
import * as path from "path";
34
import * as postcss from "postcss";
@@ -35,13 +36,13 @@ export class BlockFactory {
3536
postcssImpl: typeof postcss;
3637
importer: Importer;
3738
options: CssBlockOptionsReadonly;
38-
blockNames: { [name: string]: number };
39+
blockNames: ObjectDictionary<number>;
3940
parser: BlockParser;
4041
preprocessors: Preprocessors;
4142

42-
private promises: { [identifier: string]: Promise<Block> };
43-
private blocks: { [identifier: string]: Block };
44-
private paths: { [path: string]: string };
43+
private promises: ObjectDictionary<Promise<Block>>;
44+
private blocks: ObjectDictionary<Block>;
45+
private paths: ObjectDictionary<string>;
4546
private preprocessQueue: PromiseQueue<PreprocessJob, ProcessedFile>;
4647

4748
constructor(options: CssBlockOptionsReadonly, postcssImpl = postcss) {

Diff for: packages/css-blocks/src/BlockParser/features/resolve-references.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ObjectDictionary } from "@opticss/util";
12
import * as postcss from "postcss";
23

34
import { Block } from "../../Block";
@@ -51,7 +52,7 @@ export async function resolveReferences(block: Block, factory: BlockFactory, fil
5152

5253
// When all import promises have resolved, save the block references and resolve.
5354
return Promise.all(namedBlockReferences).then((results) => {
54-
let localNames: {[name: string]: string} = {};
55+
let localNames: ObjectDictionary<string> = {};
5556
results.forEach(([localName, importPath, atRule, otherBlock]) => {
5657
if (localNames[localName]) {
5758
throw new errors.InvalidBlockSyntax(

Diff for: packages/css-blocks/src/TemplateAnalysis/ElementAnalysis.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,7 @@ function serializeDynamicAttrs(c: DynamicAttrs<whatever, whatever>, styleIndexes
829829
stringExpression: true,
830830
condition: true,
831831
value: 0,
832-
group: {} as {[n: string]: number},
832+
group: {} as ObjectDictionary<number>,
833833
container: 0,
834834
disallowFalsy: false,
835835
};

Diff for: packages/css-blocks/src/TemplateAnalysis/TemplateAnalysis.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ export class TemplateAnalysis<K extends keyof TemplateTypes> implements StyleAna
279279
serialize(): SerializedTemplateAnalysis<K> {
280280
let blocks = {};
281281
let stylesFound: string[] = [];
282-
let elements: { [id: string]: SerializedElementAnalysis } = {};
282+
let elements: ObjectDictionary<SerializedElementAnalysis> = {};
283283
let template = this.template.serialize();
284284
let styleNameMap = new Map<Style, string>();
285285
let styleIndexes = new Map<Style, number>();

Diff for: packages/css-blocks/src/TemplateAnalysis/validations/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import { ObjectDictionary } from "@opticss/util";
2+
13
import * as errors from "../../errors";
24
import { ElementAnalysis } from "../ElementAnalysis";
35
import { StyleAnalysis } from "../StyleAnalysis";
46

57
import { Validator } from "./Validator";
6-
78
import { attributeGroupValidator } from "./attribute-group-validator";
89
import { attributeParentValidator } from "./attribute-parent-validator";
910
import { classPairsValidator } from "./class-pairs-validator";
@@ -15,13 +16,12 @@ export * from "./root-class-validator";
1516
export * from "./attribute-parent-validator";
1617
export * from "./property-conflict-validator";
1718

18-
export interface TemplateValidators {
19+
export interface TemplateValidators extends ObjectDictionary<Validator> {
1920
"no-root-classes": Validator;
2021
"no-class-pairs": Validator;
2122
"no-attribute-orphans": Validator;
2223
"no-duplicate-attribute-groups": Validator;
2324
"no-required-resolution": Validator;
24-
[name: string]: Validator;
2525
}
2626

2727
export type TemplateValidatorOptions = {

Diff for: packages/css-blocks/src/TemplateRewriter/RewriteMapping.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,20 @@ import {
99
SimpleAttribute,
1010
SimpleTagname,
1111
} from "@opticss/template-api";
12-
import { assertNever, Maybe, maybe, objectValues } from "@opticss/util";
12+
import { assertNever, Maybe, maybe, ObjectDictionary, objectValues } from "@opticss/util";
1313
import { inspect } from "util";
1414

1515
import { Style } from "../Block";
1616

1717
import { ClassRewrite, IndexedClassRewrite } from "./ClassRewrite";
1818

19+
export type ClassExpressionMap = ObjectDictionary<BooleanExpression<number> | undefined>;
1920
export class IndexedClassMapping implements IndexedClassRewrite<Style> {
2021
inputs: Style[];
2122
staticClasses: string[];
22-
private map: { [k: string]: BooleanExpression<number> | undefined };
23+
private map: ClassExpressionMap;
2324
private _inputMap: Map<Style, number>;
24-
constructor(inputs: Style[], staticClasses: string[], map: {[k: string]: BooleanExpression<number> | undefined}) {
25+
constructor(inputs: Style[], staticClasses: string[], map: ClassExpressionMap) {
2526
this.inputs = inputs;
2627
this.staticClasses = staticClasses;
2728
this._inputMap = new Map<Style, number>();

Diff for: packages/css-blocks/src/importing.ts

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { whatever } from "@opticss/util";
1+
import { ObjectDictionary, whatever } from "@opticss/util";
22
import * as fs from "fs";
33
import * as path from "path";
44

@@ -12,9 +12,7 @@ declare module "./options" {
1212
}
1313
}
1414

15-
export interface ImporterData {
16-
[key: string]: whatever;
17-
}
15+
export type ImporterData = ObjectDictionary<whatever>;
1816

1917
/**
2018
* A FileIdentifier is a string with a whatever internal encoding is needed to uniquely resolve
@@ -179,12 +177,7 @@ export interface Alias {
179177
path: string;
180178
}
181179

182-
export type PathAliases = Alias[] | {
183-
/**
184-
* map of alias prefixes to absolute paths.
185-
*/
186-
[alias: string]: string;
187-
};
180+
export type PathAliases = Alias[] | ObjectDictionary<string>;
188181

189182
/**
190183
* The PathAliasImporter is a replacement for the fileystem importer. Relative import paths

Diff for: packages/css-blocks/test/util/MockImportRegistry.ts

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ObjectDictionary } from "@opticss/util";
12
import { assert } from "chai";
23
import * as path from "path";
34

@@ -6,17 +7,12 @@ import { OptionsReader } from "../../src/OptionsReader";
67
import { ImportedFile, Importer, PathBasedImporter } from "../../src/importing";
78

89
const PROJECT_DIR = path.resolve(__dirname, "../../..");
9-
10-
export interface SourceRegistry {
11-
[sourcePath: string]: {
12-
contents: string;
13-
syntax: Syntax;
14-
};
15-
}
16-
17-
export interface ImportedFiles {
18-
[sourcePath: string]: boolean;
10+
export interface SourceWithSyntax {
11+
contents: string;
12+
syntax: Syntax;
1913
}
14+
export type SourceRegistry = ObjectDictionary<SourceWithSyntax>;
15+
export type ImportedFiles = ObjectDictionary<boolean>;
2016

2117
export class MockImporter extends PathBasedImporter {
2218
registry: MockImportRegistry;

Diff for: packages/jsx/src/analyzer/types.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { SourceLocation } from "@opticss/element-analysis";
2+
import { ObjectDictionary } from "@opticss/util";
23
import { Expression } from "babel-types";
34
import { ElementAnalysis } from "css-blocks";
45

@@ -7,10 +8,7 @@ export type StringExpression = Expression;
78
export type TernaryExpression = Expression;
89

910
export type JSXElementAnalysis = ElementAnalysis<BooleanExpression, StringExpression, TernaryExpression>;
10-
11-
export interface Flags {
12-
[flag: string]: boolean;
13-
}
11+
export type Flags = ObjectDictionary<boolean>;
1412

1513
export function newJSXElementAnalysis(location: SourceLocation, tagName?: string, id?: string): JSXElementAnalysis {
1614
return new ElementAnalysis<BooleanExpression, StringExpression, TernaryExpression>(location, tagName, id);

Diff for: packages/jsx/src/importer/index.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ObjectDictionary } from "@opticss/util";
12
import { NodePath } from "babel-traverse";
23
import {
34
ClassDeclaration,
@@ -27,16 +28,12 @@ const VALID_FILE_EXTENSIONS = {
2728
".jsx": 1, ".tsx": 1,
2829
};
2930

30-
interface BlockRegistry {
31-
[key: string]: number;
32-
}
33-
3431
/**
35-
* If a given block name is in the passed BlockRegistry, throw.
32+
* If a given block name is in the passed registry, throw.
3633
* @param name The Block name in question.
3734
* @param registry The registry to check.
3835
*/
39-
function throwIfRegistered(name: string, blockRegistry: BlockRegistry, loc: ErrorLocation) {
36+
function throwIfRegistered(name: string, blockRegistry: ObjectDictionary<number>, loc: ErrorLocation) {
4037
// TODO: Location reporting in errors.
4138
if (blockRegistry[name]) {
4239
throw new TemplateImportError(`Block identifier "${name}" cannot be re-defined in any scope once imported.`, loc);
@@ -57,7 +54,7 @@ export function importer(file: JSXTemplate, analysis: Analysis, blockFactory: Bl
5754

5855
// Keep a running record of local block names while traversing so we can check
5956
// for name conflicts elsewhere in the file.
60-
let _localBlocks: BlockRegistry = {};
57+
let _localBlocks: ObjectDictionary<number> = {};
6158
let dirname = path.dirname(file.identifier);
6259
let aliases = options.aliases || {};
6360

Diff for: packages/jsx/src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as fs from "fs";
22
import * as path from "path";
33

44
import {
5-
some, unwrap,
5+
ObjectDictionary, some, unwrap,
66
} from "@opticss/util";
77
import traverse from "babel-traverse";
88
import * as babylon from "babylon";
@@ -43,7 +43,7 @@ function readFile(filename: string, encoding?: string | null): Promise<string |
4343
export interface JSXAnalyzerOptions {
4444
baseDir: string;
4545
parserOptions?: object;
46-
aliases?: { [alias: string]: string };
46+
aliases?: ObjectDictionary<string>;
4747
compilationOptions?: CssBlocksOptions;
4848
}
4949

Diff for: packages/jsx/src/transformer/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { whatever } from "@opticss/util";
1+
import { ObjectDictionary, whatever } from "@opticss/util";
22
import { CssBlockOptionsReadonly, PluginOptions, PluginOptionsReader, StyleMapping } from "css-blocks";
33

44
export interface RewriterOptions {
5-
meta?: { [metaProp: string]: whatever };
5+
meta?: ObjectDictionary<whatever>;
66
cssBlocks: {
77
styleMapping: StyleMapping | null;
88
compilationOptions: PluginOptions;
@@ -18,7 +18,7 @@ export class CSSBlocksJSXTransformer {
1818

1919
styleMapping: StyleMapping | null;
2020
cssBlockOptions: CssBlockOptionsReadonly;
21-
blocks: { [path: string]: StyleMapping } = {};
21+
blocks: ObjectDictionary<StyleMapping> = {};
2222

2323
constructor(opts?: RewriterOptions) {
2424
this.cssBlockOptions = new PluginOptionsReader(opts && opts.cssBlocks && opts.cssBlocks.compilationOptions);

Diff for: packages/jsx/src/utils/Analysis.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
TemplateInfo,
44
TemplateInfoFactory,
55
} from "@opticss/template-api";
6-
import { Maybe, none, whatever } from "@opticss/util";
6+
import { Maybe, none, ObjectDictionary, whatever } from "@opticss/util";
77
import { File } from "babel-types";
88
import {
99
Block,
@@ -69,7 +69,7 @@ export class MetaAnalysis extends MetaTemplateAnalysis {
6969

7070
files: JSXTemplate[] = [];
7171
analysisPromises: Promise<Analysis>[] = [];
72-
blockPromises: { [path: string]: Promise<Block> } = {};
72+
blockPromises: ObjectDictionary<Promise<Block>> = {};
7373

7474
fileCount(): number {
7575
return this.analyses.length;

Diff for: packages/webpack-plugin/src/CssAssets.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ObjectDictionary } from "@opticss/util";
12
import * as async from "async";
23
import * as convertSourceMap from "convert-source-map";
34
import * as debugGenerator from "debug";
@@ -82,17 +83,13 @@ export interface ConcatenationOptions {
8283
*/
8384
export interface CssAssetsOptions {
8485
/** Maps css files from a source location to a webpack asset location. */
85-
cssFiles: {
86-
[assetPath: string]: string | CssSourceOptions;
87-
};
86+
cssFiles: ObjectDictionary<string | CssSourceOptions>;
8887
/**
8988
* Maps several webpack assets to a new concatenated asset and manages their
9089
* sourcemaps. The concatenated asset will belong to all the chunks to which
9190
* the assets belonged.
9291
*/
93-
concat: {
94-
[concatAssetPath: string]: string[] | ConcatenationOptions;
95-
};
92+
concat: ObjectDictionary<string[] | ConcatenationOptions>;
9693

9794
/**
9895
* When true, any source maps related to the assets are written out as

Diff for: packages/webpack-plugin/src/LoaderOptions.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1+
import { ObjectDictionary } from "@opticss/util";
2+
13
import { WebpackAny } from "./Plugin";
2-
export interface LoaderOptions {
3-
[opt: string]: WebpackAny;
4-
}
4+
export type LoaderOptions = ObjectDictionary<WebpackAny>;

Diff for: packages/webpack-plugin/src/Plugin.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { RawSource, Source, SourceMapSource } from "webpack-sources";
99
import {
1010
TemplateTypes,
1111
} from "@opticss/template-api";
12+
import { ObjectDictionary } from "@opticss/util";
1213
import {
1314
Block,
1415
BlockCompiler,
@@ -58,9 +59,7 @@ export interface BlockCompilationComplete {
5859
optimizerActions: Actions;
5960
}
6061

61-
interface Assets {
62-
[key: string]: Source;
63-
}
62+
type Assets = ObjectDictionary<Source>;
6463

6564
interface CompilationResult {
6665
optimizationResult: OptimizationResult;

Diff for: packages/webpack-plugin/test/configs/templateConfig.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
TemplateInfo,
77
TemplateInfoFactory,
88
} from "@opticss/template-api";
9-
import { whatever } from "@opticss/util";
9+
import { ObjectDictionary, whatever } from "@opticss/util";
1010
import {
1111
Block,
1212
BlockFactory,
@@ -56,7 +56,7 @@ export class TestTemplateInfo implements TemplateInfo<"WebpackPlugin.TestTemplat
5656
TemplateInfoFactory.constructors["WebpackPlugin.TestTemplate"] = TestTemplateInfo.deserialize;
5757

5858
class TestAnalysis extends TemplateAnalysis<"WebpackPlugin.TestTemplate"> {
59-
blocks: { [name: string]: Block } = {};
59+
blocks: ObjectDictionary<Block> = {};
6060
constructor(template: TestTemplateInfo) {
6161
super(template);
6262
}

Diff for: packages/webpack-plugin/test/util/MockImportRegistry.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
import { assert } from "chai";
22
import * as path from "path";
33

4+
import { ObjectDictionary } from "@opticss/util";
45
import { ImportedFile, Importer, PathBasedImporter, PluginOptionsReader, Syntax } from "css-blocks";
56

67
const PROJECT_DIR = path.resolve(__dirname, "../../..");
78

8-
export interface SourceRegistry {
9-
[sourcePath: string]: string;
10-
}
11-
12-
export interface ImportedFiles {
13-
[sourcePath: string]: boolean;
14-
}
9+
export type SourceRegistry = ObjectDictionary<string>;
10+
export type ImportedFiles = ObjectDictionary<boolean>;
1511

1612
export class MockImporter extends PathBasedImporter {
1713
registry: MockImportRegistry;

0 commit comments

Comments
 (0)