Skip to content

Commit 7e2b3cd

Browse files
im3dabasiadmsnell
authored andcommitted
TypeScript: Migrate block-serialization-default-parser to TS. (WordPress#70766)
Part of WordPress#67691. Replaces WordPress#70507 (where the original patch was prepared). Co-authored-by: Dennis Snell <dennis.snell@automattic.com>
1 parent c14b9be commit 7e2b3cd

File tree

1 file changed

+84
-83
lines changed
  • packages/block-serialization-default-parser/src

1 file changed

+84
-83
lines changed

packages/block-serialization-default-parser/src/index.js renamed to packages/block-serialization-default-parser/src/index.ts

Lines changed: 84 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,33 @@
1-
/**
2-
* @type {string}
3-
*/
4-
let document;
5-
/**
6-
* @type {number}
7-
*/
8-
let offset;
9-
/**
10-
* @type {ParsedBlock[]}
11-
*/
12-
let output;
13-
/**
14-
* @type {ParsedFrame[]}
15-
*/
16-
let stack;
17-
18-
/**
19-
* @typedef {Object|null} Attributes
20-
*/
21-
22-
/**
23-
* @typedef {Object} ParsedBlock
24-
* @property {string|null} blockName Block name.
25-
* @property {Attributes} attrs Block attributes.
26-
* @property {ParsedBlock[]} innerBlocks Inner blocks.
27-
* @property {string} innerHTML Inner HTML.
28-
* @property {Array<string|null>} innerContent Inner content.
29-
*/
1+
let document: string;
2+
let offset: number;
3+
let output: ParsedBlock[];
4+
let stack: ParsedFrame[];
5+
6+
type Attributes = Record< string, any > | null;
7+
8+
type ParsedBlock = {
9+
blockName: string | null;
10+
attrs: Attributes;
11+
innerBlocks: ParsedBlock[];
12+
innerHTML: string;
13+
innerContent: Array< string | null >;
14+
};
3015

31-
/**
32-
* @typedef {Object} ParsedFrame
33-
* @property {ParsedBlock} block Block.
34-
* @property {number} tokenStart Token start.
35-
* @property {number} tokenLength Token length.
36-
* @property {number} prevOffset Previous offset.
37-
* @property {number|null} leadingHtmlStart Leading HTML start.
38-
*/
16+
type ParsedFrame = {
17+
block: ParsedBlock;
18+
tokenStart: number;
19+
tokenLength: number;
20+
prevOffset: number;
21+
leadingHtmlStart: number | null;
22+
};
3923

40-
/**
41-
* @typedef {'no-more-tokens'|'void-block'|'block-opener'|'block-closer'} TokenType
42-
*/
24+
type TokenType =
25+
| 'no-more-tokens'
26+
| 'void-block'
27+
| 'block-opener'
28+
| 'block-closer';
4329

44-
/**
45-
* @typedef {[TokenType, string, Attributes, number, number]} Token
46-
*/
30+
type Token = [ TokenType, string, Attributes, number, number ];
4731

4832
/**
4933
* Matches block comment delimiters
@@ -81,8 +65,6 @@ let stack;
8165
* once browsers reliably support atomic grouping or possessive
8266
* quantifiers natively we should remove this trick and simplify
8367
*
84-
* @type {RegExp}
85-
*
8668
* @since 3.8.0
8769
* @since 4.6.1 added optimization to prevent backtracking on attribute parsing
8870
*/
@@ -92,14 +74,21 @@ const tokenizer =
9274
/**
9375
* Constructs a block object.
9476
*
95-
* @param {string|null} blockName
96-
* @param {Attributes} attrs
97-
* @param {ParsedBlock[]} innerBlocks
98-
* @param {string} innerHTML
99-
* @param {string[]} innerContent
100-
* @return {ParsedBlock} The block object.
77+
* @param blockName Either the abbreviated core types, e.g. "paragraph", or the fully-qualified
78+
* block type with namespace and type, e.g. "core/paragraph" or "my-plugin/csv-table".
79+
* @param attrs The attributes for the block, or null if there are no attributes.
80+
* @param innerBlocks An array of inner blocks.
81+
* @param innerHTML The inner HTML of the block.
82+
* @param innerContent An array of inner content strings.
83+
* @return The block object.
10184
*/
102-
function Block( blockName, attrs, innerBlocks, innerHTML, innerContent ) {
85+
function Block(
86+
blockName: string | null,
87+
attrs: Attributes,
88+
innerBlocks: ParsedBlock[],
89+
innerHTML: string,
90+
innerContent: string[]
91+
): ParsedBlock {
10392
return {
10493
blockName,
10594
attrs,
@@ -112,24 +101,30 @@ function Block( blockName, attrs, innerBlocks, innerHTML, innerContent ) {
112101
/**
113102
* Constructs a freeform block object.
114103
*
115-
* @param {string} innerHTML
116-
* @return {ParsedBlock} The freeform block object.
104+
* @param innerHTML The inner HTML of the block.
105+
* @return The freeform block object.
117106
*/
118-
function Freeform( innerHTML ) {
107+
function Freeform( innerHTML: string ): ParsedBlock {
119108
return Block( null, {}, [], innerHTML, [ innerHTML ] );
120109
}
121110

122111
/**
123112
* Constructs a frame object.
124113
*
125-
* @param {ParsedBlock} block
126-
* @param {number} tokenStart
127-
* @param {number} tokenLength
128-
* @param {number} prevOffset
129-
* @param {number|null} leadingHtmlStart
130-
* @return {ParsedFrame} The frame object.
114+
* @param block The block object.
115+
* @param tokenStart The start offset of the token in the document.
116+
* @param tokenLength The length of the token in the document.
117+
* @param prevOffset The offset of the previous token in the document.
118+
* @param leadingHtmlStart The start offset of leading HTML before the block.
119+
* @return The frame object.
131120
*/
132-
function Frame( block, tokenStart, tokenLength, prevOffset, leadingHtmlStart ) {
121+
function Frame(
122+
block: ParsedBlock,
123+
tokenStart: number,
124+
tokenLength: number,
125+
prevOffset: number | null,
126+
leadingHtmlStart: number | null
127+
): ParsedFrame {
133128
return {
134129
block,
135130
tokenStart,
@@ -142,7 +137,7 @@ function Frame( block, tokenStart, tokenLength, prevOffset, leadingHtmlStart ) {
142137
/**
143138
* Parser function, that converts input HTML into a block based structure.
144139
*
145-
* @param {string} doc The HTML document to parse.
140+
* @param doc The HTML document to parse.
146141
*
147142
* @example
148143
* Input post:
@@ -214,9 +209,9 @@ function Frame( block, tokenStart, tokenLength, prevOffset, leadingHtmlStart ) {
214209
* }
215210
* ];
216211
* ```
217-
* @return {ParsedBlock[]} A block-based representation of the input HTML.
212+
* @return A block-based representation of the input HTML.
218213
*/
219-
export const parse = ( doc ) => {
214+
export const parse = ( doc: string ): ParsedBlock[] => {
220215
document = doc;
221216
offset = 0;
222217
output = [];
@@ -233,9 +228,9 @@ export const parse = ( doc ) => {
233228
/**
234229
* Parses the next token in the input document.
235230
*
236-
* @return {boolean} Returns true when there is more tokens to parse.
231+
* @return Returns true when there is more tokens to parse.
237232
*/
238-
function proceed() {
233+
function proceed(): boolean {
239234
const stackDepth = stack.length;
240235
const next = nextToken();
241236
const [ tokenType, blockName, attrs, startOffset, tokenLength ] = next;
@@ -333,7 +328,7 @@ function proceed() {
333328

334329
// Otherwise we're nested and we have to close out the current
335330
// block and add it as a innerBlock to the parent.
336-
const stackTop = /** @type {ParsedFrame} */ ( stack.pop() );
331+
const stackTop = stack.pop() as ParsedFrame;
337332
const html = document.substr(
338333
stackTop.prevOffset,
339334
startOffset - stackTop.prevOffset
@@ -365,10 +360,10 @@ function proceed() {
365360
* delimiters is constrained to be an object
366361
* and cannot be things like `true` or `null`
367362
*
368-
* @param {string} input JSON input string to parse
369-
* @return {Object|null} parsed JSON if valid
363+
* @param input JSON input string to parse
364+
* @return parsed JSON if valid or null if invalid
370365
*/
371-
function parseJSON( input ) {
366+
function parseJSON( input: string ): Object | null {
372367
try {
373368
return JSON.parse( input );
374369
} catch ( e ) {
@@ -379,9 +374,9 @@ function parseJSON( input ) {
379374
/**
380375
* Finds the next token in the document.
381376
*
382-
* @return {Token} The next matched token.
377+
* @return The next matched token.
383378
*/
384-
function nextToken() {
379+
function nextToken(): Token {
385380
// Aye the magic
386381
// we're using a single RegExp to tokenize the block comment delimiters
387382
// we're also using a trick here because the only difference between a
@@ -435,9 +430,9 @@ function nextToken() {
435430
/**
436431
* Adds a freeform block to the output.
437432
*
438-
* @param {number} [rawLength]
433+
* @param rawLength Optional length of the raw HTML to include as freeform content.
439434
*/
440-
function addFreeform( rawLength ) {
435+
function addFreeform( rawLength?: number ) {
441436
const length = rawLength ? rawLength : document.length - offset;
442437

443438
if ( 0 === length ) {
@@ -450,12 +445,18 @@ function addFreeform( rawLength ) {
450445
/**
451446
* Adds inner block to the parent block.
452447
*
453-
* @param {ParsedBlock} block
454-
* @param {number} tokenStart
455-
* @param {number} tokenLength
456-
* @param {number} [lastOffset]
448+
* @param block The inner block to be added to the parent.
449+
* @param tokenStart The start offset of the block token in the document.
450+
* @param tokenLength The total length of the block token.
451+
* @param lastOffset Optional offset marking the end of the current block,
452+
* used to update the parent's HTML content boundaries.
457453
*/
458-
function addInnerBlock( block, tokenStart, tokenLength, lastOffset ) {
454+
function addInnerBlock(
455+
block: ParsedBlock,
456+
tokenStart: number,
457+
tokenLength: number,
458+
lastOffset?: number
459+
) {
459460
const parent = stack[ stack.length - 1 ];
460461
parent.block.innerBlocks.push( block );
461462
const html = document.substr(
@@ -475,11 +476,11 @@ function addInnerBlock( block, tokenStart, tokenLength, lastOffset ) {
475476
/**
476477
* Adds block from the stack to the output.
477478
*
478-
* @param {number} [endOffset]
479+
* @param endOffset Optional offset marking the end of the block's HTML content.
479480
*/
480-
function addBlockFromStack( endOffset ) {
481+
function addBlockFromStack( endOffset?: number ) {
481482
const { block, leadingHtmlStart, prevOffset, tokenStart } =
482-
/** @type {ParsedFrame} */ ( stack.pop() );
483+
stack.pop() as ParsedFrame;
483484

484485
const html = endOffset
485486
? document.substr( prevOffset, endOffset - prevOffset )

0 commit comments

Comments
 (0)