Skip to content

Commit

Permalink
Small refactor of extractImportReferences
Browse files Browse the repository at this point in the history
  • Loading branch information
kobelb committed Feb 22, 2021
1 parent bfff33f commit 558c404
Showing 1 changed file with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,10 @@ export function extractImportReferences(

const ref = extractImportRef(textSegment);
if (ref) {
const index = textSegment.indexOf('import("');
const { name, path, index, length } = ref;
if (index !== 0) {
texts.push(textSegment.substr(0, index));
}
const { name, path } = ref;
const lengthOfImport = 'import(".")'.length + path.length + name.length;
const plugin = getPluginForPath(path, plugins);

if (!plugin) {
Expand All @@ -53,7 +51,7 @@ export function extractImportReferences(
}
// If we can't create a link for this, still remove the import("..."). part to make
// it easier to read.
const str = textSegment.substr(index + lengthOfImport - name.length, name.length);
const str = textSegment.substr(index + length - name.length, name.length);
if (str && str !== '') {
texts.push(str);
}
Expand All @@ -71,7 +69,7 @@ export function extractImportReferences(
text: name,
});
}
textSegment = textSegment.substr(index + lengthOfImport);
textSegment = textSegment.substr(index + length);
} else {
if (textSegment && textSegment !== '') {
texts.push(textSegment);
Expand All @@ -82,12 +80,16 @@ export function extractImportReferences(
return texts;
}

function extractImportRef(str: string): { path: string; name: string } | undefined {
function extractImportRef(
str: string
): { path: string; name: string; index: number; length: number } | undefined {
const groups = str.match(/import\("(.*?)"\)\.(\w*)/);
if (groups) {
const path = groups[1];
const name = groups[2];
return { path, name };
const index = groups.index!;
const length = groups[0].length;
return { path, name, index, length };
}
}

Expand Down

0 comments on commit 558c404

Please sign in to comment.