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
+44
View File
@@ -0,0 +1,44 @@
/**
* 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.
*/
import { stripSlashes } from 'slashes';
import isConstantStringLiteral from './is-constant-string-literal';
import parseType from './parse-type';
import resolve from './resolve';
const parseModuleSpecifier = (moduleSpecifierString, {
isDynamicImport,
resolveFrom
}) => {
const {
isConstant,
value
} = !isDynamicImport || isConstantStringLiteral(moduleSpecifierString) ? {
isConstant: true,
value: stripSlashes(moduleSpecifierString.substring(1, moduleSpecifierString.length - 1))
} : {
isConstant: false,
value: undefined
};
return {
type: isConstant ? parseType(value) : `unknown`,
isConstant,
code: moduleSpecifierString,
value,
resolved: typeof resolveFrom === `string` && isConstant ? resolve(resolveFrom, value) : undefined
};
};
export default parseModuleSpecifier;
@@ -0,0 +1,39 @@
/**
* 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;
};
export default isConstantStringLiteral;
@@ -0,0 +1,39 @@
/**
* 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.
*/
import module from 'module';
const builtinModules = new Set(module.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`;
};
export default parseType;
@@ -0,0 +1,28 @@
/**
* 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.
*/
import { dirname } from 'path';
const resolve = (from, to) => {
try {
return require.resolve(to, {
paths: [dirname(from)]
});
} catch {
return undefined;
}
};
export default resolve;