Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(android): allow and escape Kotlin keyword as package identifier #328

Merged
merged 23 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
87535ab
fix(android): allows and escape kotlin only keyword as identifier
pewsheen May 16, 2024
94dfd9a
fix: add android_identifier_escape_kotlin_keyword method
pewsheen May 16, 2024
e6db09b
chore: add changelog
pewsheen May 16, 2024
c02ff92
mod must be public to be used in tauri
lucasfernog May 23, 2024
7ac11a0
Fix build errors for iOS (#272)
hampuslidin May 23, 2024
b230e0a
refactor!: use config.domain as package name and bundle id (#330)
pewsheen May 24, 2024
be55f47
fix(deps): update rust crate textwrap to v0.16.1 (#329)
renovate[bot] May 24, 2024
4c76db1
fix(deps): update rust crate os_pipe to v1.1.5 (#325)
renovate[bot] May 24, 2024
d29629e
fix(deps): update rust crate libc to v0.2.155 (#324)
renovate[bot] May 24, 2024
5d932c4
fix(deps): update rust crate ignore to v0.4.22 (#323)
renovate[bot] May 24, 2024
55983b3
fix(deps): update rust crate deunicode to v1.6.0 (#321)
renovate[bot] May 24, 2024
49033d3
fix(deps): update rust crate core-foundation to v0.9.4 (#320)
renovate[bot] May 24, 2024
518f539
chore(deps): update rust crate serde to v1.0.202 (#311)
renovate[bot] May 24, 2024
0db5c92
chore(deps): update rust crate embed-resource to v2.4.2 (#317)
renovate[bot] May 24, 2024
043e74c
chore(deps): update rust crate serde_json to v1.0.117 (#318)
renovate[bot] May 24, 2024
c530bbc
chore(deps): update rust crate thiserror to v1.0.61 (#319)
renovate[bot] May 24, 2024
41b4147
fix(deps): update rust crate duct to v0.13.7 (#322)
renovate[bot] May 24, 2024
9d6ef3a
fix(deps): update rust crate toml to v0.8.13 (#332)
renovate[bot] May 24, 2024
ffab8bb
fix(deps): update rust crate ureq to v2.9.7 (#333)
renovate[bot] May 24, 2024
e798b07
publish new versions (#301)
github-actions[bot] May 24, 2024
79ab4fc
Merge branch 'dev' into fix/escape-kotlin-keyword
pewsheen May 27, 2024
6c1fcd4
fix: add `true` `false` `null` to java keywords
pewsheen May 27, 2024
d0dc86a
fix: wry template package name should reverse
pewsheen May 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/android-kotlin-keyword-as-ident.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"cargo-mobile2": "patch"
---

On Android, allows using Kotlin keywords as identifiers and escape them in templates.
15 changes: 3 additions & 12 deletions src/config/app/identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use std::error::Error;
use std::fmt;

static RESERVED_PACKAGE_NAMES: [&str; 2] = ["kotlin", "java"];
static RESERVED_KEYWORDS: [&str; 63] = [
// https://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html
static RESERVED_JAVA_KEYWORDS: [&str; 53] = [
"abstract",
"as",
"assert",
"boolean",
"break",
Expand All @@ -27,21 +27,17 @@ static RESERVED_KEYWORDS: [&str; 63] = [
"finally",
"float",
"for",
"fun",
"goto",
"if",
"implements",
"import",
"instanceof",
"in",
"int",
"interface",
"is",
"long",
"native",
"new",
"null",
"object",
"package",
"private",
"protected",
Expand All @@ -59,13 +55,8 @@ static RESERVED_KEYWORDS: [&str; 63] = [
"transient",
"true",
"try",
"typealias",
"typeof",
"val",
"var",
"void",
"volatile",
"when",
"while",
];

Expand Down Expand Up @@ -129,7 +120,7 @@ pub fn check_identifier_syntax(identifier_name: &str) -> Result<(), IdentifierEr
if label.is_empty() {
return Err(IdentifierError::EmptyLabel);
}
if RESERVED_KEYWORDS.contains(&label) {
if RESERVED_JAVA_KEYWORDS.contains(&label) {
return Err(IdentifierError::ReservedKeyword {
keyword: label.to_owned(),
});
Expand Down
14 changes: 14 additions & 0 deletions src/config/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,20 @@ impl App {
.join(".")
}

pub fn android_identifier_escape_kotlin_keyword(&self) -> String {
self.reverse_identifier()
.split('.')
.map(|s| {
if crate::reserved_names::KOTLIN_ONLY_KEYWORDS.contains(&s) {
format!("`{}`", s)
} else {
s.to_string()
}
})
.collect::<Vec<_>>()
.join(".")
}

pub fn manifest_path(&self) -> PathBuf {
self.root_dir().join("Cargo.toml")
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub mod init;
pub mod opts;
pub mod os;
mod project;
mod reserved_names;
pub mod reserved_names;
pub mod target;
mod templating;
pub mod update;
Expand Down
13 changes: 13 additions & 0 deletions src/reserved_names/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ pub static WINDOWS: &[&str] = &[

pub static ARTIFACTS: &[&str] = &["deps", "examples", "build", "incremental"];

pub static KOTLIN_ONLY_KEYWORDS: &[&str] = &[
"as",
"fun",
"in",
"is",
"object",
"typealias",
"typeof",
"val",
"var",
"when",
];

pub fn in_keywords(s: impl AsRef<str>) -> bool {
KEYWORDS.contains(&s.as_ref())
}
Expand Down
24 changes: 24 additions & 0 deletions src/templating/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
Bicycle, EscapeFn, HelperDef, JsonMap,
},
config::{app, Config},
reserved_names::KOTLIN_ONLY_KEYWORDS,
util::{self, Git},
};
use std::collections::HashMap;
Expand Down Expand Up @@ -122,6 +123,28 @@ fn reverse_domain_snake_case(
.map_err(Into::into)
}

fn escape_kotlin_keyword(
helper: &Helper,
_: &Handlebars,
_: &Context,
_: &mut RenderContext,
out: &mut dyn Output,
) -> HelperResult {
let escaped_result = get_str(helper)
.split('.')
.map(|s| {
if KOTLIN_ONLY_KEYWORDS.contains(&s) {
format!("`{}`", s)
} else {
s.to_string()
}
})
.collect::<Vec<_>>()
.join(".");

out.write(&escaped_result).map_err(Into::into)
}

fn app_root(ctx: &Context) -> Result<&str, RenderErrorReason> {
let app_root = ctx
.data()
Expand Down Expand Up @@ -219,6 +242,7 @@ pub fn init(config: Option<&Config>) -> Bicycle {
"reverse-domain-snake-case",
Box::new(reverse_domain_snake_case),
);
helpers.insert("escape-kotlin-keyword", Box::new(escape_kotlin_keyword));
helpers.insert("dot-to-slash", Box::new(dot_to_slash));
if config.is_some() {
// don't mix these up or very bad things will happen to all of us
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package {{reverse-domain app.identifier}}
package {{escape-kotlin-keyword (reverse-domain app.identifier)}}

class MainActivity : WryActivity()
Loading