From e4a350fcbf0dc430c2209cd4145d54fe26fcd345 Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Thu, 24 Sep 2026 13:05:46 -0500 Subject: [PATCH] fix(plugin): refuse names whose factory would not compile `delete`, `class` or `default` made `export function delete(...)`, which is not TypeScript. `create app` and `app` made a factory named after something the entry already binds, so the import broke src/app.ts, and the command still reported the wiring as a success. Both are now refused before anything is written. Signed-off-by: Diego Resendez --- src/cmd_plugin.rs | 24 +++++++++++--- src/naming.rs | 81 +++++++++++++++++++++++++++++++++++++++++++++++ tests/plugin.rs | 56 ++++++++++++++++++++++++++++++++ 3 files changed, 157 insertions(+), 4 deletions(-) diff --git a/src/cmd_plugin.rs b/src/cmd_plugin.rs index 59d9533..bbd86e6 100644 --- a/src/cmd_plugin.rs +++ b/src/cmd_plugin.rs @@ -50,11 +50,18 @@ fn resolve_slug( Some(n) => n, None => ask.text("Plugin name?", None, "the NAME argument")?, }; - naming::slug(&raw).ok_or_else(|| { + let slug = naming::slug(&raw).ok_or_else(|| { invalid(format!( "'{raw}' does not make a plugin name: start with a letter, and use letters, digits, spaces or hyphens" )) - }) + })?; + let fun = naming::camel(&slug); + if naming::is_reserved(&fun) { + return Err(invalid(format!( + "'{raw}' would make the factory `{fun}`, which is a reserved word in TypeScript; pick another name" + ))); + } + Ok(slug) } /// Relative, and never climbing out: `..` or an absolute path would put the @@ -85,6 +92,17 @@ fn in_app(opts: Opts, ask: &mut Ask, root: &Path) -> } let slug = resolve_slug(opts.name, ask)?; + let fun = naming::camel(&slug); + let src = std::fs::read_to_string(&entry_path)?; + // A second binding of a name the entry already has is a SyntaxError, and it + // would break the app rather than only the new plugin. + if naming::used_in(&src, &fun) { + return Err(invalid(format!( + "{} already uses `{fun}`, so the plugin's factory would collide with it; pick another name", + entry_path.strip_prefix(root).unwrap_or(&entry_path).display() + ))); + } + let dir = root.join(&folder).join(&slug); if dir.exists() { return Err(Error::new( @@ -102,11 +120,9 @@ fn in_app(opts: Opts, ask: &mut Ask, root: &Path) -> Path::new(&folder).join(&slug).join("index.ts").display() ); - let fun = naming::camel(&slug); let call = format!("{fun}()"); // Both entry candidates live in src/, so the plugin folder is one level up. let from = format!("../{folder}/{slug}/index"); - let src = std::fs::read_to_string(&entry_path)?; let imported = wire::add_import(&src, &fun, &from); match wire::add_to_createapp(&imported, "plugins", &call) { Some(wired) => { diff --git a/src/naming.rs b/src/naming.rs index 82dddcc..1a7212e 100644 --- a/src/naming.rs +++ b/src/naming.rs @@ -51,6 +51,75 @@ pub fn camel(slug: &str) -> String { first + &words.map(capitalised).collect::() } +/// Words that cannot name a function in a TypeScript module: JS keywords, the +/// strict-mode reserved words (modules are always strict) and the literals. +/// `camel` of a slug is lowercase-first, so only lowercase spellings matter. +const RESERVED: &[&str] = &[ + "arguments", + "await", + "break", + "case", + "catch", + "class", + "const", + "continue", + "debugger", + "default", + "delete", + "do", + "else", + "enum", + "eval", + "export", + "extends", + "false", + "finally", + "for", + "function", + "if", + "implements", + "import", + "in", + "instanceof", + "interface", + "let", + "new", + "null", + "package", + "private", + "protected", + "public", + "return", + "static", + "super", + "switch", + "this", + "throw", + "true", + "try", + "typeof", + "var", + "void", + "while", + "with", + "yield", +]; + +/// True when `ident` cannot be used as the factory's name. +pub fn is_reserved(ident: &str) -> bool { + RESERVED.contains(&ident) +} + +/// True when `ident` appears as a whole identifier anywhere in `src`. +/// +/// ponytail: a token scan, not a parse, so a match inside a comment or a string +/// also counts. That refuses a name that would have been fine, never the other +/// way round; a scope-aware check is the upgrade if it ever gets in the way. +pub fn used_in(src: &str, ident: &str) -> bool { + src.split(|c: char| !(c.is_ascii_alphanumeric() || c == '_' || c == '$')) + .any(|token| token == ident) +} + #[cfg(test)] mod tests { use super::*; @@ -75,6 +144,18 @@ mod tests { assert_eq!(slug("rate.limit"), None); } + #[test] + fn reserved_words_and_uses_are_found() { + assert!(is_reserved("delete")); + assert!(is_reserved("class")); + assert!(!is_reserved("pluginAlgo")); + let src = "import { createApp } from 'x';\nexport const app = createApp({});\n"; + assert!(used_in(src, "createApp")); + assert!(used_in(src, "app")); + assert!(!used_in(src, "appModule")); + assert!(!used_in(src, "create")); + } + #[test] fn identifiers_follow_the_slug() { assert_eq!(camel("plugin-algo"), "pluginAlgo"); diff --git a/tests/plugin.rs b/tests/plugin.rs index e26c726..645399d 100644 --- a/tests/plugin.rs +++ b/tests/plugin.rs @@ -353,3 +353,59 @@ fn package_rejects_a_bad_scope_or_npm_name() { .failure() .stderr(contains("npm name")); } + +#[test] +fn a_name_whose_factory_would_be_a_reserved_word_writes_nothing() { + for name in ["delete", "class", "default", "new", "import"] { + let d = project(APP); + matcha(d.path()) + .args(["create", "plugin", name]) + .assert() + .failure() + .stderr(contains("reserved word")); + assert!(!d.path().join("plugins").exists(), "{name}"); + assert_eq!( + std::fs::read_to_string(d.path().join("src/app.ts")).unwrap(), + APP + ); + } +} + +#[test] +fn a_package_whose_factory_would_be_a_reserved_word_writes_nothing() { + let d = tempdir().unwrap(); + matcha(d.path()) + .args([ + "create", + "plugin", + "delete", + "--package", + "p", + "--scope", + "acme", + ]) + .assert() + .failure() + .stderr(contains("reserved word")); + assert!(!d.path().join("p").exists()); +} + +/// `create app` → `createApp`, and `app` → `app`: both already exist in the entry, and a second +/// binding of the same name is a SyntaxError that would break the app, not just the plugin. +#[test] +fn in_app_refuses_a_factory_name_the_entry_already_uses() { + for name in ["create app", "app"] { + let d = project(APP); + matcha(d.path()) + .args(["create", "plugin", name]) + .assert() + .failure() + .stderr(contains("already")); + assert!(!d.path().join("plugins").exists(), "{name}"); + assert_eq!( + std::fs::read_to_string(d.path().join("src/app.ts")).unwrap(), + APP, + "{name}" + ); + } +}