diff --git a/packages/autop/CHANGELOG.md b/packages/autop/CHANGELOG.md index 0f40e07f861653..918e47c7352990 100644 --- a/packages/autop/CHANGELOG.md +++ b/packages/autop/CHANGELOG.md @@ -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 diff --git a/packages/autop/package.json b/packages/autop/package.json index 9a4f5d7fbaaacb..185eaf491c9a06 100644 --- a/packages/autop/package.json +++ b/packages/autop/package.json @@ -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" diff --git a/packages/autop/src/index.js b/packages/autop/src/index.js index 3a0fc7fa73600e..b2c605620852f3 100644 --- a/packages/autop/src/index.js +++ b/packages/autop/src/index.js @@ -1,7 +1,7 @@ /** * The regular expression for an HTML element. * - * @type {string} + * @type {RegExp} */ const htmlSplitRegex = ( () => { /* eslint-disable no-multi-spaces */ @@ -52,7 +52,7 @@ 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 = []; @@ -60,9 +60,15 @@ function htmlSplit( 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 ) { @@ -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} replacePairs In the form {from: 'to', …}. + * @return {string} The formatted text. */ function replaceInHtmlTags( haystack, replacePairs ) { // Find all elements. @@ -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; @@ -399,7 +406,7 @@ export function removep( html ) { html = html.replace( /\n[\s\u00a0]+\n/g, '\n\n' ); // Replace
tags with line breaks. - html = html.replace( /(\s*)
\s*/gi, function( match, space ) { + html = html.replace( /(\s*)
\s*/gi, function( _, space ) { if ( space && space.indexOf( '\n' ) !== -1 ) { return '\n\n'; } @@ -470,7 +477,7 @@ export function removep( html ) { // Restore preserved tags. if ( preserve.length ) { html = html.replace( //g, function() { - return preserve.shift(); + return /** @type {string} */ ( preserve.shift() ); } ); } diff --git a/packages/autop/tsconfig.json b/packages/autop/tsconfig.json new file mode 100644 index 00000000000000..1a0a90bc8cb582 --- /dev/null +++ b/packages/autop/tsconfig.json @@ -0,0 +1,9 @@ +{ + "extends": "../../tsconfig.base.json", + "compilerOptions": { + "rootDir": "src", + "declarationDir": "build-types" + }, + "references": [ { "path": "../dom-ready" } ], + "include": [ "src/**/*" ] +} diff --git a/packages/escape-html/CHANGELOG.md b/packages/escape-html/CHANGELOG.md index 9f38d3c2bb14ef..ab18f8a217b329 100644 --- a/packages/escape-html/CHANGELOG.md +++ b/packages/escape-html/CHANGELOG.md @@ -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) diff --git a/packages/escape-html/package.json b/packages/escape-html/package.json index fab30cccc0bece..7f208d33df38a0 100644 --- a/packages/escape-html/package.json +++ b/packages/escape-html/package.json @@ -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" diff --git a/packages/escape-html/tsconfig.json b/packages/escape-html/tsconfig.json new file mode 100644 index 00000000000000..3c2c31f506f132 --- /dev/null +++ b/packages/escape-html/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../tsconfig.base.json", + "compilerOptions": { + "rootDir": "src", + "declarationDir": "build-types" + }, + "include": [ "src/**/*" ] +} diff --git a/packages/html-entities/CHANGELOG.md b/packages/html-entities/CHANGELOG.md index f9355f9f968653..5d0ba8e3277ef0 100644 --- a/packages/html-entities/CHANGELOG.md +++ b/packages/html-entities/CHANGELOG.md @@ -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) diff --git a/packages/html-entities/package.json b/packages/html-entities/package.json index 5e0cb8c0f1d047..f3df5c2856ffaa 100644 --- a/packages/html-entities/package.json +++ b/packages/html-entities/package.json @@ -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" }, diff --git a/packages/html-entities/src/index.js b/packages/html-entities/src/index.js index 615087f992e78d..25e440748765cf 100644 --- a/packages/html-entities/src/index.js +++ b/packages/html-entities/src/index.js @@ -1,3 +1,4 @@ +/** @type {HTMLTextAreaElement} */ let _decodeTextArea; /** @@ -36,5 +37,23 @@ export function decodeEntities( html ) { _decodeTextArea.innerHTML = html; const decoded = _decodeTextArea.textContent; _decodeTextArea.innerHTML = ''; - return decoded; + + /** + * Cast to string, HTMLTextAreaElement should always have `string` textContent. + * + * > The `textContent` property of the `Node` interface represents the text content of the + * > node and its descendants. + * > + * > Value: A string or `null` + * > + * > * If the node is a `document` or a Doctype, `textContent` returns `null`. + * > * If the node is a CDATA section, comment, processing instruction, or text node, + * > textContent returns the text inside the node, i.e., the `Node.nodeValue`. + * > * For other node types, `textContent returns the concatenation of the textContent of + * > every child node, excluding comments and processing instructions. (This is an empty + * > string if the node has no children.) + * + * @see https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent + */ + return /** @type {string} */ ( decoded ); } diff --git a/packages/html-entities/tsconfig.json b/packages/html-entities/tsconfig.json new file mode 100644 index 00000000000000..3c2c31f506f132 --- /dev/null +++ b/packages/html-entities/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../tsconfig.base.json", + "compilerOptions": { + "rootDir": "src", + "declarationDir": "build-types" + }, + "include": [ "src/**/*" ] +} diff --git a/tsconfig.json b/tsconfig.json index d5c74947987457..c44723b21e2f58 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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" },