typenative 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/ci.yml +2 -2
- package/.github/workflows/npm-publish-github-packages.yml +2 -2
- package/CHANGELOG.md +271 -1
- package/CONTRIBUTION.md +2 -1
- package/README.md +15 -7
- package/bin/cli.js +1 -0
- package/bin/index.js +62 -3
- package/bin/main.js +1 -0
- package/bin/node/core.js +41 -0
- package/bin/node/events.js +15 -10
- package/bin/node/process.js +13 -0
- package/bin/node/punycode.js +2 -2
- package/bin/node/timers.js +4 -2
- package/bin/node/url.js +1 -1
- package/bin/parse-node.js +2 -2
- package/bin/regex_vm.js +2057 -0
- package/bin/transpiler.js +17225 -4830
- package/bin/typecheck-node.js +2 -1
- package/bin/version.js +1 -1
- package/conformance.md +57 -15
- package/examples/README.md +46 -23
- package/examples/angular-templates/README.md +139 -0
- package/examples/angular-templates/bench.mjs +173 -0
- package/examples/angular-templates/dom-type-globals.d.ts +21 -0
- package/examples/angular-templates/dom-type-stubs.ts +21 -0
- package/examples/angular-templates/main.ts +135 -0
- package/examples/angular-templates/parity.mjs +157 -0
- package/examples/angular-templates/run-node.mjs +69 -0
- package/examples/angular-templates/vendor/angular/compiler/LICENSE +21 -0
- package/examples/desktop-notes/README.md +11 -13
- package/examples/dupe-finder/README.md +7 -5
- package/examples/fibonacci/README.md +7 -7
- package/examples/hello-world/README.md +6 -6
- package/examples/json-server/README.md +19 -18
- package/examples/mandelbrot-png/README.md +11 -13
- package/examples/marked-cli/README.md +38 -37
- package/examples/openai-chat/README.md +148 -0
- package/examples/openai-chat/bench.mjs +175 -0
- package/examples/openai-chat/main.ts +488 -0
- package/examples/react-compiler/README.md +170 -0
- package/examples/react-compiler/bench.mjs +181 -0
- package/examples/react-compiler/main.ts +158 -0
- package/examples/react-compiler/parity.mjs +362 -0
- package/examples/react-compiler/run-node.mjs +88 -0
- package/examples/react-compiler/vendor/babel/LICENSE +22 -0
- package/examples/react-compiler/vendor/babel/helper-globals/data/builtin-lower.json +15 -0
- package/examples/react-compiler/vendor/babel/helper-globals/data/builtin-upper.json +51 -0
- package/examples/react-compiler/vendor/charcodes/package.json +25 -0
- package/examples/react-compiler/vendor/invariant/LICENSE +21 -0
- package/examples/react-compiler/vendor/invariant/invariant.cjs +51 -0
- package/examples/react-compiler/vendor/invariant/invariant.d.cts +11 -0
- package/examples/react-compiler/vendor/invariant/types/LICENSE +21 -0
- package/examples/react-compiler/vendor/jridgewell/gen-mapping/LICENSE +19 -0
- package/examples/react-compiler/vendor/jridgewell/resolve-uri/LICENSE +19 -0
- package/examples/react-compiler/vendor/jridgewell/sourcemap-codec/LICENSE +19 -0
- package/examples/react-compiler/vendor/jridgewell/trace-mapping/LICENSE +19 -0
- package/examples/react-compiler/vendor/js-tokens/LICENSE +21 -0
- package/examples/react-compiler/vendor/js-tokens/index.cjs +23 -0
- package/examples/react-compiler/vendor/js-tokens/index.d.cts +13 -0
- package/examples/react-compiler/vendor/jsesc/LICENSE-MIT.txt +20 -0
- package/examples/react-compiler/vendor/jsesc/jsesc.cjs +337 -0
- package/examples/react-compiler/vendor/jsesc/jsesc.d.cts +384 -0
- package/examples/react-compiler/vendor/jsesc/types/LICENSE +21 -0
- package/examples/react-compiler/vendor/picocolors/LICENSE +15 -0
- package/examples/react-compiler/vendor/picocolors/picocolors.cjs +75 -0
- package/examples/react-compiler/vendor/picocolors/picocolors.d.cts +5 -0
- package/examples/react-compiler/vendor/picocolors/types.d.ts +51 -0
- package/examples/react-compiler/vendor/react-compiler/LICENSE +21 -0
- package/examples/react-compiler/vendor/stubs/babel-core.ts +31 -0
- package/examples/react-compiler/vendor/stubs/debug.ts +18 -0
- package/examples/react-compiler/vendor/stubs/js-tokens.d.ts +13 -0
- package/examples/react-compiler/vendor/stubs/pretty-format.ts +18 -0
- package/examples/react-compiler/vendor/zod/LICENSE +21 -0
- package/examples/react-compiler/vendor/zod-validation-error/LICENSE +9 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/utils/NonEmptyArray.ts +5 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/utils/joinPath.ts +49 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/utils/joinValues.ts +48 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/utils/stringify.ts +50 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/utils/titleCase.ts +6 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/MessageBuilder.ts +126 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/ValidationError.ts +39 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/custom.ts +12 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/errorMap.ts +78 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/index.ts +2 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidElement.ts +12 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidKey.ts +12 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidStringFormat.ts +131 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidType.ts +54 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidUnion.ts +12 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidValue.ts +40 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/notMultipleOf.ts +12 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/tooBig.ts +76 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/tooSmall.ts +76 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/types.ts +27 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/unrecognizedKeys.ts +27 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/fromError.ts +13 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/fromZodError.ts +61 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/fromZodIssue.ts +38 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/index.ts +20 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/isValidationError.ts +5 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/isValidationErrorLike.ts +8 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/isZodErrorLike.ts +11 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/toValidationError.ts +20 -0
- package/examples/react-compiler/vendor.mjs +688 -0
- package/examples/todo-cli/README.md +9 -9
- package/examples/yaml-cli/README.md +38 -28
- package/package.json +8 -5
- package/performance/README.md +195 -0
- package/performance/any-dynamic.ts +21 -0
- package/performance/any-record-view.ts +20 -0
- package/performance/arr-hof.ts +21 -0
- package/performance/arr-push-index.ts +15 -0
- package/performance/arr-sort.ts +21 -0
- package/performance/async-await.ts +24 -0
- package/performance/class-dispatch.ts +52 -0
- package/performance/events-emit.ts +27 -0
- package/performance/exc-throw-rare.ts +25 -0
- package/performance/exc-try-catch.ts +26 -0
- package/performance/fn-closures.ts +29 -0
- package/performance/fn-recursion-tree.ts +31 -0
- package/performance/gen-for-of.ts +46 -0
- package/performance/json-roundtrip.ts +27 -0
- package/performance/map-delete.ts +24 -0
- package/performance/map-string-keys.ts +22 -0
- package/performance/num-arith-loop.ts +14 -0
- package/performance/num-bitwise.ts +21 -0
- package/performance/num-mod-int.ts +16 -0
- package/performance/num-to-string.ts +16 -0
- package/performance/obj-alloc.ts +32 -0
- package/performance/regex-exec.ts +29 -0
- package/performance/regex-site.ts +28 -0
- package/performance/set-ops.ts +20 -0
- package/performance/str-char-scan.ts +35 -0
- package/performance/str-concat-plus.ts +18 -0
- package/performance/str-concat.ts +24 -0
- package/performance/str-short-scan.ts +26 -0
- package/performance/str-split-join.ts +23 -0
- package/performance/str-template.ts +16 -0
- package/performance/union-switch.ts +57 -0
- package/tsparser/check.go +2 -1
- package/tsparser/main.go +1 -0
- package/types/typenative-npm.d.ts +3 -2
- package/types/typenative.d.ts +42 -12
- package/ROUND2-HANDOFF.md +0 -200
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright 2024 Justin Ridgewell <justin@ridgewell.name>
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
|
11
|
+
all copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
SOFTWARE.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright 2019 Justin Ridgewell <jridgewell@google.com>
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
|
11
|
+
all copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
SOFTWARE.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright 2024 Justin Ridgewell <justin@ridgewell.name>
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
|
11
|
+
all copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
SOFTWARE.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright 2024 Justin Ridgewell <justin@ridgewell.name>
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
|
11
|
+
all copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
SOFTWARE.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2014, 2015, 2016, 2017, 2018 Simon Lydell
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Copyright 2014, 2015, 2016, 2017, 2018 Simon Lydell
|
|
2
|
+
// License: MIT. (See LICENSE.)
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
})
|
|
7
|
+
|
|
8
|
+
// This regex comes from regex.coffee, and is inserted here by generate-index.js
|
|
9
|
+
// (run `npm run build`).
|
|
10
|
+
exports.default = /((['"])(?:(?!\2|\\).|\\(?:\r\n|[\s\S]))*(\2)?|`(?:[^`\\$]|\\[\s\S]|\$(?!\{)|\$\{(?:[^{}]|\{[^}]*\}?)*\}?)*(`)?)|(\/\/.*)|(\/\*(?:[^*]|\*(?!\/))*(\*\/)?)|(\/(?!\*)(?:\[(?:(?![\]\\]).|\\.)*\]|(?![\/\]\\]).|\\.)+\/(?:(?!\s*(?:\b|[\u0080-\uFFFF$\\'"~({]|[+\-!](?!=)|\.?\d))|[gmiyus]{1,6}\b(?![\u0080-\uFFFF$\\]|\s*(?:[+\-*%&|^<>!=?({]|\/(?![\/*])))))|(0[xX][\da-fA-F]+|0[oO][0-7]+|0[bB][01]+|(?:\d*\.\d+|\d+\.?)(?:[eE][+-]?\d+)?)|((?!\d)(?:(?!\s)[$\w\u0080-\uFFFF]|\\u[\da-fA-F]{4}|\\u\{[\da-fA-F]+\})+)|(--|\+\+|&&|\|\||=>|\.{3}|(?:[+\-\/%&|^]|\*{1,2}|<{1,2}|>{1,3}|!=?|={1,2})=?|[?~.,:;[\](){}])|(\s+)|(^$|[\s\S])/g
|
|
11
|
+
|
|
12
|
+
exports.matchToToken = function(match) {
|
|
13
|
+
var token = {type: "invalid", value: match[0], closed: undefined}
|
|
14
|
+
if (match[ 1]) token.type = "string" , token.closed = !!(match[3] || match[4])
|
|
15
|
+
else if (match[ 5]) token.type = "comment"
|
|
16
|
+
else if (match[ 6]) token.type = "comment", token.closed = !!match[7]
|
|
17
|
+
else if (match[ 8]) token.type = "regex"
|
|
18
|
+
else if (match[ 9]) token.type = "number"
|
|
19
|
+
else if (match[10]) token.type = "name"
|
|
20
|
+
else if (match[11]) token.type = "punctuator"
|
|
21
|
+
else if (match[12]) token.type = "whitespace"
|
|
22
|
+
return token
|
|
23
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Declarations for js-tokens@4 (which ships none), as Babel 7's
|
|
2
|
+
// @babel/code-frame uses it: the default export is the tokenizing RegExp and
|
|
3
|
+
// `matchToToken` turns one of its matches into a token. Babel's highlighter
|
|
4
|
+
// also names the token types of js-tokens@8 in its Babel 8 branch, so those
|
|
5
|
+
// are declared too (type-only).
|
|
6
|
+
|
|
7
|
+
declare const jsTokens: RegExp & { default: RegExp };
|
|
8
|
+
declare namespace jsTokens {
|
|
9
|
+
function matchToToken(match: RegExpExecArray): { type: string; value: string; closed?: boolean };
|
|
10
|
+
type Token = { type: string; value: string; closed?: boolean };
|
|
11
|
+
type JSXToken = { type: string; value: string; closed?: boolean };
|
|
12
|
+
}
|
|
13
|
+
export = jsTokens;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright Mathias Bynens <https://mathiasbynens.be/>
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const object = {};
|
|
4
|
+
const hasOwnProperty = object.hasOwnProperty;
|
|
5
|
+
const forOwn = (object, callback) => {
|
|
6
|
+
for (const key in object) {
|
|
7
|
+
if (hasOwnProperty.call(object, key)) {
|
|
8
|
+
callback(key, object[key]);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const extend = (destination, source) => {
|
|
14
|
+
if (!source) {
|
|
15
|
+
return destination;
|
|
16
|
+
}
|
|
17
|
+
forOwn(source, (key, value) => {
|
|
18
|
+
destination[key] = value;
|
|
19
|
+
});
|
|
20
|
+
return destination;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const forEach = (array, callback) => {
|
|
24
|
+
const length = array.length;
|
|
25
|
+
let index = -1;
|
|
26
|
+
while (++index < length) {
|
|
27
|
+
callback(array[index]);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const fourHexEscape = (hex) => {
|
|
32
|
+
return '\\u' + ('0000' + hex).slice(-4);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const hexadecimal = (code, lowercase) => {
|
|
36
|
+
let hexadecimal = code.toString(16);
|
|
37
|
+
if (lowercase) return hexadecimal;
|
|
38
|
+
return hexadecimal.toUpperCase();
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const toString = object.toString;
|
|
42
|
+
const isArray = Array.isArray;
|
|
43
|
+
const isBuffer = (value) => {
|
|
44
|
+
return typeof Buffer === 'function' && Buffer.isBuffer(value);
|
|
45
|
+
};
|
|
46
|
+
const isObject = (value) => {
|
|
47
|
+
// This is a very simple check, but it’s good enough for what we need.
|
|
48
|
+
return toString.call(value) == '[object Object]';
|
|
49
|
+
};
|
|
50
|
+
const isString = (value) => {
|
|
51
|
+
return typeof value == 'string' ||
|
|
52
|
+
toString.call(value) == '[object String]';
|
|
53
|
+
};
|
|
54
|
+
const isNumber = (value) => {
|
|
55
|
+
return typeof value == 'number' ||
|
|
56
|
+
toString.call(value) == '[object Number]';
|
|
57
|
+
};
|
|
58
|
+
const isBigInt = (value) => {
|
|
59
|
+
return typeof value == 'bigint';
|
|
60
|
+
};
|
|
61
|
+
const isFunction = (value) => {
|
|
62
|
+
return typeof value == 'function';
|
|
63
|
+
};
|
|
64
|
+
const isMap = (value) => {
|
|
65
|
+
return toString.call(value) == '[object Map]';
|
|
66
|
+
};
|
|
67
|
+
const isSet = (value) => {
|
|
68
|
+
return toString.call(value) == '[object Set]';
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/*--------------------------------------------------------------------------*/
|
|
72
|
+
|
|
73
|
+
// https://mathiasbynens.be/notes/javascript-escapes#single
|
|
74
|
+
const singleEscapes = {
|
|
75
|
+
'\\': '\\\\',
|
|
76
|
+
'\b': '\\b',
|
|
77
|
+
'\f': '\\f',
|
|
78
|
+
'\n': '\\n',
|
|
79
|
+
'\r': '\\r',
|
|
80
|
+
'\t': '\\t'
|
|
81
|
+
// `\v` is omitted intentionally, because in IE < 9, '\v' == 'v'.
|
|
82
|
+
// '\v': '\\x0B'
|
|
83
|
+
};
|
|
84
|
+
const regexSingleEscape = /[\\\b\f\n\r\t]/;
|
|
85
|
+
|
|
86
|
+
const regexDigit = /[0-9]/;
|
|
87
|
+
const regexWhitespace = /[\xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000]/;
|
|
88
|
+
|
|
89
|
+
const escapeEverythingRegex = /([\uD800-\uDBFF][\uDC00-\uDFFF])|([\uD800-\uDFFF])|(['"`])|[^]/g;
|
|
90
|
+
const escapeNonAsciiRegex = /([\uD800-\uDBFF][\uDC00-\uDFFF])|([\uD800-\uDFFF])|(['"`])|[^ !#-&\(-\[\]-_a-~]/g;
|
|
91
|
+
|
|
92
|
+
const jsesc = (argument, options) => {
|
|
93
|
+
const increaseIndentation = () => {
|
|
94
|
+
oldIndent = indent;
|
|
95
|
+
++options.indentLevel;
|
|
96
|
+
indent = options.indent.repeat(options.indentLevel)
|
|
97
|
+
};
|
|
98
|
+
// Handle options
|
|
99
|
+
const defaults = {
|
|
100
|
+
'escapeEverything': false,
|
|
101
|
+
'minimal': false,
|
|
102
|
+
'isScriptContext': false,
|
|
103
|
+
'quotes': 'single',
|
|
104
|
+
'wrap': false,
|
|
105
|
+
'es6': false,
|
|
106
|
+
'json': false,
|
|
107
|
+
'compact': true,
|
|
108
|
+
'lowercaseHex': false,
|
|
109
|
+
'numbers': 'decimal',
|
|
110
|
+
'indent': '\t',
|
|
111
|
+
'indentLevel': 0,
|
|
112
|
+
'__inline1__': false,
|
|
113
|
+
'__inline2__': false
|
|
114
|
+
};
|
|
115
|
+
const json = options && options.json;
|
|
116
|
+
if (json) {
|
|
117
|
+
defaults.quotes = 'double';
|
|
118
|
+
defaults.wrap = true;
|
|
119
|
+
}
|
|
120
|
+
options = extend(defaults, options);
|
|
121
|
+
if (
|
|
122
|
+
options.quotes != 'single' &&
|
|
123
|
+
options.quotes != 'double' &&
|
|
124
|
+
options.quotes != 'backtick'
|
|
125
|
+
) {
|
|
126
|
+
options.quotes = 'single';
|
|
127
|
+
}
|
|
128
|
+
const quote = options.quotes == 'double' ?
|
|
129
|
+
'"' :
|
|
130
|
+
(options.quotes == 'backtick' ?
|
|
131
|
+
'`' :
|
|
132
|
+
'\''
|
|
133
|
+
);
|
|
134
|
+
const compact = options.compact;
|
|
135
|
+
const lowercaseHex = options.lowercaseHex;
|
|
136
|
+
let indent = options.indent.repeat(options.indentLevel);
|
|
137
|
+
let oldIndent = '';
|
|
138
|
+
const inline1 = options.__inline1__;
|
|
139
|
+
const inline2 = options.__inline2__;
|
|
140
|
+
const newLine = compact ? '' : '\n';
|
|
141
|
+
let result;
|
|
142
|
+
let isEmpty = true;
|
|
143
|
+
const useBinNumbers = options.numbers == 'binary';
|
|
144
|
+
const useOctNumbers = options.numbers == 'octal';
|
|
145
|
+
const useDecNumbers = options.numbers == 'decimal';
|
|
146
|
+
const useHexNumbers = options.numbers == 'hexadecimal';
|
|
147
|
+
|
|
148
|
+
if (json && argument && isFunction(argument.toJSON)) {
|
|
149
|
+
argument = argument.toJSON();
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (!isString(argument)) {
|
|
153
|
+
if (isMap(argument)) {
|
|
154
|
+
if (argument.size == 0) {
|
|
155
|
+
return 'new Map()';
|
|
156
|
+
}
|
|
157
|
+
if (!compact) {
|
|
158
|
+
options.__inline1__ = true;
|
|
159
|
+
options.__inline2__ = false;
|
|
160
|
+
}
|
|
161
|
+
return 'new Map(' + jsesc(Array.from(argument), options) + ')';
|
|
162
|
+
}
|
|
163
|
+
if (isSet(argument)) {
|
|
164
|
+
if (argument.size == 0) {
|
|
165
|
+
return 'new Set()';
|
|
166
|
+
}
|
|
167
|
+
return 'new Set(' + jsesc(Array.from(argument), options) + ')';
|
|
168
|
+
}
|
|
169
|
+
if (isBuffer(argument)) {
|
|
170
|
+
if (argument.length == 0) {
|
|
171
|
+
return 'Buffer.from([])';
|
|
172
|
+
}
|
|
173
|
+
return 'Buffer.from(' + jsesc(Array.from(argument), options) + ')';
|
|
174
|
+
}
|
|
175
|
+
if (isArray(argument)) {
|
|
176
|
+
result = [];
|
|
177
|
+
options.wrap = true;
|
|
178
|
+
if (inline1) {
|
|
179
|
+
options.__inline1__ = false;
|
|
180
|
+
options.__inline2__ = true;
|
|
181
|
+
}
|
|
182
|
+
if (!inline2) {
|
|
183
|
+
increaseIndentation();
|
|
184
|
+
}
|
|
185
|
+
forEach(argument, (value) => {
|
|
186
|
+
isEmpty = false;
|
|
187
|
+
if (inline2) {
|
|
188
|
+
options.__inline2__ = false;
|
|
189
|
+
}
|
|
190
|
+
result.push(
|
|
191
|
+
(compact || inline2 ? '' : indent) +
|
|
192
|
+
jsesc(value, options)
|
|
193
|
+
);
|
|
194
|
+
});
|
|
195
|
+
if (isEmpty) {
|
|
196
|
+
return '[]';
|
|
197
|
+
}
|
|
198
|
+
if (inline2) {
|
|
199
|
+
return '[' + result.join(', ') + ']';
|
|
200
|
+
}
|
|
201
|
+
return '[' + newLine + result.join(',' + newLine) + newLine +
|
|
202
|
+
(compact ? '' : oldIndent) + ']';
|
|
203
|
+
} else if (isNumber(argument) || isBigInt(argument)) {
|
|
204
|
+
if (json) {
|
|
205
|
+
// Some number values (e.g. `Infinity`) cannot be represented in JSON.
|
|
206
|
+
// `BigInt` values less than `-Number.MAX_VALUE` or greater than
|
|
207
|
+
// `Number.MAX_VALUE` cannot be represented in JSON so they will become
|
|
208
|
+
// `-Infinity` or `Infinity`, respectively, and then become `null` when
|
|
209
|
+
// stringified.
|
|
210
|
+
return JSON.stringify(Number(argument));
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
let result;
|
|
214
|
+
if (useDecNumbers) {
|
|
215
|
+
result = String(argument);
|
|
216
|
+
} else if (useHexNumbers) {
|
|
217
|
+
let hexadecimal = argument.toString(16);
|
|
218
|
+
if (!lowercaseHex) {
|
|
219
|
+
hexadecimal = hexadecimal.toUpperCase();
|
|
220
|
+
}
|
|
221
|
+
result = '0x' + hexadecimal;
|
|
222
|
+
} else if (useBinNumbers) {
|
|
223
|
+
result = '0b' + argument.toString(2);
|
|
224
|
+
} else if (useOctNumbers) {
|
|
225
|
+
result = '0o' + argument.toString(8);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
if (isBigInt(argument)) {
|
|
229
|
+
return result + 'n';
|
|
230
|
+
}
|
|
231
|
+
return result;
|
|
232
|
+
} else if (isBigInt(argument)) {
|
|
233
|
+
if (json) {
|
|
234
|
+
// `BigInt` values less than `-Number.MAX_VALUE` or greater than
|
|
235
|
+
// `Number.MAX_VALUE` will become `-Infinity` or `Infinity`,
|
|
236
|
+
// respectively, and cannot be represented in JSON.
|
|
237
|
+
return JSON.stringify(Number(argument));
|
|
238
|
+
}
|
|
239
|
+
return argument + 'n';
|
|
240
|
+
} else if (!isObject(argument)) {
|
|
241
|
+
if (json) {
|
|
242
|
+
// For some values (e.g. `undefined`, `function` objects),
|
|
243
|
+
// `JSON.stringify(value)` returns `undefined` (which isn’t valid
|
|
244
|
+
// JSON) instead of `'null'`.
|
|
245
|
+
return JSON.stringify(argument) || 'null';
|
|
246
|
+
}
|
|
247
|
+
return String(argument);
|
|
248
|
+
} else { // it’s an object
|
|
249
|
+
result = [];
|
|
250
|
+
options.wrap = true;
|
|
251
|
+
increaseIndentation();
|
|
252
|
+
forOwn(argument, (key, value) => {
|
|
253
|
+
isEmpty = false;
|
|
254
|
+
result.push(
|
|
255
|
+
(compact ? '' : indent) +
|
|
256
|
+
jsesc(key, options) + ':' +
|
|
257
|
+
(compact ? '' : ' ') +
|
|
258
|
+
jsesc(value, options)
|
|
259
|
+
);
|
|
260
|
+
});
|
|
261
|
+
if (isEmpty) {
|
|
262
|
+
return '{}';
|
|
263
|
+
}
|
|
264
|
+
return '{' + newLine + result.join(',' + newLine) + newLine +
|
|
265
|
+
(compact ? '' : oldIndent) + '}';
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
const regex = options.escapeEverything ? escapeEverythingRegex : escapeNonAsciiRegex;
|
|
270
|
+
result = argument.replace(regex, (char, pair, lone, quoteChar, index, string) => {
|
|
271
|
+
if (pair) {
|
|
272
|
+
if (options.minimal) return pair;
|
|
273
|
+
const first = pair.charCodeAt(0);
|
|
274
|
+
const second = pair.charCodeAt(1);
|
|
275
|
+
if (options.es6) {
|
|
276
|
+
// https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
|
|
277
|
+
const codePoint = (first - 0xD800) * 0x400 + second - 0xDC00 + 0x10000;
|
|
278
|
+
const hex = hexadecimal(codePoint, lowercaseHex);
|
|
279
|
+
return '\\u{' + hex + '}';
|
|
280
|
+
}
|
|
281
|
+
return fourHexEscape(hexadecimal(first, lowercaseHex)) + fourHexEscape(hexadecimal(second, lowercaseHex));
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
if (lone) {
|
|
285
|
+
return fourHexEscape(hexadecimal(lone.charCodeAt(0), lowercaseHex));
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
if (
|
|
289
|
+
char == '\0' &&
|
|
290
|
+
!json &&
|
|
291
|
+
!regexDigit.test(string.charAt(index + 1))
|
|
292
|
+
) {
|
|
293
|
+
return '\\0';
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
if (quoteChar) {
|
|
297
|
+
if (quoteChar == quote || options.escapeEverything) {
|
|
298
|
+
return '\\' + quoteChar;
|
|
299
|
+
}
|
|
300
|
+
return quoteChar;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
if (regexSingleEscape.test(char)) {
|
|
304
|
+
// no need for a `hasOwnProperty` check here
|
|
305
|
+
return singleEscapes[char];
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
if (options.minimal && !regexWhitespace.test(char)) {
|
|
309
|
+
return char;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
const hex = hexadecimal(char.charCodeAt(0), lowercaseHex);
|
|
313
|
+
if (json || hex.length > 2) {
|
|
314
|
+
return fourHexEscape(hex);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
return '\\x' + ('00' + hex).slice(-2);
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
if (quote == '`') {
|
|
321
|
+
result = result.replace(/\$\{/g, '\\${');
|
|
322
|
+
}
|
|
323
|
+
if (options.isScriptContext) {
|
|
324
|
+
// https://mathiasbynens.be/notes/etago
|
|
325
|
+
result = result
|
|
326
|
+
.replace(/<\/(script|style)/gi, '<\\/$1')
|
|
327
|
+
.replace(/<!--/g, json ? '\\u003C!--' : '\\x3C!--');
|
|
328
|
+
}
|
|
329
|
+
if (options.wrap) {
|
|
330
|
+
result = quote + result + quote;
|
|
331
|
+
}
|
|
332
|
+
return result;
|
|
333
|
+
};
|
|
334
|
+
|
|
335
|
+
jsesc.version = '3.0.2';
|
|
336
|
+
|
|
337
|
+
module.exports = jsesc;
|