Skip to content

Commit d821bbf

Browse files
author
Andy
authored
Simplify RulesMap construction (microsoft#18858)
1 parent 7aee3a1 commit d821bbf

2 files changed

Lines changed: 9 additions & 27 deletions

File tree

src/services/formatting/rulesMap.ts

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,20 @@ namespace ts.formatting {
66
public map: RulesBucket[];
77
public mapRowLength: number;
88

9-
constructor() {
10-
this.map = [];
11-
this.mapRowLength = 0;
12-
}
13-
14-
static create(rules: Rule[]): RulesMap {
15-
const result = new RulesMap();
16-
result.Initialize(rules);
17-
return result;
18-
}
19-
20-
public Initialize(rules: Rule[]) {
9+
constructor(rules: ReadonlyArray<Rule>) {
2110
this.mapRowLength = SyntaxKind.LastToken + 1;
22-
this.map = <any>new Array(this.mapRowLength * this.mapRowLength); // new Array<RulesBucket>(this.mapRowLength * this.mapRowLength);
11+
this.map = new Array<RulesBucket>(this.mapRowLength * this.mapRowLength);
2312

2413
// This array is used only during construction of the rulesbucket in the map
25-
const rulesBucketConstructionStateList: RulesBucketConstructionState[] = <any>new Array(this.map.length); // new Array<RulesBucketConstructionState>(this.map.length);
26-
27-
this.FillRules(rules, rulesBucketConstructionStateList);
28-
return this.map;
29-
}
30-
31-
public FillRules(rules: Rule[], rulesBucketConstructionStateList: RulesBucketConstructionState[]): void {
32-
rules.forEach((rule) => {
14+
const rulesBucketConstructionStateList: RulesBucketConstructionState[] = new Array<RulesBucketConstructionState>(this.map.length);
15+
for (const rule of rules) {
3316
this.FillRule(rule, rulesBucketConstructionStateList);
34-
});
17+
}
3518
}
3619

3720
private GetRuleBucketIndex(row: number, column: number): number {
3821
Debug.assert(row <= SyntaxKind.LastKeyword && column <= SyntaxKind.LastKeyword, "Must compute formatting context from tokens");
39-
const rulesBucketIndex = (row * this.mapRowLength) + column;
40-
return rulesBucketIndex;
22+
return (row * this.mapRowLength) + column;
4123
}
4224

4325
private FillRule(rule: Rule, rulesBucketConstructionStateList: RulesBucketConstructionState[]): void {
@@ -57,7 +39,7 @@ namespace ts.formatting {
5739
});
5840
}
5941

60-
public GetRule(context: FormattingContext): Rule {
42+
public GetRule(context: FormattingContext): Rule | undefined {
6143
const bucketIndex = this.GetRuleBucketIndex(context.currentTokenSpan.kind, context.nextTokenSpan.kind);
6244
const bucket = this.map[bucketIndex];
6345
if (bucket) {
@@ -74,7 +56,7 @@ namespace ts.formatting {
7456
const MaskBitSize = 5;
7557
const Mask = 0x1f;
7658

77-
export enum RulesPosition {
59+
enum RulesPosition {
7860
IgnoreRulesSpecific = 0,
7961
IgnoreRulesAny = MaskBitSize * 1,
8062
ContextRulesSpecific = MaskBitSize * 2,

src/services/formatting/rulesProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace ts.formatting {
1010
constructor() {
1111
this.globalRules = new Rules();
1212
const activeRules = this.globalRules.HighPriorityCommonRules.concat(this.globalRules.UserConfigurableRules).concat(this.globalRules.LowPriorityCommonRules);
13-
this.rulesMap = RulesMap.create(activeRules);
13+
this.rulesMap = new RulesMap(activeRules);
1414
}
1515

1616
public getRulesMap() {

0 commit comments

Comments
 (0)