This commit is contained in:
Tutur33
2023-11-24 22:35:41 +01:00
parent 3c0b507a93
commit 7644b2a0f7
45165 changed files with 4803356 additions and 3 deletions
+53
View File
@@ -0,0 +1,53 @@
/**
* 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
*
* http://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.
*/
/// <reference types="node"/>
declare namespace parseImports {
export type ModuleSpecifierType =
| 'invalid'
| 'absolute'
| 'relative'
| 'builtin'
| 'package'
| 'unknown'
export type Import = {
isDynamicImport: boolean
moduleSpecifier: {
type: ModuleSpecifierType
isConstant: boolean
code: string
value?: string
resolved?: string
}
importClause?: {
default?: string
named: { specifier: string; binding: string }[]
namespace?: string
}
}
export type Options = { readonly resolveFrom?: string }
}
declare const parseImports: {
(code: string, options?: parseImports.Options): Promise<
IterableIterator<parseImports.Import>
>
}
export = parseImports
+74
View File
@@ -0,0 +1,74 @@
"use strict";
exports.__esModule = true;
exports.default = void 0;
var _esModuleLexer = require("es-module-lexer");
var _parseModuleSpecifier = _interopRequireDefault(require("./parse-module-specifier"));
var _parseImportClause = _interopRequireDefault(require("./parse-import-clause"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* 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
*
* http://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.
*/
const parseImports = async (code, {
resolveFrom
} = {}) => {
const [imports] = await (0, _esModuleLexer.parse)(code, resolveFrom != null ? resolveFrom : undefined);
return function* () {
for (let {
d: dynamicImportStartIndex,
ss: statementStartIndex,
s: moduleSpecifierStartIndex,
e: moduleSpecifierEndIndexExclusive
} of imports) {
const isDynamicImport = dynamicImportStartIndex > -1; // Include string literal quotes in character range
if (!isDynamicImport) {
moduleSpecifierStartIndex--;
moduleSpecifierEndIndexExclusive++;
}
const moduleSpecifierString = code.substring(moduleSpecifierStartIndex, moduleSpecifierEndIndexExclusive);
const moduleSpecifier = (0, _parseModuleSpecifier.default)(moduleSpecifierString, {
isDynamicImport,
resolveFrom
});
let importClause;
if (!isDynamicImport) {
let importClauseString = code.substring(statementStartIndex + `import`.length, moduleSpecifierStartIndex).trim();
if (importClauseString.endsWith(`from`)) {
importClauseString = importClauseString.substring(0, importClauseString.length - `from`.length);
}
importClause = (0, _parseImportClause.default)(importClauseString);
}
yield {
isDynamicImport,
moduleSpecifier,
importClause
};
}
}();
};
var _default = parseImports;
exports.default = _default;
+72
View File
@@ -0,0 +1,72 @@
"use strict";
exports.__esModule = true;
exports.default = void 0;
var _skip = require("./skip");
var _parseNamedImports = _interopRequireDefault(require("./parse-named-imports"));
var _parseNamespaceImport = _interopRequireDefault(require("./parse-namespace-import"));
var _parseDefaultImport = _interopRequireDefault(require("./parse-default-import"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* 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
*
* http://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.
*/
// Assumes import clause is syntactically valid
const parseImportClause = importClauseString => {
let defaultImport;
let namespaceImport;
const namedImports = [];
for (let i = 0; i < importClauseString.length; i++) {
if (_skip.separatorRegex.test(importClauseString[i])) {
continue;
}
if (importClauseString[i] === `{`) {
let newNamedImports;
({
namedImports: newNamedImports,
i
} = (0, _parseNamedImports.default)(importClauseString, i));
namedImports.push(...newNamedImports);
} else if (importClauseString[i] === `*`) {
;
({
namespaceImport,
i
} = (0, _parseNamespaceImport.default)(importClauseString, i));
} else {
;
({
defaultImport,
i
} = (0, _parseDefaultImport.default)(importClauseString, i));
}
}
return {
default: defaultImport,
namespace: namespaceImport,
named: namedImports
};
};
var _default = parseImportClause;
exports.default = _default;
@@ -0,0 +1,33 @@
"use strict";
exports.__esModule = true;
exports.default = void 0;
var _skip = require("./skip");
/**
* 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
*
* http://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.
*/
const parseDefaultImport = (importClauseString, i) => {
const startIndex = i;
i = (0, _skip.skipNonSeparators)(importClauseString, i);
return {
defaultImport: importClauseString.substring(startIndex, i),
i
};
};
var _default = parseDefaultImport;
exports.default = _default;
@@ -0,0 +1,53 @@
"use strict";
exports.__esModule = true;
exports.default = void 0;
/**
* 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
*
* http://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.
*/
const parseNamedImports = (importClauseString, i) => {
const startIndex = ++i;
while (i < importClauseString.length && importClauseString[i] !== `}`) {
i++;
}
const namedImports = importClauseString.substring(startIndex, i++).split(`,`).map(namedImport => {
namedImport = namedImport.trim();
if (namedImport.includes(` `)) {
const components = namedImport.split(` `);
return {
specifier: components[0],
binding: components[components.length - 1]
};
}
return {
specifier: namedImport,
binding: namedImport
};
}).filter(({
specifier
}) => specifier.length > 0);
return {
namedImports,
i
};
};
var _default = parseNamedImports;
exports.default = _default;
@@ -0,0 +1,37 @@
"use strict";
exports.__esModule = true;
exports.default = void 0;
var _skip = require("./skip");
/**
* 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
*
* http://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.
*/
const parseNamespaceImport = (importClauseString, i) => {
i++;
i = (0, _skip.skipSeparators)(importClauseString, i);
i += `as`.length;
i = (0, _skip.skipSeparators)(importClauseString, i);
const startIndex = i;
i = (0, _skip.skipNonSeparators)(importClauseString, i);
return {
namespaceImport: importClauseString.substring(startIndex, i),
i
};
};
var _default = parseNamespaceImport;
exports.default = _default;
+42
View File
@@ -0,0 +1,42 @@
"use strict";
exports.__esModule = true;
exports.skipNonSeparators = exports.skipSeparators = exports.separatorRegex = void 0;
/**
* 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
*
* http://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.
*/
const separatorRegex = /^(?:\s+|,)$/u;
exports.separatorRegex = separatorRegex;
const skipSeparators = (imported, i) => {
while (i < imported.length && separatorRegex.test(imported[i])) {
i++;
}
return i;
};
exports.skipSeparators = skipSeparators;
const skipNonSeparators = (imported, i) => {
while (i < imported.length && !separatorRegex.test(imported[i])) {
i++;
}
return i;
};
exports.skipNonSeparators = skipNonSeparators;
+55
View File
@@ -0,0 +1,55 @@
"use strict";
exports.__esModule = true;
exports.default = void 0;
var _slashes = require("slashes");
var _isConstantStringLiteral = _interopRequireDefault(require("./is-constant-string-literal"));
var _parseType = _interopRequireDefault(require("./parse-type"));
var _resolve = _interopRequireDefault(require("./resolve"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* 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
*
* http://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.
*/
const parseModuleSpecifier = (moduleSpecifierString, {
isDynamicImport,
resolveFrom
}) => {
const {
isConstant,
value
} = !isDynamicImport || (0, _isConstantStringLiteral.default)(moduleSpecifierString) ? {
isConstant: true,
value: (0, _slashes.stripSlashes)(moduleSpecifierString.substring(1, moduleSpecifierString.length - 1))
} : {
isConstant: false,
value: undefined
};
return {
type: isConstant ? (0, _parseType.default)(value) : `unknown`,
isConstant,
code: moduleSpecifierString,
value,
resolved: typeof resolveFrom === `string` && isConstant ? (0, _resolve.default)(resolveFrom, value) : undefined
};
};
var _default = parseModuleSpecifier;
exports.default = _default;
@@ -0,0 +1,45 @@
"use strict";
exports.__esModule = true;
exports.default = void 0;
/**
* 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
*
* http://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.
*/
// Assumes the string is syntactically valid
const isConstantStringLiteral = stringLiteral => {
const quote = [`'`, `"`, `\``].find(quoteCandidate => stringLiteral.startsWith(quoteCandidate) && stringLiteral.endsWith(quoteCandidate));
if (quote == null) {
return false;
}
for (let i = 1; i < stringLiteral.length - 1; i++) {
// Check for end of string literal before end of stringLiteral
if (stringLiteral[i] === quote && stringLiteral[i - 1] !== `\\`) {
return false;
} // Check for interpolated value in template literal
if (quote === `\`` && stringLiteral.substring(i, i + 2) === `\${` && stringLiteral[i - 1] !== `\\`) {
return false;
}
}
return true;
};
var _default = isConstantStringLiteral;
exports.default = _default;
@@ -0,0 +1,48 @@
"use strict";
exports.__esModule = true;
exports.default = void 0;
var _module2 = _interopRequireDefault(require("module"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* 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
*
* http://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.
*/
const builtinModules = new Set(_module2.default.builtinModules);
const parseType = moduleSpecifier => {
if (moduleSpecifier.length === 0) {
return `invalid`;
}
if (moduleSpecifier.startsWith(`/`)) {
return `absolute`;
}
if (moduleSpecifier.startsWith(`.`)) {
return `relative`;
}
if (builtinModules.has(moduleSpecifier)) {
return `builtin`;
}
return `package`;
};
var _default = parseType;
exports.default = _default;
+34
View File
@@ -0,0 +1,34 @@
"use strict";
exports.__esModule = true;
exports.default = void 0;
var _path = require("path");
/**
* 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
*
* http://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.
*/
const resolve = (from, to) => {
try {
return require.resolve(to, {
paths: [(0, _path.dirname)(from)]
});
} catch {
return undefined;
}
};
var _default = resolve;
exports.default = _default;