-
-
Notifications
You must be signed in to change notification settings - Fork 47
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
Return decoded specifiers and identifiers #61
Comments
Thanks, yes the API of this project could be more polished to avoid these footguns. It was actually originally written for https://github.com/guybedford/es-module-shims hence why it is entirely offset based, with a heavy focus on the overall library footprint. I've added a new section to the readme in https://github.com/guybedford/es-module-lexer#escape-sequences, I think that should cover the concerns, but further clarifications welcome. |
That workaround had occurred to me too. However, it doesn't work for a few reasons:
|
The
Both of the examples support
Because of the first point, this case works out fine too. |
Ah, of course this doesn't, will put some thought to this one. |
Here's my test: import lexer from 'es-module-lexer';
function tryCatch(fn) {
console.log(`\n${fn}:`);
try {
return fn();
} catch (err) {
return err;
}
}
let js, imp;
js = `import 'abc.js';`;
[[imp]] = await lexer.parse(js);
console.log(tryCatch(() => JSON.parse(js.substring(imp.s, imp.e))));
console.log(tryCatch(() => JSON.parse('"' + js.substring(imp.s, imp.e) + '"')));
js = `import './\\x61\\x62\\x63.js';`;
[[imp]] = await lexer.parse(js);
console.log(tryCatch(() => JSON.parse('"' + js.substring(imp.s, imp.e) + '"')));
js = `import './\\u{20204}.js';`;
[[imp]] = await lexer.parse(js);
console.log(tryCatch(() => JSON.parse('"' + js.substring(imp.s, imp.e) + '"'))); I'm running with node v15.2.0 and es-module-lexer v0.3.26 from npm. This is the output:
Edit: I think indirect import lexer from 'es-module-lexer';
function tryCatch(fn) {
console.log(`\n${fn}:`);
try {
return fn();
} catch (err) {
return err;
}
}
let js, imp;
js = `import 'abc.js';`;
[[imp]] = await lexer.parse(js);
console.log(tryCatch(() => (0, eval)(js.substring(imp.s - 1, imp.e + 1))));
js = `import './\\x61\\x62\\x63.js';`;
[[imp]] = await lexer.parse(js);
console.log(tryCatch(() => (0, eval)(js.substring(imp.s - 1, imp.e + 1))));
js = `import './\\u{20204}.js';`;
[[imp]] = await lexer.parse(js);
console.log(tryCatch(() => (0, eval)(js.substring(imp.s - 1, imp.e + 1))));
js = `import './".js';`;
[[imp]] = await lexer.parse(js);
console.log(tryCatch(() => (0, eval)(js.substring(imp.s - 1, imp.e + 1)))); The output:
|
Ok, I've put together a PR in #62. Let me know if that seems like it can work here. |
Yeah that approach seems good to me. |
Released in 0.4.0. |
I can confirm the fix. Looks good! |
Context: vitejs/vite#2083. Import paths are strings and can therefore contain escape sequences, like this:
Right now the documentation for this module seems to encourage using this library in a way that doesn't handle any escape sequences. Taking a raw slice of the input means that the backslashes are literal backslashes in the path instead of the actual underlying escape sequences. So in other words you would get the string
"./\\u0442\\u0435\\u0441\\u0442"
instead of the string"./тест"
and then fail to import the path.This can of course be worked around by interpreting the escape sequences yourself. But I thought I'd file this issue because it could be nice for either this library to do that for you or for there to be a recommended way of doing this, since one reason to use this library is to extract the import paths. If that's out of scope, maybe just calling out that you need to be sure to handle escape sequences yourself when you use this library with a link to the spec?
The text was updated successfully, but these errors were encountered: