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

fix(dynamic-import-vars): escape special glob characters #1636

Merged
Show file tree
Hide file tree
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
@@ -1,15 +1,18 @@
import path from 'path';

import fastGlob from 'fast-glob';

export class VariableDynamicImportError extends Error {}

/* eslint-disable-next-line no-template-curly-in-string */
const example = 'For example: import(`./foo/${bar}.js`).';

function sanitizeString(str) {
if (str === '') return str;
if (str.includes('*')) {
throw new VariableDynamicImportError('A dynamic import cannot contain * characters.');
}
return str;
return fastGlob.escapePath(str);
}

function templateLiteralToGlob(node) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,21 @@ test('throws when dynamic import imports does not contain a file extension', (t)
);
t.true(error instanceof VariableDynamicImportError);
});

test('escapes ()', (t) => {
const ast = parse('import(`./${foo}/(foo).js`);', {
sourceType: 'module'
});

const glob = dynamicImportToGlob(ast.body[0].expression.source);
t.is(glob, './*/\\(foo\\).js');
});

test('escapes []', (t) => {
const ast = parse('import(`./${foo}/[foo].js`);', {
sourceType: 'module'
});

const glob = dynamicImportToGlob(ast.body[0].expression.source);
t.is(glob, './*/\\[foo\\].js');
});
Loading