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 <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-09-24 13:05:46 -05:00
1 parent 70b51e5de4
commit e4a350fcbf
3 files changed
+157 -4

No files matched your search

+20 -4
View File
@@ -50,11 +50,18 @@ fn resolve_slug<R: BufRead, W: Write>(
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<R: BufRead, W: Write>(opts: Opts, ask: &mut Ask<R, W>, 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<R: BufRead, W: Write>(opts: Opts, ask: &mut Ask<R, W>, 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) => {
+81
View File
@@ -51,6 +51,75 @@ pub fn camel(slug: &str) -> String {
first + &words.map(capitalised).collect::<String>()
}
/// 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");