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

Packages: Add and check types #20669

Merged
merged 5 commits into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 6 additions & 0 deletions packages/autop/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## Master

### New feature

- Include TypeScript type declarations ([#20669](https://github.com/WordPress/gutenberg/pull/20669))

## 2.3.0 (2019-05-21)

### Bug Fix
Expand Down
1 change: 1 addition & 0 deletions packages/autop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"main": "build/index.js",
"module": "build-module/index.js",
"react-native": "src/index",
"types": "build-types",
"sideEffects": false,
"dependencies": {
"@babel/runtime": "^7.8.3"
Expand Down
25 changes: 16 additions & 9 deletions packages/autop/src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* The regular expression for an HTML element.
*
* @type {string}
* @type {RegExp}
*/
const htmlSplitRegex = ( () => {
/* eslint-disable no-multi-spaces */
Expand Down Expand Up @@ -52,17 +52,23 @@ const htmlSplitRegex = ( () => {
* Separate HTML elements and comments from the text.
*
* @param {string} input The text which has to be formatted.
* @return {Array} The formatted text.
* @return {string[]} The formatted text.
*/
function htmlSplit( input ) {
const parts = [];
let workingInput = input;

let match;
while ( ( match = workingInput.match( htmlSplitRegex ) ) ) {
parts.push( workingInput.slice( 0, match.index ) );
// The `match` result, when invoked on a RegExp with the `g` flag (`/foo/g`) will not include `index`.
// 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 );

parts.push( workingInput.slice( 0, index ) );
parts.push( match[ 0 ] );
workingInput = workingInput.slice( match.index + match[ 0 ].length );
workingInput = workingInput.slice( index + match[ 0 ].length );
}

if ( workingInput.length ) {
Expand All @@ -75,9 +81,9 @@ function htmlSplit( input ) {
/**
* Replace characters or phrases within HTML elements only.
*
* @param {string} haystack The text which has to be formatted.
* @param {Object} replacePairs In the form {from: 'to', ...}.
* @return {string} The formatted text.
* @param {string} haystack The text which has to be formatted.
* @param {Record<string,string>} replacePairs In the form {from: 'to', }.
* @return {string} The formatted text.
*/
function replaceInHtmlTags( haystack, replacePairs ) {
// Find all elements.
Expand Down Expand Up @@ -337,6 +343,7 @@ export function removep( html ) {
'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 = [];
let preserveLinebreaks = false;
let preserveBr = false;
Expand Down Expand Up @@ -399,7 +406,7 @@ export function removep( html ) {
html = html.replace( /\n[\s\u00a0]+\n/g, '\n\n' );

// Replace <br> tags with line breaks.
html = html.replace( /(\s*)<br ?\/?>\s*/gi, function( match, space ) {
html = html.replace( /(\s*)<br ?\/?>\s*/gi, function( _, space ) {
if ( space && space.indexOf( '\n' ) !== -1 ) {
return '\n\n';
}
Expand Down Expand Up @@ -470,7 +477,7 @@ export function removep( html ) {
// Restore preserved tags.
if ( preserve.length ) {
html = html.replace( /<wp-preserve>/g, function() {
return preserve.shift();
return /** @type {string} */ ( preserve.shift() );
} );
}

Expand Down
9 changes: 9 additions & 0 deletions packages/autop/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"rootDir": "src",
"declarationDir": "build-types"
},
"references": [ { "path": "../dom-ready" } ],
"include": [ "src/**/*" ]
}
6 changes: 6 additions & 0 deletions packages/escape-html/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## Master

### New feature

- Include TypeScript type declarations ([#20669](https://github.com/WordPress/gutenberg/pull/20669))

## 1.2.0 (2019-03-20)

- Add fix for WordPress wptexturize greater-than tokenize bug (see https://core.trac.wordpress.org/ticket/45387)
Expand Down
1 change: 1 addition & 0 deletions packages/escape-html/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"main": "build/index.js",
"module": "build-module/index.js",
"react-native": "src/index",
"types": "build-types",
"sideEffects": false,
"dependencies": {
"@babel/runtime": "^7.8.3"
Expand Down
8 changes: 8 additions & 0 deletions packages/escape-html/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"rootDir": "src",
"declarationDir": "build-types"
},
"include": [ "src/**/*" ]
}
6 changes: 6 additions & 0 deletions packages/html-entities/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## Master

### New feature

- Include TypeScript type declarations ([#20669](https://github.com/WordPress/gutenberg/pull/20669))

## 2.0.2 (2018-12-12)

## 2.0.1 (2018-11-21)
Expand Down
1 change: 1 addition & 0 deletions packages/html-entities/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"main": "build/index.js",
"module": "build-module/index.js",
"react-native": "src/index",
"types": "build-types",
"dependencies": {
"@babel/runtime": "^7.8.3"
},
Expand Down
4 changes: 3 additions & 1 deletion packages/html-entities/src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/** @type {HTMLTextAreaElement} */
let _decodeTextArea;

/**
Expand Down Expand Up @@ -36,5 +37,6 @@ export function decodeEntities( html ) {
_decodeTextArea.innerHTML = html;
const decoded = _decodeTextArea.textContent;
_decodeTextArea.innerHTML = '';
return decoded;
// Cast to string, HTMLTextElement should always have `string` textContent
sirreal marked this conversation as resolved.
Show resolved Hide resolved
return /** @type {string} */ ( decoded );
}
8 changes: 8 additions & 0 deletions packages/html-entities/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"rootDir": "src",
"declarationDir": "build-types"
},
"include": [ "src/**/*" ]
}
3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
"references": [
{ "path": "bin" },
{ "path": "packages/a11y" },
{ "path": "packages/autop" },
{ "path": "packages/blob" },
{ "path": "packages/dom-ready" },
{ "path": "packages/escape-html" },
{ "path": "packages/html-entities" },
{ "path": "packages/i18n" },
{ "path": "packages/is-shallow-equal" },
{ "path": "packages/priority-queue" },
Expand Down