Repository navigation
Conversation
…wc paths via lib.rs PR #17 introduced scraper HTML parsing + swc_ecma_parser AST walking as the replacement for the regex-based resource extraction, but the diff had three rust-standards violations that block reviewer approval: 1. Four Selector::parse(...).unwrap() calls (R11.4 - no unwrap in production). The selector strings are compile-time constants, so the panic is theoretical, but rust-standards bans unwrap/expect/panic unconditionally and forbids #[allow] macros for silencing lint. Fix: lazy-init a OnceLock<Result<[Selector; 4], String>> in cache/static.rs, surface the (impossible) parse error via euv_log! + early return. 2. fn.rs hardcoded byte/string literals (R1.3c literal purity). Move CSS selector strings (script[src], link[href], img[src], script:not([src])), URL scheme prefixes (http://, https://, data:, //, ://), and the 16 resource-extension suffixes (.wasm, .js, .mjs, .css, ...) to const.rs. Replace the long if/else chain in is_resource_path() with a RESOURCE_EXTENSIONS.iter().any(|s| value.ends_with(*s)) loop. 3. swc_ecma_ast::Expr / Lit / Callee / MemberProp / etc. long-path calls in sub-file fn.rs bodies (R6.4-pitfall-b). The sub-file already has use super::*; and the lib.rs already re-exports swc_ecma_ast::{EsVersion, Program} - extend that block to also expose ArrowFunctionBody, Callee, Decl, Expr, Lit, MemberProp, ModuleDecl, ModuleItem, Prop, PropOrSpread, Stmt, VarDeclOrExpr, and the corresponding swc_common::FileName + scraper::selector::SelectorErrorKind. Replace every swc_ecma_ast::xxx reference with the short name (Expr, Stmt, ...). Resulting audit: 19/19 PASS.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
refactor(cache): bring PR #17 diff into rust-standards compliance
PR #17 (
refactor(cache): replace regex resource extraction with HTML parser + JS AST) replaced the fragile regex-based resource discovery withscraper::Html+swc_ecma_parser, but the 870-line diff introduced three categories of rust-standards violations that would block reviewer approval:1.
R11.4— fourSelector::parse(...).unwrap()calls inextract_resources_with_scraperSelector::parse("script[src]").unwrap()x 4 selectorsunwrap/expect/panic!in production unconditionally and forbids#[allow]-macro lint silencing.OnceLock<Result<[Selector; 4], String>>incache/static.rsviaRESOURCE_SELECTORS. Theinit_resource_selectorshelper returnsResultandSelector::parse(...)errors are converted viamap_err(|e: SelectorErrorKind| e.to_string())+?. The caller matches onResultand early-returns witheuv_log!if initialization fails.2.
R1.3c— hardcoded byte/string literals infn.rs"script[src]","link[href]","img[src]","script:not([src])""http://","https://","data:","//","://".wasm,.js,.mjs,.css,.json,.png,.jpg,.jpeg,.gif,.svg,.ico,.woff,.woff2,.ttf,.otf,.webpcache/const.rsaspub(crate) const. The 16 extensions become a singleRESOURCE_EXTENSIONS: &[&str]slice, andis_resource_path()becomes:if !src.starts_with("http://") && !src.starts_with("https://") && !src.starts_with("//") && !src.starts_with("data:") && !paths.contains(...)blocks collapse into a singleis_external_resource_url(url: &str) -> boolhelper usingHTTP_PREFIX/HTTPS_PREFIX/SCHEME_RELATIVE/DATA_PREFIXconstants.3.
R6.4-pitfall-b—swc_ecma_ast::Expr/Lit/Callee/MemberProp/ ... long-path calls in sub-filefn.rsfn.rsalready hasuse super::*;andlib.rsalready re-exportsswc_ecma_ast::{EsVersion, Program}— but the newextract_resources_from_expr/_stmt/_js_ast/is_resource_pathbody uses 50+ long-pathswc_ecma_ast::Variantpatterns that should have been brought into the re-export block.lib.rspub(crate) use swc_ecma_ast::{...}to also exposeArrowFunctionBody, Callee, Decl, Expr, Lit, MemberProp, ModuleDecl, ModuleItem, Prop, PropOrSpread, Stmt, VarDeclOrExpr, and the correspondingswc_common::FileName+scraper::selector::SelectorErrorKind. Replace everyswc_ecma_ast::xxxreference infn.rswith the short name.Verification
cargo fmt --allis idempotent (no diff after second run).Files changed (4)
src-tauri/src/cache/const.rs— +35 lines (10 new constants + 1 extension slice + doc comments)src-tauri/src/cache/fn.rs— refactor: 4unwrap->?+ match, magic literals -> const refs, 50swc_ecma_ast::X-> short namessrc-tauri/src/cache/static.rs— +9 lines (newRESOURCE_SELECTORSOnceLock)src-tauri/src/lib.rs— extendpub(crate) useblocks withSelectorExprKind,FileName, and 10 newswc_ecma_astvariantsScope discipline
This PR is strictly a refactor of the PR #17 diff itself; no behavior changes. The 4 selectors compile to the same
Selectorinstances, the 16 extension suffixes produce identical matches, and the AST walk visits the sameExpr/Stmtvariants. The only behavioral differences are:<script src>/<link href>/<img src>/ inline-<script>extraction instead of panicking. (In practice this is unreachable because the selector strings are compile-time constants.)Test surface
extract_resources_with_scraper/extract_resources_from_js_ast/is_resource_pathare private. End-to-end validation requires a Tauri build, which happens via the existingBuild APKworkflow on merge.