Skip to content

Commit

Permalink
chore: apply automated lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
autofix-ci[bot] authored Jan 11, 2024
1 parent f662bdb commit 1784a75
Showing 1 changed file with 34 additions and 26 deletions.
60 changes: 34 additions & 26 deletions src/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,17 @@ const EXPORT_NAMED_DEFAULT_RE =
const TYPE_RE = /^\s*?type\s/;

export function findStaticImports(code: string): StaticImport[] {
return matchAll(ESM_STATIC_IMPORT_RE, code, { type: "static" });
return _filterStatement(
_tryGetLocations(code, "import"),
matchAll(ESM_STATIC_IMPORT_RE, code, { type: "static" }),
);
}

export function findDynamicImports(code: string): DynamicImport[] {
return matchAll(DYNAMIC_IMPORT_RE, code, { type: "dynamic" });
return _filterStatement(
_tryGetLocations(code, "import"),
matchAll(DYNAMIC_IMPORT_RE, code, { type: "dynamic" }),
);
}

export function findTypeImports(code: string): TypeImport[] {
Expand Down Expand Up @@ -211,7 +217,7 @@ export function findExports(code: string): ESMExport[] {
{
type: "namedDefault",
code,
}
},
).map((exp) => {
exp.name = "default";

Expand All @@ -238,17 +244,14 @@ export function findExports(code: string): ESMExport[] {
if (exports.length === 0) {
return [];
}
const exportLocations = _tryGetExportLocations(code);
const exportLocations = _tryGetLocations(code, "export");
if (exportLocations && exportLocations.length === 0) {
return [];
}

return (
exports
// Filter false positive export matches
.filter(
(exp) => !exportLocations || _isExportStatement(exportLocations, exp),
)
// Filter false positive export matches
_filterStatement(exportLocations, exports)
// Prevent multiple exports of same function, only keep latest iteration of signatures
.filter((exp, index, exports) => {
const nextExport = exports[index + 1];
Expand Down Expand Up @@ -287,17 +290,14 @@ export function findTypeExports(code: string): ESMExport[] {
if (exports.length === 0) {
return [];
}
const exportLocations = _tryGetExportLocations(code);
const exportLocations = _tryGetLocations(code, "export");
if (exportLocations && exportLocations.length === 0) {
return [];
}

return (
exports
// Filter false positive export matches
.filter(
(exp) => !exportLocations || _isExportStatement(exportLocations, exp),
)
// Filter false positive export matches
_filterStatement(exportLocations, exports)
// Prevent multiple exports of same function, only keep latest iteration of signatures
.filter((exp, index, exports) => {
const nextExport = exports[index + 1];
Expand Down Expand Up @@ -384,23 +384,31 @@ interface TokenLocation {
end: number;
}

function _isExportStatement(exportsLocation: TokenLocation[], exp: ESMExport) {
return exportsLocation.some((location) => {
// AST token inside the regex match
return exp.start <= location.start && exp.end >= location.end;
// AST Token start or end is within the regex match
// return (exp.start <= location.start && location.start <= exp.end) ||
// (exp.start <= location.end && location.end <= exp.end)
function _filterStatement<T extends TokenLocation>(
locations: TokenLocation[] | undefined,
statements: T[],
): T[] {
return statements.filter((exp) => {
return (
!locations ||
locations.some((location) => {
// AST token inside the regex match
return exp.start <= location.start && exp.end >= location.end;
// AST Token start or end is within the regex match
// return (exp.start <= location.start && location.start <= exp.end) ||
// (exp.start <= location.end && location.end <= exp.end)
})
);
});
}

function _tryGetExportLocations(code: string) {
function _tryGetLocations(code: string, label: string) {
try {
return _getExportLocations(code);
return _getLocations(code, label);
} catch {}
}

function _getExportLocations(code: string) {
function _getLocations(code: string, label: string) {
const tokens = tokenizer(code, {
ecmaVersion: "latest",
sourceType: "module",
Expand All @@ -410,7 +418,7 @@ function _getExportLocations(code: string) {
});
const locations: TokenLocation[] = [];
for (const token of tokens) {
if (token.type.label === "export") {
if (token.type.label === label) {
locations.push({
start: token.start,
end: token.end,
Expand Down

0 comments on commit 1784a75

Please sign in to comment.