Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small refactor of extractImportReferences #17

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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