This commit is contained in:
2025-05-09 05:30:08 +02:00
parent 7bb10e7df4
commit 73367bad9e
5322 changed files with 1266973 additions and 313 deletions

View File

@@ -0,0 +1,5 @@
Copyright 2018 Kilian Valkhof
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

View File

@@ -0,0 +1,296 @@
/**
* @fileoverview Restrict usage of duplicate imports.
* @author Simen Bekkhus
*/
"use strict";
//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
const NAMED_TYPES = ["ImportSpecifier", "ExportSpecifier"];
const NAMESPACE_TYPES = [
"ImportNamespaceSpecifier",
"ExportNamespaceSpecifier",
];
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
/**
* Check if an import/export type belongs to (ImportSpecifier|ExportSpecifier) or (ImportNamespaceSpecifier|ExportNamespaceSpecifier).
* @param {string} importExportType An import/export type to check.
* @param {string} type Can be "named" or "namespace"
* @returns {boolean} True if import/export type belongs to (ImportSpecifier|ExportSpecifier) or (ImportNamespaceSpecifier|ExportNamespaceSpecifier) and false if it doesn't.
*/
function isImportExportSpecifier(importExportType, type) {
const arrayToCheck = type === "named" ? NAMED_TYPES : NAMESPACE_TYPES;
return arrayToCheck.includes(importExportType);
}
/**
* Return the type of (import|export).
* @param {ASTNode} node A node to get.
* @returns {string} The type of the (import|export).
*/
function getImportExportType(node) {
if (node.specifiers && node.specifiers.length > 0) {
const nodeSpecifiers = node.specifiers;
const index = nodeSpecifiers.findIndex(
({ type }) =>
isImportExportSpecifier(type, "named") ||
isImportExportSpecifier(type, "namespace"),
);
const i = index > -1 ? index : 0;
return nodeSpecifiers[i].type;
}
if (node.type === "ExportAllDeclaration") {
if (node.exported) {
return "ExportNamespaceSpecifier";
}
return "ExportAll";
}
return "SideEffectImport";
}
/**
* Returns a boolean indicates if two (import|export) can be merged
* @param {ASTNode} node1 A node to check.
* @param {ASTNode} node2 A node to check.
* @returns {boolean} True if two (import|export) can be merged, false if they can't.
*/
function isImportExportCanBeMerged(node1, node2) {
const importExportType1 = getImportExportType(node1);
const importExportType2 = getImportExportType(node2);
if (
(importExportType1 === "ExportAll" &&
importExportType2 !== "ExportAll" &&
importExportType2 !== "SideEffectImport") ||
(importExportType1 !== "ExportAll" &&
importExportType1 !== "SideEffectImport" &&
importExportType2 === "ExportAll")
) {
return false;
}
if (
(isImportExportSpecifier(importExportType1, "namespace") &&
isImportExportSpecifier(importExportType2, "named")) ||
(isImportExportSpecifier(importExportType2, "namespace") &&
isImportExportSpecifier(importExportType1, "named"))
) {
return false;
}
return true;
}
/**
* Returns a boolean if we should report (import|export).
* @param {ASTNode} node A node to be reported or not.
* @param {[ASTNode]} previousNodes An array contains previous nodes of the module imported or exported.
* @returns {boolean} True if the (import|export) should be reported.
*/
function shouldReportImportExport(node, previousNodes) {
let i = 0;
while (i < previousNodes.length) {
if (isImportExportCanBeMerged(node, previousNodes[i])) {
return true;
}
i++;
}
return false;
}
/**
* Returns array contains only nodes with declarations types equal to type.
* @param {[{node: ASTNode, declarationType: string}]} nodes An array contains objects, each object contains a node and a declaration type.
* @param {string} type Declaration type.
* @returns {[ASTNode]} An array contains only nodes with declarations types equal to type.
*/
function getNodesByDeclarationType(nodes, type) {
return nodes
.filter(({ declarationType }) => declarationType === type)
.map(({ node }) => node);
}
/**
* Returns the name of the module imported or re-exported.
* @param {ASTNode} node A node to get.
* @returns {string} The name of the module, or empty string if no name.
*/
function getModule(node) {
if (node && node.source && node.source.value) {
return node.source.value.trim();
}
return "";
}
/**
* Checks if the (import|export) can be merged with at least one import or one export, and reports if so.
* @param {RuleContext} context The ESLint rule context object.
* @param {ASTNode} node A node to get.
* @param {Map} modules A Map object contains as a key a module name and as value an array contains objects, each object contains a node and a declaration type.
* @param {string} declarationType A declaration type can be an import or export.
* @param {boolean} includeExports Whether or not to check for exports in addition to imports.
* @returns {void} No return value.
*/
function checkAndReport(
context,
node,
modules,
declarationType,
includeExports,
) {
const module = getModule(node);
if (modules.has(module)) {
const previousNodes = modules.get(module);
const messagesIds = [];
const importNodes = getNodesByDeclarationType(previousNodes, "import");
let exportNodes;
if (includeExports) {
exportNodes = getNodesByDeclarationType(previousNodes, "export");
}
if (declarationType === "import") {
if (shouldReportImportExport(node, importNodes)) {
messagesIds.push("import");
}
if (includeExports) {
if (shouldReportImportExport(node, exportNodes)) {
messagesIds.push("importAs");
}
}
} else if (declarationType === "export") {
if (shouldReportImportExport(node, exportNodes)) {
messagesIds.push("export");
}
if (shouldReportImportExport(node, importNodes)) {
messagesIds.push("exportAs");
}
}
messagesIds.forEach(messageId =>
context.report({
node,
messageId,
data: {
module,
},
}),
);
}
}
/**
* @callback nodeCallback
* @param {ASTNode} node A node to handle.
*/
/**
* Returns a function handling the (imports|exports) of a given file
* @param {RuleContext} context The ESLint rule context object.
* @param {Map} modules A Map object contains as a key a module name and as value an array contains objects, each object contains a node and a declaration type.
* @param {string} declarationType A declaration type can be an import or export.
* @param {boolean} includeExports Whether or not to check for exports in addition to imports.
* @returns {nodeCallback} A function passed to ESLint to handle the statement.
*/
function handleImportsExports(
context,
modules,
declarationType,
includeExports,
) {
return function (node) {
const module = getModule(node);
if (module) {
checkAndReport(
context,
node,
modules,
declarationType,
includeExports,
);
const currentNode = { node, declarationType };
let nodes = [currentNode];
if (modules.has(module)) {
const previousNodes = modules.get(module);
nodes = [...previousNodes, currentNode];
}
modules.set(module, nodes);
}
};
}
/** @type {import('../shared/types').Rule} */
module.exports = {
meta: {
type: "problem",
defaultOptions: [
{
includeExports: false,
},
],
docs: {
description: "Disallow duplicate module imports",
recommended: false,
url: "https://eslint.org/docs/latest/rules/no-duplicate-imports",
},
schema: [
{
type: "object",
properties: {
includeExports: {
type: "boolean",
},
},
additionalProperties: false,
},
],
messages: {
import: "'{{module}}' import is duplicated.",
importAs: "'{{module}}' import is duplicated as export.",
export: "'{{module}}' export is duplicated.",
exportAs: "'{{module}}' export is duplicated as import.",
},
},
create(context) {
const [{ includeExports }] = context.options;
const modules = new Map();
const handlers = {
ImportDeclaration: handleImportsExports(
context,
modules,
"import",
includeExports,
),
};
if (includeExports) {
handlers.ExportNamedDeclaration = handleImportsExports(
context,
modules,
"export",
includeExports,
);
handlers.ExportAllDeclaration = handleImportsExports(
context,
modules,
"export",
includeExports,
);
}
return handlers;
},
};

