build(typescript): migrate library to typescript

This commit is contained in:
Michael Solati 2021-03-22 20:17:24 -07:00
Родитель fb673c0ab3
Коммит 88609bb9d8
11 изменённых файлов: 2650 добавлений и 35 удалений

1
.eslintignore Normal file
Просмотреть файл

@ -0,0 +1 @@
dist/

3
.eslintrc.json Normal file
Просмотреть файл

@ -0,0 +1,3 @@
{
"extends": "./node_modules/gts/"
}

3
.prettierrc.js Normal file
Просмотреть файл

@ -0,0 +1,3 @@
module.exports = {
...require('gts/.prettierrc.json')
}

2582
package-lock.json сгенерированный

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -2,12 +2,16 @@
"name": "webdev-infra", "name": "webdev-infra",
"version": "1.0.2", "version": "1.0.2",
"description": "This is a placeholder README for the webdev-infra repo, which will contain shared code for https://web.dev, https://developer.chrome.com and others.", "description": "This is a placeholder README for the webdev-infra repo, which will contain shared code for https://web.dev, https://developer.chrome.com and others.",
"main": "index.js", "main": "dist/index.js",
"typings": "dist/index.d.ts",
"directories": { "directories": {
"doc": "docs" "doc": "docs"
}, },
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1",
"lint": "gts lint",
"build": "rimraf ./dist && tsc",
"prepare": "npm run compile"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
@ -22,5 +26,11 @@
"homepage": "https://github.com/GoogleChrome/webdev-infra#readme", "homepage": "https://github.com/GoogleChrome/webdev-infra#readme",
"dependencies": { "dependencies": {
"striptags": "^3.1.1" "striptags": "^3.1.1"
},
"devDependencies": {
"@types/node": "^14.11.2",
"gts": "^3.1.0",
"typescript": "^4.0.3",
"rimraf": "^3.0.2"
} }
} }

2
src/filters/index.ts Normal file
Просмотреть файл

@ -0,0 +1,2 @@
export * from './svg';
export * from './toc';

Просмотреть файл

@ -25,12 +25,8 @@ let svgIndex = 0;
* The basic issue is that SVGs use "#foo" references for clip-paths and so on, * The basic issue is that SVGs use "#foo" references for clip-paths and so on,
* however, when inlined these become global. We rewrite all IDs so they're * however, when inlined these become global. We rewrite all IDs so they're
* unique. * unique.
*
* @param {string} raw
* @param {string} className
* @return {string}
*/ */
const updateSvgForInclude = (raw, className = '') => { export const updateSvgForInclude = (raw: string, className = ''): string => {
if (!raw) { if (!raw) {
return ''; return '';
} }
@ -60,5 +56,3 @@ const updateSvgForInclude = (raw, className = '') => {
return raw; return raw;
}; };
module.exports = {updateSvgForInclude};

Просмотреть файл

@ -26,7 +26,7 @@
* (Don't try this at home). * (Don't try this at home).
*/ */
const striptags = require('striptags'); import * as striptags from 'striptags';
// This matches a heading tag like `<h2 foo="bar">contents</h2>`. It uses backreferences so that // This matches a heading tag like `<h2 foo="bar">contents</h2>`. It uses backreferences so that
// the regexp catches the correct closing tag (i.e., <h2> will be closed by the nearest upcoming // the regexp catches the correct closing tag (i.e., <h2> will be closed by the nearest upcoming
@ -52,18 +52,13 @@ let options = {
spanClass: 'toc__span', spanClass: 'toc__span',
}; };
/** export interface TocNodeType {
* @typedef {{ title: string;
* title: string, id?: string;
* id?: string, children?: TocNodeType[];
* children?: TocNodeType[], }
* }}
*/
// eslint-disable-next-line no-unused-vars
let TocNodeType;
/** @param {TocNodeType} node */ const renderNode = (node: TocNodeType): string => {
const renderNode = (node) => {
let escapedTitle = striptags(node.title); let escapedTitle = striptags(node.title);
// Remove any hanging "#" which is generated by other code. // Remove any hanging "#" which is generated by other code.
@ -80,26 +75,23 @@ const renderNode = (node) => {
return `<li class="${options.listItemClass}">${titlePart}${childrenPart}</li>`; return `<li class="${options.listItemClass}">${titlePart}${childrenPart}</li>`;
}; };
/** @param {TocNodeType[]} toplevel */ const renderChildren = (toplevel: TocNodeType[]): string => {
const renderChildren = (toplevel) => {
return `<ul class="${options.listClass}">${toplevel return `<ul class="${options.listClass}">${toplevel
.map(renderNode) .map(renderNode)
.join('')}</ul>`; .join('')}</ul>`;
}; };
/** /**
* @param {string} raw inner HTML content of the page * @param raw inner HTML content of the page
* @return {string} toc contents * @return toc contents
*/ */
const toc = (raw, opts) => { export const toc = (raw: string, opts: TODO): string => {
// Merge user configuration into top level options. // Merge user configuration into top level options.
options = {...options, ...opts}; options = {...options, ...opts};
/** @type {TocNodeType[]} */ let toplevel: TocNodeType[] = [];
let toplevel = [];
/** @type {TocNodeType|null} */ let current: TocNodeType | null = null;
let current = null;
for (;;) { for (;;) {
const headingMatch = headingRe.exec(raw); const headingMatch = headingRe.exec(raw);
@ -138,7 +130,3 @@ const toc = (raw, opts) => {
} }
return renderChildren(toplevel); return renderChildren(toplevel);
}; };
module.exports = {
toc,
};

1
src/index.ts Normal file
Просмотреть файл

@ -0,0 +1 @@
export * from './filters';

9
tsconfig.json Normal file
Просмотреть файл

@ -0,0 +1,9 @@
{
"extends": "./node_modules/gts/tsconfig-google.json",
"compilerOptions": {
"rootDir": ".",
"outDir": "dist",
"typesRoots": ["./types"]
},
"include": ["src/**/*.ts", "test/**/*.ts", "types/**/*"]
}

22
types/utils/todo.d.ts поставляемый Normal file
Просмотреть файл

@ -0,0 +1,22 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
declare global {
export type TODO = any; // eslint-disable-line @typescript-eslint/no-explicit-any
}
// empty export to keep file a module
export {};