Skip to content

Commit

Permalink
fix(dynamic-import-vars): escape special glob characters (#1636)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Nov 25, 2023
1 parent 2c58b01 commit 944e7d3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/dynamic-import-vars/src/dynamic-import-to-glob.js
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');
});

0 comments on commit 944e7d3

Please sign in to comment.