From 0f0b2352c133140bcdfd70a12fa82f83a82873a4 Mon Sep 17 00:00:00 2001 From: James Garbutt <43081j@users.noreply.github.com> Date: Thu, 6 Jan 2022 20:32:42 +0000 Subject: [PATCH] Move entire codebase to ESM (#351) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Felix Böhm <188768+fb55@users.noreply.github.com> --- .eslintrc.js | 20 - .eslintrc.json | 21 + bench/.eslintrc.js | 9 - bench/.eslintrc.json | 6 + bench/memory/named-entity-data.js | 8 +- bench/memory/sax-parser.js | 20 +- bench/package.json | 3 +- bench/perf/index.js | 75 +- package-lock.json | 3698 ++++++++++++----- package.json | 20 +- .../parse5-html-rewriting-stream/lib/index.js | 10 +- .../parse5-html-rewriting-stream/package.json | 1 + .../test/rewriting-stream.test.js | 469 ++- .../lib/index.js | 144 +- .../package.json | 1 + packages/parse5-parser-stream/lib/index.js | 10 +- packages/parse5-parser-stream/package.json | 1 + .../parse5-parser-stream/test/.eslintrc.js | 6 - .../test/location-info.test.js | 16 +- .../test/parser-stream.test.js | 38 +- .../test/scripting.test.js | 23 +- .../test/utils/parse-chunked.js | 10 +- .../lib/index.js | 10 +- .../package.json | 1 + .../test/plain-text-conversion-stream.test.js | 26 +- .../parse5-sax-parser/lib/dev-null-stream.js | 8 +- packages/parse5-sax-parser/lib/index.js | 49 +- .../lib/parser-feedback-simulator.js | 14 +- packages/parse5-sax-parser/package.json | 1 + .../test/location-info.test.js | 86 +- .../test/parser-feedback-simulator.test.js | 24 +- .../parse5-sax-parser/test/sax-parser.test.js | 134 +- .../parse5-serializer-stream/lib/index.js | 14 +- .../parse5-serializer-stream/package.json | 1 + .../test/serializer-stream.test.js | 12 +- packages/parse5/lib/common/doctype.js | 22 +- packages/parse5/lib/common/error-codes.js | 6 +- packages/parse5/lib/common/foreign-content.js | 44 +- packages/parse5/lib/common/html.js | 36 +- packages/parse5/lib/common/unicode.js | 69 +- .../extensions/error-reporting/mixin-base.js | 12 +- .../error-reporting/parser-mixin.js | 16 +- .../error-reporting/preprocessor-mixin.js | 12 +- .../error-reporting/tokenizer-mixin.js | 12 +- .../location-info/open-element-stack-mixin.js | 10 +- .../extensions/location-info/parser-mixin.js | 30 +- .../location-info/tokenizer-mixin.js | 20 +- .../position-tracking/preprocessor-mixin.js | 10 +- packages/parse5/lib/index.js | 18 +- .../lib/parser/formatting-element-list.js | 10 +- packages/parse5/lib/parser/index.js | 98 +- .../parse5/lib/parser/open-element-stack.js | 8 +- packages/parse5/lib/serializer/index.js | 26 +- packages/parse5/lib/tokenizer/index.js | 32 +- .../parse5/lib/tokenizer/named-entity-data.js | 2 +- packages/parse5/lib/tokenizer/preprocessor.js | 10 +- packages/parse5/lib/tree-adapters/default.js | 152 +- packages/parse5/lib/utils/merge-options.js | 8 +- packages/parse5/lib/utils/mixin.js | 8 +- packages/parse5/package.json | 1 + .../test/formatting-element-list.test.js | 40 +- .../parse5/test/location-info-parser.test.js | 154 +- .../test/location-info-tokenizer.test.js | 32 +- .../parse5/test/open-element-stack.test.js | 64 +- packages/parse5/test/parser.test.js | 140 +- packages/parse5/test/serializer.test.js | 43 +- packages/parse5/test/tokenizer.test.js | 54 +- scripts/.eslintrc.js | 6 - .../array-mapped-radix-tree.js | 14 +- scripts/generate-named-entity-data/index.js | 17 +- .../generate-named-entity-data/radix-tree.js | 10 +- .../generate-named-entity-data/tree-node.js | 6 +- .../generate-parser-feedback-test/index.js | 40 +- test/.eslintrc.js | 6 - test/utils/common.js | 123 +- .../generate-location-info-parser-tests.js | 49 +- test/utils/generate-parsing-tests.js | 67 +- test/utils/generate-serializer-tests.js | 17 +- test/utils/generate-tokenization-tests.js | 54 +- test/utils/load-sax-parser-test-data.js | 20 +- test/utils/parse-dat-file.js | 10 +- test/utils/serialize-to-dat-file-format.js | 10 +- tsconfig.json | 18 + 83 files changed, 4057 insertions(+), 2598 deletions(-) delete mode 100644 .eslintrc.js create mode 100644 .eslintrc.json delete mode 100644 bench/.eslintrc.js create mode 100644 bench/.eslintrc.json delete mode 100644 packages/parse5-parser-stream/test/.eslintrc.js delete mode 100644 scripts/.eslintrc.js delete mode 100644 test/.eslintrc.js create mode 100644 tsconfig.json diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index c3774cdab..000000000 --- a/.eslintrc.js +++ /dev/null @@ -1,20 +0,0 @@ -module.exports = { - env: { - es6: true, - node: true - }, - extends: ['eslint:recommended', 'prettier'], - plugins: ['prettier'], - rules: { - 'prettier/prettier': 'error', - 'no-console': 'error', - curly: ['error', 'all'], - 'prefer-arrow-callback': 'error', - 'one-var': ['error', 'never'], - 'no-var': 'error', - 'prefer-const': 'error' - }, - parserOptions: { - ecmaVersion: 6 - } -}; diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 000000000..fc68cdf55 --- /dev/null +++ b/.eslintrc.json @@ -0,0 +1,21 @@ +{ + "env": { + "es2020": true, + "node": true, + "mocha": true + }, + "extends": ["eslint:recommended", "prettier"], + "plugins": ["prettier"], + "rules": { + "prettier/prettier": "error", + "no-console": "error", + "curly": ["error", "all"], + "prefer-arrow-callback": "error", + "one-var": ["error", "never"], + "no-var": "error", + "prefer-const": "error" + }, + "parserOptions": { + "sourceType": "module" + } +} diff --git a/bench/.eslintrc.js b/bench/.eslintrc.js deleted file mode 100644 index 401a667b5..000000000 --- a/bench/.eslintrc.js +++ /dev/null @@ -1,9 +0,0 @@ -module.exports = { - extends: ['../.eslintrc.js'], - rules: { - 'no-console': 'off' - }, - parserOptions: { - ecmaVersion: 8 - } -}; diff --git a/bench/.eslintrc.json b/bench/.eslintrc.json new file mode 100644 index 000000000..1cb404c3c --- /dev/null +++ b/bench/.eslintrc.json @@ -0,0 +1,6 @@ +{ + "extends": ["../.eslintrc.json"], + "rules": { + "no-console": "off" + } +} diff --git a/bench/memory/named-entity-data.js b/bench/memory/named-entity-data.js index bca60f005..408821a86 100644 --- a/bench/memory/named-entity-data.js +++ b/bench/memory/named-entity-data.js @@ -1,13 +1,11 @@ -'use strict'; - -const format = require('human-format'); +import format from 'human-format'; main(); -function main() { +async function main() { const before = process.memoryUsage().rss; - require('../../packages/parse5/lib/tokenizer/named-entity-data'); + await import('../../packages/parse5/lib/tokenizer/named-entity-data.js'); const after = process.memoryUsage().rss; diff --git a/bench/memory/sax-parser.js b/bench/memory/sax-parser.js index d1e893cf4..06828c0f1 100644 --- a/bench/memory/sax-parser.js +++ b/bench/memory/sax-parser.js @@ -1,10 +1,8 @@ -'use strict'; - -const fs = require('fs'); -const format = require('human-format'); -const promisifyEvent = require('promisify-event'); -const memwatch = require('node-memwatch'); -const SAXParser = require('../../packages/parse5-sax-parser/lib'); +import * as fs from 'fs'; +import format from 'human-format'; +import promisifyEvent from 'promisify-event'; +import memwatch from '@airbnb/node-memwatch'; +import SAXParser from '../../packages/parse5-sax-parser/lib/index.js'; main(); @@ -16,13 +14,13 @@ async function main() { const heapDiffMeasurement = new memwatch.HeapDiff(); let heapDiff = null; - memwatch.on('stats', stats => { + memwatch.on('stats', (stats) => { maxMemUsage = Math.max(maxMemUsage, stats['current_base']); }); startDate = new Date(); - const parserPromise = parse().then(dataSize => { + const parserPromise = parse().then((dataSize) => { parsedDataSize = dataSize; endDate = new Date(); heapDiff = heapDiffMeasurement.end(); @@ -30,7 +28,7 @@ async function main() { await Promise.all([ parserPromise, - promisifyEvent(memwatch, 'stats') // NOTE: we need at least one `stats` result + promisifyEvent(memwatch, 'stats'), // NOTE: we need at least one `stats` result ]); printResults(parsedDataSize, startDate, endDate, heapDiff, maxMemUsage); @@ -57,7 +55,7 @@ function getDuration(startDate, endDate) { const scale = new format.Scale({ seconds: 1, minutes: 60, - hours: 3600 + hours: 3600, }); return format((endDate - startDate) / 1000, { scale: scale }); diff --git a/bench/package.json b/bench/package.json index dbb9794b2..8a8e45512 100644 --- a/bench/package.json +++ b/bench/package.json @@ -1,5 +1,6 @@ { "name": "parse5-benchmarks", + "type": "module", "version": "1.0.0", "description": "parse5 regression benchmarks", "author": "Ivan Nikulin ", @@ -7,7 +8,7 @@ "dependencies": { "benchmark": "^2.1.4", "human-format": "^0.7.0", - "node-memwatch": "^1.0.1", + "@airbnb/node-memwatch": "^2.0.0", "parse5": "*", "promisify-event": "^1.0.0" } diff --git a/bench/perf/index.js b/bench/perf/index.js index ae87e0496..591fd0f01 100644 --- a/bench/perf/index.js +++ b/bench/perf/index.js @@ -1,37 +1,38 @@ -'use strict'; - -const { join } = require('path'); -const { readFileSync, createReadStream, readdirSync } = require('fs'); -const Benchmark = require('benchmark'); -const { loadTreeConstructionTestData } = require('../../test/utils/generate-parsing-tests'); -const loadSAXParserTestData = require('../../test/utils/load-sax-parser-test-data'); -const { treeAdapters, WritableStreamStub } = require('../../test/utils/common'); +import { readFileSync, createReadStream, readdirSync } from 'fs'; +import Benchmark from 'benchmark'; +import { loadTreeConstructionTestData } from '../../test/utils/generate-parsing-tests.js'; +import { loadSAXParserTestData } from '../../test/utils/load-sax-parser-test-data.js'; +import { treeAdapters, WritableStreamStub } from '../../test/utils/common.js'; +import * as parse5 from '../../packages/parse5/lib/index.js'; +import { ParserStream as parse5Stream } from '../../packages/parse5-parser-stream/lib/index.js'; +import * as parse5Upstream from 'parse5'; + +const hugePagePath = new URL('../../test/data/huge-page/huge-page.html', import.meta.url); +const treeConstructionPath = new URL('../../test/data/html5lib-tests/tree-construction', import.meta.url); +const saxPath = new URL('../../test/data/sax/', import.meta.url); //HACK: https://github.com/bestiejs/benchmark.js/issues/51 /* global workingCopy, WorkingCopyParserStream, upstreamParser, hugePage, microTests, runMicro, runPages, files */ -global.workingCopy = require('../../packages/parse5/lib'); -global.WorkingCopyParserStream = require('../../packages/parse5-parser-stream/lib'); -global.upstreamParser = require('parse5'); +global.workingCopy = parse5; +global.WorkingCopyParserStream = parse5Stream; +global.upstreamParser = parse5Upstream; // Huge page data -global.hugePage = readFileSync(join(__dirname, '../../test/data/huge-page/huge-page.html')).toString(); +global.hugePage = readFileSync(hugePagePath).toString(); // Micro data -global.microTests = loadTreeConstructionTestData( - [join(__dirname, '../../test/data/html5lib-tests/tree-construction')], - treeAdapters.default -) +global.microTests = loadTreeConstructionTestData([treeConstructionPath], treeAdapters.default) .filter( - test => + (test) => //NOTE: this test caused stack overflow in parse5 v1.x test.input !== '