Every name a statement of the file exports after it was declared: the one `export default NAME` names, and each word between the braces of an `export { … }`. Read once for the file. Asking the source again for each name, with a pattern compiled for it, cost a file of twenty thousand unexported constants a second for every megabyte, and four times that for each doubling.
(&self)
| 711 | /// unexported constants a second for every megabyte, and four times that |
| 712 | /// for each doubling. |
| 713 | fn later_exports(&self) -> &HashSet<&'t str> { |
| 714 | self.later_exports.get_or_init(|| { |
| 715 | // One statement, tried where a line starts. `R` (CRLF mode): JS's |
| 716 | // multiline `$` treats `\r` as a line end as well as `\n`; `(?m)` |
| 717 | // alone sees only `\n`, and would miss `export default NAME;\r\n` |
| 718 | // on every Windows autocrlf checkout. ASCII classes, not `\w`: this |
| 719 | // crate's `\w` is Unicode, JS's is not. |
| 720 | static STATEMENT: std::sync::OnceLock<regex::Regex> = std::sync::OnceLock::new(); |
| 721 | let statement = STATEMENT.get_or_init(|| { |
| 722 | regex::Regex::new( |
| 723 | r"\A[ \t]*export\s+(?:default\s+([A-Za-z_$][0-9A-Za-z_$]*)\s*;?[ \t]*(?mR:$)|\{([^}]*)\})", |
| 724 | ) |
| 725 | .expect("the later-export pattern compiles") |
| 726 | }); |
| 727 | let src: &'t str = self.src; |
| 728 | let bytes = src.as_bytes(); |
| 729 | let mut names: HashSet<&'t str> = HashSet::new(); |
| 730 | let mut line = 0usize; |
| 731 | loop { |
| 732 | // Only a line that opens with `export` can hold one. |
| 733 | let mut word = line; |
| 734 | while word < bytes.len() && (bytes[word] == b' ' || bytes[word] == b'\t') { |
| 735 | word += 1; |
| 736 | } |
| 737 | if bytes[word..].starts_with(b"export") { |
| 738 | if let Some(found) = statement.captures(&src[line..]) { |
| 739 | if let Some(default) = found.get(1) { |
| 740 | names.insert(&src[line + default.start()..line + default.end()]); |
| 741 | } else if let Some(braces) = found.get(2) { |
| 742 | // Inside `{ … }` a name is bounded by a character that |
| 743 | // can't continue an identifier, not by `\b`, which |
| 744 | // takes a `$` for a separator. |
| 745 | let from = line + braces.start(); |
| 746 | let inner = &src[from..line + braces.end()]; |
| 747 | let mut start: Option<usize> = None; |
| 748 | for (at, b) in inner.bytes().enumerate() { |
| 749 | let part = b.is_ascii_alphanumeric() || b == b'_' || b == b'$'; |
| 750 | match (part, start) { |
| 751 | (true, None) => start = Some(at), |
| 752 | (false, Some(s)) => { |
| 753 | names.insert(&src[from + s..from + at]); |
| 754 | start = None; |
| 755 | } |
| 756 | _ => {} |
| 757 | } |
| 758 | } |
| 759 | if let Some(s) = start { |
| 760 | names.insert(&src[from + s..from + inner.len()]); |
| 761 | } |
| 762 | } |
| 763 | } |
| 764 | } |
| 765 | // The next line starts after a `\n`, or after a `\r` no `\n` follows. |
| 766 | let mut next = word; |
| 767 | while next < bytes.len() && bytes[next] != b'\n' && bytes[next] != b'\r' { |
| 768 | next += 1; |
| 769 | } |
| 770 | if next >= bytes.len() { |
no test coverage detected