Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert autop package to TypeScript #61573

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions packages/autop/src/index.js → packages/autop/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* @type {RegExp}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove all the JSDoc type anottations now, they are duplicate and redundant. The docgen tool that generates the Markdown documentation will retrieve the types from the code, and should generate exactly the same output.

*/
const htmlSplitRegex = ( () => {
const htmlSplitRegex: RegExp = ( () => {
/* eslint-disable no-multi-spaces */
const comments =
'!' + // Start of comment, after the <.
Expand Down Expand Up @@ -55,7 +55,7 @@ const htmlSplitRegex = ( () => {
*
* @return {string[]} The formatted text.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, we only need to document the params, the types are on the function signature:

@param input The text which...
@return The formatted text.

*/
function htmlSplit( input ) {
function htmlSplit( input: string ): string[] {
const parts = [];
let workingInput = input;

Expand All @@ -65,7 +65,7 @@ function htmlSplit( input ) {
// If the `g` flag is omitted, `index` is included.
// `htmlSplitRegex` does not have the `g` flag so we can assert it will have an index number.
// Assert `match.index` is a number.
const index = /** @type {number} */ ( match.index );
const index = match.index!;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't use the ! operator, here match.index as number is preferred.


parts.push( workingInput.slice( 0, index ) );
parts.push( match[ 0 ] );
Expand All @@ -87,7 +87,10 @@ function htmlSplit( input ) {
*
* @return {string} The formatted text.
*/
function replaceInHtmlTags( haystack, replacePairs ) {
function replaceInHtmlTags(
haystack: string,
replacePairs: Record< string, string >
): string {
// Find all elements.
const textArr = htmlSplit( haystack );
let changed = false;
Expand Down Expand Up @@ -137,8 +140,8 @@ function replaceInHtmlTags( haystack, replacePairs ) {
*
* @return {string} Text which has been converted into paragraph tags.
*/
export function autop( text, br = true ) {
const preTags = [];
export function autop( text: string, br: boolean = true ): string {
const preTags: Array< [ string, string ] > = [];

if ( text.trim() === '' ) {
return '';
Expand Down Expand Up @@ -340,13 +343,12 @@ export function autop( text, br = true ) {
*
* @return {string} The content with stripped paragraph tags.
*/
export function removep( html ) {
export function removep( html: string ): string {
const blocklist =
'blockquote|ul|ol|li|dl|dt|dd|table|thead|tbody|tfoot|tr|th|td|h[1-6]|fieldset|figure';
const blocklist1 = blocklist + '|div|p';
const blocklist2 = blocklist + '|pre';
/** @type {string[]} */
const preserve = [];
const preserve: string[] = [];
let preserveLinebreaks = false;
let preserveBr = false;

Expand Down Expand Up @@ -480,7 +482,7 @@ export function removep( html ) {
// Restore preserved tags.
if ( preserve.length ) {
html = html.replace( /<wp-preserve>/g, () => {
return /** @type {string} */ ( preserve.shift() );
return preserve.shift()!;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here also as string is better.

} );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ test( 'that_autop_treats_block_level_elements_as_blocks', () => {
];

// Check whitespace normalization.
let content = [];
let content: string[] = [];

blocks.forEach( ( block ) => {
content.push( `<${ block }>foo</${ block }>` );
Expand Down Expand Up @@ -388,18 +388,18 @@ test( 'that autop treats inline elements as inline', () => {
'select',
];

let content = [];
let expected = [];
const content: string[] = [];
const expected: string[] = [];

inlines.forEach( ( inline ) => {
content.push( `<${ inline }>foo</${ inline }>` );
expected.push( `<p><${ inline }>foo</${ inline }></p>` );
} );

content = content.join( '\n\n' );
expected = expected.join( '\n' );
const contentString = content.join( '\n\n' );
const expectedString = expected.join( '\n' );

expect( autop( content ).trim() ).toBe( expected );
expect( autop( contentString ).trim() ).toBe( expectedString );
} );

test( 'element sanity', () => {
Expand Down
Loading