View File

@@ -0,0 +1,24 @@
# json-buffer
JSON functions that can convert buffers!
[![build status](https://secure.travis-ci.org/dominictarr/json-buffer.png)](http://travis-ci.org/dominictarr/json-buffer)
[![testling badge](https://ci.testling.com/dominictarr/json-buffer.png)](https://ci.testling.com/dominictarr/json-buffer)
JSON mangles buffers by converting to an array...
which isn't helpful. json-buffers converts to base64 instead,
and deconverts base64 to a buffer.
``` js
var JSONB = require('json-buffer')
var Buffer = require('buffer').Buffer
var str = JSONB.stringify(Buffer.from('hello there!'))
console.log(JSONB.parse(str)) //GET a BUFFER back
```
## License
MIT

View File

@@ -0,0 +1,16 @@
# Installation
> `npm install --save @types/react-dom`
# Summary
This package contains type definitions for react-dom (https://react.dev/).
# Details
Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/react-dom.
### Additional Details
* Last updated: Wed, 02 Apr 2025 09:02:14 GMT
* Dependencies: none
* Peer dependencies: [@types/react](https://npmjs.com/package/@types/react)
# Credits
These definitions were written by [Asana](https://asana.com), [AssureSign](http://www.assuresign.com), [Microsoft](https://microsoft.com), [MartynasZilinskas](https://github.com/MartynasZilinskas), [Josh Rutherford](https://github.com/theruther4d), [Jessica Franco](https://github.com/Jessidhia), and [Sebastian Silbermann](https://github.com/eps1lon).

View File

@@ -0,0 +1,33 @@
/**
* A function that merges React refs into one.
* Supports both functions and ref objects created using createRef() and useRef().
*
* Usage:
* ```tsx
* <div ref={mergeRefs(ref1, ref2, ref3)} />
* ```
*
* @param {(React.Ref<T> | undefined)[]} inputRefs Array of refs
* @returns {React.Ref<T> | React.RefCallback<T>} Merged refs
*/
export default function mergeRefs() {
var inputRefs = [];
for (var _i = 0; _i < arguments.length; _i++) {
inputRefs[_i] = arguments[_i];
}
var filteredInputRefs = inputRefs.filter(Boolean);
if (filteredInputRefs.length <= 1) {
var firstRef = filteredInputRefs[0];
return firstRef || null;
}
return function mergedRefs(ref) {
filteredInputRefs.forEach(function (inputRef) {
if (typeof inputRef === 'function') {
inputRef(ref);
}
else if (inputRef) {
inputRef.current = ref;
}
});
};
}

View File

@@ -0,0 +1,37 @@
'use strict';
var test = require('tape');
var parse = require('../');
test('boolean default true', function (t) {
var argv = parse([], {
boolean: 'sometrue',
default: { sometrue: true },
});
t.equal(argv.sometrue, true);
t.end();
});
test('boolean default false', function (t) {
var argv = parse([], {
boolean: 'somefalse',
default: { somefalse: false },
});
t.equal(argv.somefalse, false);
t.end();
});
test('boolean default to null', function (t) {
var argv = parse([], {
boolean: 'maybe',
default: { maybe: null },
});
t.equal(argv.maybe, null);
var argvLong = parse(['--maybe'], {
boolean: 'maybe',
default: { maybe: null },
});
t.equal(argvLong.maybe, true);
t.end();
});

View File

@@ -0,0 +1,7 @@
'use strict';
if (process.env.NODE_ENV === 'production') {
module.exports = require('../cjs/use-sync-external-store-shim.production.js');
} else {
module.exports = require('../cjs/use-sync-external-store-shim.development.js');
}

View File

@@ -0,0 +1 @@
{"version":3,"names":["_assertClassBrand","require","_classPrivateSetter","privateMap","setter","receiver","value","assertClassBrand"],"sources":["../../src/helpers/classPrivateSetter.ts"],"sourcesContent":["/* @minVersion 7.24.0 */\n\nimport assertClassBrand from \"./assertClassBrand.ts\";\n\nexport default function _classPrivateSetter(\n privateMap: WeakMap<any, any> | WeakSet<any>,\n setter: Function,\n receiver: any,\n value: any,\n) {\n setter(assertClassBrand(privateMap, receiver), value);\n return value;\n}\n"],"mappings":";;;;;;AAEA,IAAAA,iBAAA,GAAAC,OAAA;AAEe,SAASC,mBAAmBA,CACzCC,UAA4C,EAC5CC,MAAgB,EAChBC,QAAa,EACbC,KAAU,EACV;EACAF,MAAM,CAAC,IAAAG,yBAAgB,EAACJ,UAAU,EAAEE,QAAQ,CAAC,EAAEC,KAAK,CAAC;EACrD,OAAOA,KAAK;AACd","ignoreList":[]}

View File

@@ -0,0 +1,18 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = _defaults;
function _defaults(obj, defaults) {
for (var keys = Object.getOwnPropertyNames(defaults), i = 0; i < keys.length; i++) {
var key = keys[i],
value = Object.getOwnPropertyDescriptor(defaults, key);
if (value && value.configurable && obj[key] === undefined) {
Object.defineProperty(obj, key, value);
}
}
return obj;
}
//# sourceMappingURL=defaults.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;"}

View File

@@ -0,0 +1,88 @@
{
"name": "postcss",
"version": "8.5.3",
"description": "Tool for transforming styles with JS plugins",
"engines": {
"node": "^10 || ^12 || >=14"
},
"exports": {
".": {
"require": "./lib/postcss.js",
"import": "./lib/postcss.mjs"
},
"./lib/at-rule": "./lib/at-rule.js",
"./lib/comment": "./lib/comment.js",
"./lib/container": "./lib/container.js",
"./lib/css-syntax-error": "./lib/css-syntax-error.js",
"./lib/declaration": "./lib/declaration.js",
"./lib/fromJSON": "./lib/fromJSON.js",
"./lib/input": "./lib/input.js",
"./lib/lazy-result": "./lib/lazy-result.js",
"./lib/no-work-result": "./lib/no-work-result.js",
"./lib/list": "./lib/list.js",
"./lib/map-generator": "./lib/map-generator.js",
"./lib/node": "./lib/node.js",
"./lib/parse": "./lib/parse.js",
"./lib/parser": "./lib/parser.js",
"./lib/postcss": "./lib/postcss.js",
"./lib/previous-map": "./lib/previous-map.js",
"./lib/processor": "./lib/processor.js",
"./lib/result": "./lib/result.js",
"./lib/root": "./lib/root.js",
"./lib/rule": "./lib/rule.js",
"./lib/stringifier": "./lib/stringifier.js",
"./lib/stringify": "./lib/stringify.js",
"./lib/symbols": "./lib/symbols.js",
"./lib/terminal-highlight": "./lib/terminal-highlight.js",
"./lib/tokenize": "./lib/tokenize.js",
"./lib/warn-once": "./lib/warn-once.js",
"./lib/warning": "./lib/warning.js",
"./package.json": "./package.json"
},
"main": "./lib/postcss.js",
"types": "./lib/postcss.d.ts",
"keywords": [
"css",
"postcss",
"rework",
"preprocessor",
"parser",
"source map",
"transform",
"manipulation",
"transpiler"
],
"funding": [
{
"type": "opencollective",
"url": "https://opencollective.com/postcss/"
},
{
"type": "tidelift",
"url": "https://tidelift.com/funding/github/npm/postcss"
},
{
"type": "github",
"url": "https://github.com/sponsors/ai"
}
],
"author": "Andrey Sitnik <andrey@sitnik.ru>",
"license": "MIT",
"homepage": "https://postcss.org/",
"repository": "postcss/postcss",
"bugs": {
"url": "https://github.com/postcss/postcss/issues"
},
"dependencies": {
"nanoid": "^3.3.8",
"picocolors": "^1.1.1",
"source-map-js": "^1.2.1"
},
"browser": {
"./lib/terminal-highlight": false,
"source-map-js": false,
"path": false,
"url": false,
"fs": false
}
}

View File

@@ -0,0 +1,5 @@
export declare function ScriptOnce({ children, log, }: {
children: string;
log?: boolean;
sync?: boolean;
}): import("react/jsx-runtime").JSX.Element | null;

View File

@@ -0,0 +1 @@
module.exports={C:{"60":0.00295,"74":0.00147,"81":0.00147,"113":0.00147,"115":0.04419,"128":0.00147,"132":0.00589,"133":0.00295,"135":0.08249,"136":0.20769,"137":0.00147,_:"2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 61 62 63 64 65 66 67 68 69 70 71 72 73 75 76 77 78 79 80 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 114 116 117 118 119 120 121 122 123 124 125 126 127 129 130 131 134 138 139 140 3.5 3.6"},D:{"43":0.00442,"47":0.00295,"58":0.00737,"59":0.00737,"66":0.00295,"68":0.00295,"69":0.01326,"71":0.00147,"74":0.00442,"80":0.00147,"83":0.00589,"87":0.00147,"89":0.00589,"90":0.00147,"95":0.00147,"97":0.00147,"103":0.00442,"104":0.00147,"109":0.22832,"111":0.00295,"114":0.01326,"115":0.00295,"116":0.01031,"118":0.00295,"119":0.00589,"120":0.00737,"121":0.00295,"122":0.00737,"123":0.00147,"124":0.00295,"125":0.00147,"126":0.00884,"127":0.00737,"128":0.04124,"129":0.01031,"130":0.00147,"131":0.06187,"132":0.1473,"133":0.80131,"134":1.61735,"135":0.00147,_:"4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 44 45 46 48 49 50 51 52 53 54 55 56 57 60 61 62 63 64 65 67 70 72 73 75 76 77 78 79 81 84 85 86 88 91 92 93 94 96 98 99 100 101 102 105 106 107 108 110 112 113 117 136 137 138"},F:{"49":0.00589,"79":0.00147,"95":0.00884,"105":0.00147,"111":0.00147,"112":0.00442,"115":0.00147,"116":0.00147,"117":0.13846,_:"9 11 12 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 50 51 52 53 54 55 56 57 58 60 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 96 97 98 99 100 101 102 103 104 106 107 108 109 110 113 114 9.5-9.6 10.0-10.1 10.5 10.6 11.1 11.5 11.6 12.1"},B:{"13":0.00147,"14":0.00295,"15":0.00295,"16":0.00884,"17":0.00147,"18":0.00442,"84":0.00147,"89":0.00442,"90":0.00884,"92":0.02946,"98":0.00147,"100":0.00147,"117":0.00147,"118":0.08396,"120":0.00884,"122":0.01031,"125":0.00147,"126":0.03977,"127":0.00147,"128":0.01031,"130":0.00442,"131":0.01915,"132":0.00737,"133":0.24305,"134":0.4257,_:"12 79 80 81 83 85 86 87 88 91 93 94 95 96 97 99 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 119 121 123 124 129"},E:{"15":0.01768,_:"0 4 5 6 7 8 9 10 11 12 13 14 3.1 3.2 5.1 6.1 7.1 9.1 10.1 11.1 12.1 15.1 15.2-15.3 15.4 15.5 16.0 16.1 16.2 16.3 16.4 16.5 17.0 17.1 17.3 17.5 18.2 18.4","13.1":0.00884,"14.1":0.01031,"15.6":0.00589,"16.6":0.00442,"17.2":0.00147,"17.4":0.00147,"17.6":0.01473,"18.0":0.03241,"18.1":0.00442,"18.3":0.1311},G:{"8":0,"3.2":0,"4.0-4.1":0,"4.2-4.3":0.00205,"5.0-5.1":0,"6.0-6.1":0.00615,"7.0-7.1":0.0041,"8.1-8.4":0,"9.0-9.2":0.00308,"9.3":0.01436,"10.0-10.2":0.00103,"10.3":0.02359,"11.0-11.2":0.10873,"11.3-11.4":0.00718,"12.0-12.1":0.0041,"12.2-12.5":0.10155,"13.0-13.1":0.00205,"13.2":0.00308,"13.3":0.0041,"13.4-13.7":0.01436,"14.0-14.4":0.0359,"14.5-14.8":0.04308,"15.0-15.1":0.02359,"15.2-15.3":0.02359,"15.4":0.02872,"15.5":0.03283,"15.6-15.8":0.40416,"16.0":0.05744,"16.1":0.11797,"16.2":0.06155,"16.3":0.10668,"16.4":0.02359,"16.5":0.04411,"16.6-16.7":0.47905,"17.0":0.02872,"17.1":0.05129,"17.2":0.03898,"17.3":0.05437,"17.4":0.10873,"17.5":0.24209,"17.6-17.7":0.70267,"18.0":0.19695,"18.1":0.6442,"18.2":0.28825,"18.3":6.02451,"18.4":0.08924},P:{"4":0.10032,"20":0.01003,"21":0.04013,"22":0.14044,"23":0.0301,"24":0.04013,"25":0.02006,"26":0.42133,"27":0.24076,"5.0-5.4":0.01003,"6.2-6.4":0.02006,"7.2-7.4":0.29092,_:"8.2 10.1 12.0 14.0 15.0 16.0 17.0","9.2":0.01003,"11.1-11.2":0.01003,"13.0":0.02006,"18.0":0.02006,"19.0":0.11035},I:{"0":0.01702,"3":0,"4":0,"2.1":0,"2.2":0,"2.3":0,"4.1":0,"4.2-4.3":0.00001,"4.4":0,"4.4.3-4.4.4":0.00002},K:{"0":0.18759,_:"10 11 12 11.1 11.5 12.1"},A:{"11":0.00147,_:"6 7 8 9 10 5.5"},S:{_:"2.5 3.0-3.1"},J:{_:"7 10"},N:{_:"10 11"},R:{_:"0"},M:{"0":0.06822},Q:{_:"14.9"},O:{"0":0.07674},H:{"0":0},L:{"0":82.87921}};

View File

@@ -0,0 +1 @@
module.exports={A:{A:{"388":"A B","900":"K D E F mC"},B:{"388":"C L M G N O P","641":"0 9 r s t u v w x y z AB BB CB DB EB FB GB HB IB JB KB LB MB NB OB I","900":"Q H R S T U V W X Y Z a b c d e f g h i j k l m n o p q"},C:{"772":"0 9 0B 1B 2B 3B 4B 5B 6B 7B 8B 9B AC BC CC DC Q H R OC S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z AB BB CB DB EB FB GB HB IB JB KB LB MB NB OB I PC EC QC RC oC pC","900":"1 2 3 4 5 6 7 8 nC LC J PB K D E F A B C L M G N O P QB RB SB TB UB VB WB XB YB ZB aB bB cB dB eB fB gB hB iB jB kB lB mB nB oB pB qB rB sB tB uB vB MC wB NC xB yB zB qC rC"},D:{"641":"0 9 r s t u v w x y z AB BB CB DB EB FB GB HB IB JB KB LB MB NB OB I PC EC QC RC","900":"1 2 3 4 5 6 7 8 J PB K D E F A B C L M G N O P QB RB SB TB UB VB WB XB YB ZB aB bB cB dB eB fB gB hB iB jB kB lB mB nB oB pB qB rB sB tB uB vB MC wB NC xB yB zB 0B 1B 2B 3B 4B 5B 6B 7B 8B 9B AC BC CC DC Q H R S T U V W X Y Z a b c d e f g h i j k l m n o p q"},E:{"772":"A","900":"J PB K D E F B C L M G sC SC tC uC vC wC TC FC GC xC yC zC UC VC HC 0C IC WC XC YC ZC aC 1C JC bC cC dC eC fC 2C KC gC hC iC jC 3C"},F:{"16":"F 4C","129":"B C 5C 6C 7C FC kC 8C GC","641":"0 d e f g h i j k l m n o p q r s t u v w x y z","900":"1 2 3 4 5 6 7 8 G N O P QB RB SB TB UB VB WB XB YB ZB aB bB cB dB eB fB gB hB iB jB kB lB mB nB oB pB qB rB sB tB uB vB wB xB yB zB 0B 1B 2B 3B 4B 5B 6B 7B 8B 9B AC BC CC DC Q H R OC S T U V W X Y Z a b c"},G:{"900":"E SC 9C lC AD BD CD DD ED FD GD HD ID JD KD LD MD ND OD PD QD RD SD UC VC HC TD IC WC XC YC ZC aC UD JC bC cC dC eC fC VD KC gC hC iC jC"},H:{"129":"WD"},I:{"641":"I","900":"LC J XD YD ZD aD lC bD cD"},J:{"900":"D A"},K:{"129":"A B C FC kC GC","641":"H"},L:{"900":"I"},M:{"772":"EC"},N:{"388":"A B"},O:{"900":"HC"},P:{"641":"2 3 4 5 6 7 8","900":"1 J dD eD fD gD hD TC iD jD kD lD mD IC JC KC nD"},Q:{"900":"oD"},R:{"900":"pD"},S:{"772":"rD","900":"qD"}},B:2,C:"CSS page-break properties",D:true};

View File

@@ -0,0 +1,347 @@
/**
* @fileoverview A rule to suggest using template literals instead of string concatenation.
* @author Toru Nagashima
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("./utils/ast-utils");
//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
/**
* Checks whether or not a given node is a concatenation.
* @param {ASTNode} node A node to check.
* @returns {boolean} `true` if the node is a concatenation.
*/
function isConcatenation(node) {
return node.type === "BinaryExpression" && node.operator === "+";
}
/**
* Gets the top binary expression node for concatenation in parents of a given node.
* @param {ASTNode} node A node to get.
* @returns {ASTNode} the top binary expression node in parents of a given node.
*/
function getTopConcatBinaryExpression(node) {
let currentNode = node;
while (isConcatenation(currentNode.parent)) {
currentNode = currentNode.parent;
}
return currentNode;
}
/**
* Checks whether or not a node contains a string literal with an octal or non-octal decimal escape sequence
* @param {ASTNode} node A node to check
* @returns {boolean} `true` if at least one string literal within the node contains
* an octal or non-octal decimal escape sequence
*/
function hasOctalOrNonOctalDecimalEscapeSequence(node) {
if (isConcatenation(node)) {
return (
hasOctalOrNonOctalDecimalEscapeSequence(node.left) ||
hasOctalOrNonOctalDecimalEscapeSequence(node.right)
);
}
// No need to check TemplateLiterals would throw parsing error
if (node.type === "Literal" && typeof node.value === "string") {
return astUtils.hasOctalOrNonOctalDecimalEscapeSequence(node.raw);
}
return false;
}
/**
* Checks whether or not a given binary expression has string literals.
* @param {ASTNode} node A node to check.
* @returns {boolean} `true` if the node has string literals.
*/
function hasStringLiteral(node) {
if (isConcatenation(node)) {
// `left` is deeper than `right` normally.
return hasStringLiteral(node.right) || hasStringLiteral(node.left);
}
return astUtils.isStringLiteral(node);
}
/**
* Checks whether or not a given binary expression has non string literals.
* @param {ASTNode} node A node to check.
* @returns {boolean} `true` if the node has non string literals.
*/
function hasNonStringLiteral(node) {
if (isConcatenation(node)) {
// `left` is deeper than `right` normally.
return (
hasNonStringLiteral(node.right) || hasNonStringLiteral(node.left)
);
}
return !astUtils.isStringLiteral(node);
}
/**
* Determines whether a given node will start with a template curly expression (`${}`) when being converted to a template literal.
* @param {ASTNode} node The node that will be fixed to a template literal
* @returns {boolean} `true` if the node will start with a template curly.
*/
function startsWithTemplateCurly(node) {
if (node.type === "BinaryExpression") {
return startsWithTemplateCurly(node.left);
}
if (node.type === "TemplateLiteral") {
return (
node.expressions.length &&
node.quasis.length &&
node.quasis[0].range[0] === node.quasis[0].range[1]
);
}
return node.type !== "Literal" || typeof node.value !== "string";
}
/**
* Determines whether a given node end with a template curly expression (`${}`) when being converted to a template literal.
* @param {ASTNode} node The node that will be fixed to a template literal
* @returns {boolean} `true` if the node will end with a template curly.
*/
function endsWithTemplateCurly(node) {
if (node.type === "BinaryExpression") {
return startsWithTemplateCurly(node.right);
}
if (node.type === "TemplateLiteral") {
return (
node.expressions.length &&
node.quasis.length &&
node.quasis.at(-1).range[0] === node.quasis.at(-1).range[1]
);
}
return node.type !== "Literal" || typeof node.value !== "string";
}
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
/** @type {import('../shared/types').Rule} */
module.exports = {
meta: {
type: "suggestion",
docs: {
description:
"Require template literals instead of string concatenation",
recommended: false,
frozen: true,
url: "https://eslint.org/docs/latest/rules/prefer-template",
},
schema: [],
fixable: "code",
messages: {
unexpectedStringConcatenation: "Unexpected string concatenation.",
},
},
create(context) {
const sourceCode = context.sourceCode;
let done = Object.create(null);
/**
* Gets the non-token text between two nodes, ignoring any other tokens that appear between the two tokens.
* @param {ASTNode} node1 The first node
* @param {ASTNode} node2 The second node
* @returns {string} The text between the nodes, excluding other tokens
*/
function getTextBetween(node1, node2) {
const allTokens = [node1]
.concat(sourceCode.getTokensBetween(node1, node2))
.concat(node2);
const sourceText = sourceCode.getText();
return allTokens
.slice(0, -1)
.reduce(
(accumulator, token, index) =>
accumulator +
sourceText.slice(
token.range[1],
allTokens[index + 1].range[0],
),
"",
);
}
/**
* Returns a template literal form of the given node.
* @param {ASTNode} currentNode A node that should be converted to a template literal
* @param {string} textBeforeNode Text that should appear before the node
* @param {string} textAfterNode Text that should appear after the node
* @returns {string} A string form of this node, represented as a template literal
*/
function getTemplateLiteral(
currentNode,
textBeforeNode,
textAfterNode,
) {
if (
currentNode.type === "Literal" &&
typeof currentNode.value === "string"
) {
/*
* If the current node is a string literal, escape any instances of ${ or ` to prevent them from being interpreted
* as a template placeholder. However, if the code already contains a backslash before the ${ or `
* for some reason, don't add another backslash, because that would change the meaning of the code (it would cause
* an actual backslash character to appear before the dollar sign).
*/
return `\`${currentNode.raw
.slice(1, -1)
.replace(/\\*(\$\{|`)/gu, matched => {
if (matched.lastIndexOf("\\") % 2) {
return `\\${matched}`;
}
return matched;
// Unescape any quotes that appear in the original Literal that no longer need to be escaped.
})
.replace(
new RegExp(`\\\\${currentNode.raw[0]}`, "gu"),
currentNode.raw[0],
)}\``;
}
if (currentNode.type === "TemplateLiteral") {
return sourceCode.getText(currentNode);
}
if (isConcatenation(currentNode) && hasStringLiteral(currentNode)) {
const plusSign = sourceCode.getFirstTokenBetween(
currentNode.left,
currentNode.right,
token => token.value === "+",
);
const textBeforePlus = getTextBetween(
currentNode.left,
plusSign,
);
const textAfterPlus = getTextBetween(
plusSign,
currentNode.right,
);
const leftEndsWithCurly = endsWithTemplateCurly(
currentNode.left,
);
const rightStartsWithCurly = startsWithTemplateCurly(
currentNode.right,
);
if (leftEndsWithCurly) {
// If the left side of the expression ends with a template curly, add the extra text to the end of the curly bracket.
// `foo${bar}` /* comment */ + 'baz' --> `foo${bar /* comment */ }${baz}`
return (
getTemplateLiteral(
currentNode.left,
textBeforeNode,
textBeforePlus + textAfterPlus,
).slice(0, -1) +
getTemplateLiteral(
currentNode.right,
null,
textAfterNode,
).slice(1)
);
}
if (rightStartsWithCurly) {
// Otherwise, if the right side of the expression starts with a template curly, add the text there.
// 'foo' /* comment */ + `${bar}baz` --> `foo${ /* comment */ bar}baz`
return (
getTemplateLiteral(
currentNode.left,
textBeforeNode,
null,
).slice(0, -1) +
getTemplateLiteral(
currentNode.right,
textBeforePlus + textAfterPlus,
textAfterNode,
).slice(1)
);
}
/*
* Otherwise, these nodes should not be combined into a template curly, since there is nowhere to put
* the text between them.
*/
return `${getTemplateLiteral(currentNode.left, textBeforeNode, null)}${textBeforePlus}+${textAfterPlus}${getTemplateLiteral(currentNode.right, textAfterNode, null)}`;
}
return `\`\${${textBeforeNode || ""}${sourceCode.getText(currentNode)}${textAfterNode || ""}}\``;
}
/**
* Returns a fixer object that converts a non-string binary expression to a template literal
* @param {SourceCodeFixer} fixer The fixer object
* @param {ASTNode} node A node that should be converted to a template literal
* @returns {Object} A fix for this binary expression
*/
function fixNonStringBinaryExpression(fixer, node) {
const topBinaryExpr = getTopConcatBinaryExpression(node.parent);
if (hasOctalOrNonOctalDecimalEscapeSequence(topBinaryExpr)) {
return null;
}
return fixer.replaceText(
topBinaryExpr,
getTemplateLiteral(topBinaryExpr, null, null),
);
}
/**
* Reports if a given node is string concatenation with non string literals.
* @param {ASTNode} node A node to check.
* @returns {void}
*/
function checkForStringConcat(node) {
if (
!astUtils.isStringLiteral(node) ||
!isConcatenation(node.parent)
) {
return;
}
const topBinaryExpr = getTopConcatBinaryExpression(node.parent);
// Checks whether or not this node had been checked already.
if (done[topBinaryExpr.range[0]]) {
return;
}
done[topBinaryExpr.range[0]] = true;
if (hasNonStringLiteral(topBinaryExpr)) {
context.report({
node: topBinaryExpr,
messageId: "unexpectedStringConcatenation",
fix: fixer => fixNonStringBinaryExpression(fixer, node),
});
}
}
return {
Program() {
done = Object.create(null);
},
Literal: checkForStringConcat,
TemplateLiteral: checkForStringConcat,
};
},
};