One `namespace_use_declaration`'s part of phpShadowsDirname (#3298): its text with JS whitespace runs collapsed to one space, the space beside `\ { } ,` dropped, and trimmed. `use function A\b, C\d;` imports each listed function, a group `use A\{B, function c}` each `function` clause. True when one lands on the local name `dirname` (any letter case) from a name other than the global `dirname`: `Fo
(raw: &str)
| 1695 | /// name other than the global `dirname`: `Foo\dirname`, not `dirname` or |
| 1696 | /// `Foo\dirname as dn`. |
| 1697 | fn php_use_shadows_dirname(raw: &str) -> bool { |
| 1698 | // .replace(/\s+/g, ' ') |
| 1699 | let mut collapsed: Vec<char> = Vec::with_capacity(raw.len()); |
| 1700 | for c in raw.chars() { |
| 1701 | if !is_js_space(c) { |
| 1702 | collapsed.push(c); |
| 1703 | } else if collapsed.last() != Some(&' ') { |
| 1704 | collapsed.push(' '); |
| 1705 | } |
| 1706 | } |
| 1707 | // .replace(/\s*([\\{},])\s*/g, '$1'): with single spaces, each space |
| 1708 | // beside one of those characters goes. |
| 1709 | let special = |c: char| matches!(c, '\\' | '{' | '}' | ','); |
| 1710 | let mut text = String::with_capacity(raw.len()); |
| 1711 | for (i, &c) in collapsed.iter().enumerate() { |
| 1712 | let beside = (i > 0 && special(collapsed[i - 1])) |
| 1713 | || collapsed.get(i + 1).is_some_and(|&next| special(next)); |
| 1714 | if c == ' ' && beside { |
| 1715 | continue; |
| 1716 | } |
| 1717 | text.push(c); |
| 1718 | } |
| 1719 | // .trim() (only `' '` is left), then /^use (function )?(.*?);?$/i. |
| 1720 | let Some(rest) = strip_prefix_ascii_ci(text.trim_matches(' '), "use ") else { return false }; |
| 1721 | let (all_functions, rest) = match strip_prefix_ascii_ci(rest, "function ") { |
| 1722 | Some(after) => (true, after), |
| 1723 | None => (false, rest), |
| 1724 | }; |
| 1725 | let names = rest.strip_suffix(';').unwrap_or(rest); |
| 1726 | // /^([\w\\]*)\{(.*)\}$/ (JS ASCII `\w`) on the trimmed names. |
| 1727 | let trimmed = names.trim_matches(is_js_space); |
| 1728 | let head_len = trimmed |
| 1729 | .bytes() |
| 1730 | .take_while(|b| b.is_ascii_alphanumeric() || *b == b'_' || *b == b'\\') |
| 1731 | .count(); |
| 1732 | let group = trimmed[head_len..] |
| 1733 | .strip_prefix('{') |
| 1734 | .and_then(|body| body.strip_suffix('}')) |
| 1735 | .map(|body| (&trimmed[..head_len], body)); |
| 1736 | if !all_functions && group.is_none() { |
| 1737 | return false; |
| 1738 | } |
| 1739 | let prefix = group.map_or("", |(head, _)| head.strip_suffix('\\').unwrap_or(head)); |
| 1740 | let list = group.map_or(names, |(_, body)| body); |
| 1741 | for raw_clause in list.split(',') { |
| 1742 | let function_clause = strip_prefix_ascii_ci(raw_clause.trim_matches(is_js_space), "function "); |
| 1743 | if !all_functions && function_clause.is_none() { |
| 1744 | continue; |
| 1745 | } |
| 1746 | let clause = function_clause.unwrap_or(raw_clause).trim_matches(is_js_space); |
| 1747 | let mut pieces = php_use_alias_re().split(clause); |
| 1748 | let name = pieces.next().unwrap_or(""); |
| 1749 | let alias = pieces.next(); |
| 1750 | let joined = if prefix.is_empty() { name.to_string() } else { format!("{prefix}\\{name}") }; |
| 1751 | let fqn = joined.strip_prefix('\\').unwrap_or(&joined); |
| 1752 | let local = alias.unwrap_or_else(|| fqn.rsplit('\\').next().unwrap_or(fqn)); |
| 1753 | if local.to_lowercase() == "dirname" && fqn.to_lowercase() != "dirname" { |
| 1754 | return true; |
no test coverage detected