Skip to content

Commit b5fa4d0

Browse files
authored
perf: optimize parser constructor (#136)
1 parent cdbc6b2 commit b5fa4d0

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

.changeset/blue-phones-agree.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"htmljs-parser": patch
3+
---
4+
5+
Optimize parser constructor to avoid initializing unecessary properties.

src/core/Parser.ts

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,22 @@ export interface StateDefinition<P extends Meta = Meta> {
3030
}
3131

3232
export class Parser {
33-
public pos!: number;
34-
public maxPos!: number;
35-
public data!: string;
36-
public activeState!: StateDefinition;
37-
public activeRange!: Meta;
38-
public forward!: number;
39-
public activeTag: STATE.OpenTagMeta | undefined; // Used to reference the closest open tag
40-
public activeAttr: STATE.AttrMeta | undefined; // Used to reference the current attribute that is being parsed
41-
public indent!: string; // Used to build the indent for the current concise line
42-
public isConcise!: boolean; // Set to true if parser is currently in concise mode
43-
public beginMixedMode?: boolean; // Used as a flag to mark that the next HTML block should enter the parser into HTML mode
44-
public endingMixedModeAtEOL?: boolean; // Used as a flag to record that the next EOL to exit HTML mode and go back to concise
45-
public textPos!: number; // Used to buffer text that is found within the body of a tag
46-
public lines: undefined | number[]; // Keeps track of line indexes to provide line/column info.
47-
48-
constructor(public options: Options) {
49-
this.options = options;
50-
}
33+
public declare pos: number;
34+
public declare maxPos: number;
35+
public declare data: string;
36+
public declare activeState: StateDefinition;
37+
public declare activeRange: Meta;
38+
public declare forward: number;
39+
public declare activeTag: STATE.OpenTagMeta | undefined; // Used to reference the closest open tag
40+
public declare activeAttr: STATE.AttrMeta | undefined; // Used to reference the current attribute that is being parsed
41+
public declare indent: string; // Used to build the indent for the current concise line
42+
public declare isConcise: boolean; // Set to true if parser is currently in concise mode
43+
public declare beginMixedMode?: boolean; // Used as a flag to mark that the next HTML block should enter the parser into HTML mode
44+
public declare endingMixedModeAtEOL?: boolean; // Used as a flag to record that the next EOL to exit HTML mode and go back to concise
45+
public declare textPos: number; // Used to buffer text that is found within the body of a tag
46+
public declare lines: undefined | number[]; // Keeps track of line indexes to provide line/column info.
47+
48+
constructor(public options: Options) {}
5149

5250
read(range: Range) {
5351
return this.data.slice(range.start, range.end);

0 commit comments

Comments
 (0)