release: 26.9.0-beta.1 — create plugin, core 26.9.0-beta.2 #15

Merged
chrnx merged 12 commits from develop into main 2026-09-24 19:41:37 +00:00
3 changed files with 157 additions and 4 deletions
Showing only changes of commit e4a350fcbf - Show all commits

No files matched your search

+20 -4
View File
@@ -50,11 +50,18 @@ fn resolve_slug<R: BufRead, W: Write>(
Some(n) => n, Some(n) => n,
None => ask.text("Plugin name?", None, "the NAME argument")?, 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!( invalid(format!(
"'{raw}' does not make a plugin name: start with a letter, and use letters, digits, spaces or hyphens" "'{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 /// 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 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); let dir = root.join(&folder).join(&slug);
if dir.exists() { if dir.exists() {
return Err(Error::new( 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() Path::new(&folder).join(&slug).join("index.ts").display()
); );
let fun = naming::camel(&slug);
let call = format!("{fun}()"); let call = format!("{fun}()");
// Both entry candidates live in src/, so the plugin folder is one level up. // Both entry candidates live in src/, so the plugin folder is one level up.
let from = format!("../{folder}/{slug}/index"); let from = format!("../{folder}/{slug}/index");
let src = std::fs::read_to_string(&entry_path)?;
let imported = wire::add_import(&src, &fun, &from); let imported = wire::add_import(&src, &fun, &from);
match wire::add_to_createapp(&imported, "plugins", &call) { match wire::add_to_createapp(&imported, "plugins", &call) {
Some(wired) => { Some(wired) => {
+81
View File
@@ -51,6 +51,75 @@ pub fn camel(slug: &str) -> String {
first + &words.map(capitalised).collect::<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)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@@ -75,6 +144,18 @@ mod tests {
assert_eq!(slug("rate.limit"), None); 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] #[test]
fn identifiers_follow_the_slug() { fn identifiers_follow_the_slug() {
assert_eq!(camel("plugin-algo"), "pluginAlgo"); assert_eq!(camel("plugin-algo"), "pluginAlgo");
+56
View File
@@ -353,3 +353,59 @@ fn package_rejects_a_bad_scope_or_npm_name() {
.failure() .failure()
.stderr(contains("npm name")); .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}"
);
}
}