Skip to content

Commit

Permalink
fix: make sure namespace name is valid identifier (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
underfin authored Oct 26, 2023
1 parent 9942a77 commit d3c81aa
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ temp
rollup
**/.git
**/node_modules

scripts
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,50 @@ input_file: crates/rolldown/tests/esbuild/import_star/import_namespace_undefined

```js
// foo/no-side-effects.cjs
var require_no-side-effects = __commonJS({
var require_no_side_effects = __commonJS({
'foo/no-side-effects.cjs'(exports, module) {
console.log('cjs')
}
});
// foo/no-side-effects.mjs
var no-side-effects_ns$1 = {
var no_side_effects_ns$1 = {
};
console.log('mjs')
// foo/no-side-effects.js
var no-side-effects_ns = {
var no_side_effects_ns = {
};
console.log('js')
export { no-side-effects_ns as no-side-effects_ns$2, no-side-effects_ns$1 as no-side-effects_ns, no-side-effects_ns$2 as no-side-effects_ns$1 };
export { no_side_effects_ns as no_side_effects_ns$2, no_side_effects_ns$1 as no_side_effects_ns, no_side_effects_ns$2 as no_side_effects_ns$1 };
```
# entry-default_js.js

```js
import { no-side-effects_ns as no-side-effects_ns$2, no-side-effects_ns$1 as no-side-effects_ns, no-side-effects_ns$2 as no-side-effects_ns$1 } from "./2.js";
import { no_side_effects_ns as no_side_effects_ns$2, no_side_effects_ns$1 as no_side_effects_ns, no_side_effects_ns$2 as no_side_effects_ns$1 } from "./2.js";
// entry-default.js
var no-side-effects_ns$2 = __toESM(require_no-side-effects());
var no_side_effects_ns$2 = __toESM(require_no_side_effects());
console.log(
no-side-effects_ns.default,
no-side-effects_ns$1.default,
no-side-effects_ns$2.default,
no_side_effects_ns.default,
no_side_effects_ns$1.default,
no_side_effects_ns$2.default,
)
```
# entry-nope_js.js

```js
import { no-side-effects_ns as no-side-effects_ns$2, no-side-effects_ns$1 as no-side-effects_ns, no-side-effects_ns$2 as no-side-effects_ns$1 } from "./2.js";
import { no_side_effects_ns as no_side_effects_ns$2, no_side_effects_ns$1 as no_side_effects_ns, no_side_effects_ns$2 as no_side_effects_ns$1 } from "./2.js";
// entry-nope.js
var no-side-effects_ns$2 = __toESM(require_no-side-effects());
var no_side_effects_ns$2 = __toESM(require_no_side_effects());
console.log(
no-side-effects_ns.nope,
no-side-effects_ns$1.nope,
no-side-effects_ns$2.nope,
no_side_effects_ns.nope,
no_side_effects_ns$1.nope,
no_side_effects_ns$2.nope,
)
```
23 changes: 21 additions & 2 deletions crates/rolldown_common/src/module_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ impl ResourceId {
return [unique_name_of_parent_dir, "_index"].concat();
}
}
// TODO: ensure valid identifier
return unique_name.to_string();
return ensure_valid_identifier(unique_name);
}
}

Expand All @@ -72,3 +71,23 @@ impl AsRef<str> for ResourceId {
self.0.path.as_str()
}
}

fn ensure_valid_identifier(s: &str) -> String {
let mut ident = String::new();
let mut need_gap = false;
for i in s.chars() {
if i.is_ascii_alphabetic() || (i.is_ascii_digit() && !ident.is_empty()) {
if need_gap {
ident.push('_');
need_gap = false;
}
ident.push(i);
} else if !ident.is_empty() {
need_gap = true;
}
}
if ident.is_empty() {
ident.push('_');
}
ident
}

0 comments on commit d3c81aa

Please sign in to comment.