Skip to content

Commit

Permalink
Enable isolated declarations (#1522)
Browse files Browse the repository at this point in the history
  • Loading branch information
fb55 authored Jul 16, 2024
1 parent be53b61 commit 9c511bc
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 36 deletions.
7 changes: 1 addition & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,42 +23,37 @@
"exports": {
".": {
"import": {
"source": "./src/index.ts",
"types": "./dist/esm/index.d.ts",
"default": "./dist/esm/index.js"
},
"require": {
"source": "./src/index.ts",
"types": "./dist/commonjs/index.d.ts",
"default": "./dist/commonjs/index.js"
}
},
"./dist/decode.js": {
"import": {
"source": "./src/decode.ts",
"types": "./dist/esm/decode.d.ts",
"default": "./dist/esm/decode.js"
},
"require": {
"source": "./src/decode.ts",
"types": "./dist/commonjs/decode.d.ts",
"default": "./dist/commonjs/decode.js"
}
},
"./dist/escape.js": {
"import": {
"source": "./src/escape.ts",
"types": "./dist/esm/escape.d.ts",
"default": "./dist/esm/escape.js"
},
"require": {
"source": "./src/escape.ts",
"types": "./dist/commonjs/escape.d.ts",
"default": "./dist/commonjs/escape.js"
}
}
},
"main": "./dist/commonjs/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/commonjs/index.d.ts",
"files": [
"dist",
Expand Down
4 changes: 2 additions & 2 deletions scripts/write-decode-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { getTrie } from "./trie/trie.js";
import { encodeTrie } from "./trie/encode-trie.js";

function convertMapToBinaryTrie(
name: string,
name: "xml" | "html",
map: Record<string, string>,
legacy: Record<string, string>,
) {
Expand All @@ -25,7 +25,7 @@ function convertMapToBinaryTrie(
new URL(`../src/generated/decode-data-${name}.ts`, import.meta.url),
`// Generated using scripts/write-decode-map.ts
export default /* #__PURE__ */ new Uint16Array(
export const ${name}DecodeTree: Uint16Array = /* #__PURE__ */ new Uint16Array(
// prettier-ignore
/* #__PURE__ */ ${stringified}
.split("")
Expand Down
2 changes: 1 addition & 1 deletion scripts/write-encode-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function restoreDiff<T extends ReadonlyArray<[number, EncodeTrieNode]>>(
}
// prettier-ignore
export default ${
export const htmlTrie: Map<number,EncodeTrieNode> = ${
// Fix the type of the first map to refer to trie nodes.
serialized.replace("<number,string>", "<number,EncodeTrieNode>")
};
Expand Down
6 changes: 3 additions & 3 deletions src/decode-codepoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const decodeMap = new Map([
/**
* Polyfill for `String.fromCodePoint`. It is used to create a string from a Unicode code point.
*/
export const fromCodePoint =
export const fromCodePoint: (...codePoints: number[]) => string =
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition, n/no-unsupported-features/es-builtins
String.fromCodePoint ??
function (codePoint: number): string {
Expand All @@ -58,7 +58,7 @@ export const fromCodePoint =
* surrogate or is outside the valid range. Otherwise return the code
* point unchanged.
*/
export function replaceCodePoint(codePoint: number) {
export function replaceCodePoint(codePoint: number): number {
if (
(codePoint >= 0xd8_00 && codePoint <= 0xdf_ff) ||
codePoint > 0x10_ff_ff
Expand All @@ -76,6 +76,6 @@ export function replaceCodePoint(codePoint: number) {
* @param codePoint The code point to decode.
* @returns The decoded code point.
*/
export default function decodeCodePoint(codePoint: number): string {
export function decodeCodePoint(codePoint: number): string {
return fromCodePoint(replaceCodePoint(codePoint));
}
14 changes: 7 additions & 7 deletions src/decode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import htmlDecodeTree from "./generated/decode-data-html.js";
import xmlDecodeTree from "./generated/decode-data-xml.js";
import { htmlDecodeTree } from "./generated/decode-data-html.js";
import { xmlDecodeTree } from "./generated/decode-data-xml.js";
import { replaceCodePoint, fromCodePoint } from "./decode-codepoint.js";

const enum CharCodes {
Expand Down Expand Up @@ -101,7 +101,7 @@ export class EntityDecoder {
*/
private readonly emitCodePoint: (cp: number, consumed: number) => void,
/** An object that is used to produce errors. */
private readonly errors?: EntityErrorProducer,
private readonly errors?: EntityErrorProducer | undefined,
) {}

/** The current state of the decoder. */
Expand Down Expand Up @@ -574,7 +574,7 @@ const xmlDecoder = /* #__PURE__ */ getDecoder(xmlDecodeTree);
*/
export function decodeHTML(
htmlString: string,
mode = DecodingMode.Legacy,
mode: DecodingMode = DecodingMode.Legacy,
): string {
return htmlDecoder(htmlString, mode);
}
Expand Down Expand Up @@ -610,11 +610,11 @@ export function decodeXML(xmlString: string): string {
}

// Re-export for use by eg. htmlparser2
export { default as htmlDecodeTree } from "./generated/decode-data-html.js";
export { default as xmlDecodeTree } from "./generated/decode-data-xml.js";
export { htmlDecodeTree } from "./generated/decode-data-html.js";
export { xmlDecodeTree } from "./generated/decode-data-xml.js";

export {
default as decodeCodePoint,
decodeCodePoint,
replaceCodePoint,
fromCodePoint,
} from "./decode-codepoint.js";
2 changes: 1 addition & 1 deletion src/encode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import htmlTrie from "./generated/encode-html.js";
import { htmlTrie } from "./generated/encode-html.js";
import { xmlReplacer, getCodePoint } from "./escape.js";

const htmlReplacer = /[\t\n\f!-,./:-@[-`{-}\u0080-\uFFFF]/g;
Expand Down
30 changes: 17 additions & 13 deletions src/escape.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const xmlReplacer = /["$&'<>\u0080-\uFFFF]/g;
export const xmlReplacer: RegExp = /["$&'<>\u0080-\uFFFF]/g;

const xmlCodeMap = new Map([
[34, "&quot;"],
Expand All @@ -9,7 +9,7 @@ const xmlCodeMap = new Map([
]);

// For compatibility with node < 4, we wrap `codePointAt`
export const getCodePoint =
export const getCodePoint: (c: string, index: number) => number =
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
String.prototype.codePointAt == null
? (c: string, index: number): number =>
Expand Down Expand Up @@ -66,7 +66,7 @@ export function encodeXML(input: string): string {
*
* @param data String to escape.
*/
export const escape = encodeXML;
export const escape: typeof encodeXML = encodeXML;

/**
* Creates a function that escapes all characters matched by the given regular
Expand Down Expand Up @@ -110,30 +110,34 @@ function getEscaper(
*
* @param data String to escape.
*/
export const escapeUTF8 = /* #__PURE__ */ getEscaper(/["&'<>]/g, xmlCodeMap);
export const escapeUTF8: (data: string) => string = /* #__PURE__ */ getEscaper(
/["&'<>]/g,
xmlCodeMap,
);

/**
* Encodes all characters that have to be escaped in HTML attributes,
* following {@link https://html.spec.whatwg.org/multipage/parsing.html#escapingString}.
*
* @param data String to escape.
*/
export const escapeAttribute = /* #__PURE__ */ getEscaper(
/["&\u00A0]/g,
new Map([
[34, "&quot;"],
[38, "&amp;"],
[160, "&nbsp;"],
]),
);
export const escapeAttribute: (data: string) => string =
/* #__PURE__ */ getEscaper(
/["&\u00A0]/g,
new Map([
[34, "&quot;"],
[38, "&amp;"],
[160, "&nbsp;"],
]),
);

/**
* Encodes all characters that have to be escaped in HTML text,
* following {@link https://html.spec.whatwg.org/multipage/parsing.html#escapingString}.
*
* @param data String to escape.
*/
export const escapeText = /* #__PURE__ */ getEscaper(
export const escapeText: (data: string) => string = /* #__PURE__ */ getEscaper(
/[&<>\u00A0]/g,
new Map([
[38, "&amp;"],
Expand Down
2 changes: 1 addition & 1 deletion src/generated/decode-data-html.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/generated/decode-data-xml.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Generated using scripts/write-decode-map.ts

export default /* #__PURE__ */ new Uint16Array(
export const xmlDecodeTree: Uint16Array = /* #__PURE__ */ new Uint16Array(
// prettier-ignore
/* #__PURE__ */ "\u0200aglq\t\x15\x18\x1b\u026d\x0f\0\0\x12p;\u4026os;\u4027t;\u403et;\u403cuot;\u4022"
.split("")
Expand Down
2 changes: 1 addition & 1 deletion src/generated/encode-html.ts

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
/* Additional Checks */
"exactOptionalPropertyTypes": true,
"forceConsistentCasingInFileNames": true,
"isolatedDeclarations": true,
"isolatedModules": true,
"noFallthroughCasesInSwitch": true,
"noImplicitOverride": true,
Expand Down

0 comments on commit 9c511bc

Please sign in to comment.