Skip to content

Commit

Permalink
Avoid demo parameter collisions in function definitions (#717)
Browse files Browse the repository at this point in the history
* Adding code for detecting collisions in parameter names

* Generation

* Move logic into append_out_param

* Use parameter name instead of terminusArgs

* Generation

* Formatting

* Useless comment

* Formatting
  • Loading branch information
ambiguousname authored Oct 29, 2024
1 parent b49d63e commit 9b4d901
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 35 deletions.
5 changes: 2 additions & 3 deletions example/demo_gen/demo/FixedDecimal.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions example/demo_gen/demo/FixedDecimalFormatter.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions feature_tests/demo_gen/demo/Float64Vec.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 4 additions & 6 deletions feature_tests/demo_gen/demo/MyString.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion feature_tests/demo_gen/demo/Opaque.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions feature_tests/demo_gen/demo/OptionString.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions feature_tests/demo_gen/demo/Utf16Wrap.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion tool/src/demo_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
//! Designed to work in conjunction with the JS backend.
//!
//! See docs/demo_gen.md for more.
use std::{collections::BTreeSet, fmt::Write};
use std::{
collections::{BTreeSet, HashMap},
fmt::Write,
};

use askama::{self, Template};
use diplomat_core::hir::{BackendAttrSupport, TypeContext};
Expand Down Expand Up @@ -201,6 +204,8 @@ pub(crate) fn run<'tcx>(
imports: BTreeSet::new(),
},

out_param_collision: HashMap::new(),

relative_import_path: import_path.clone(),
module_name: module_name.clone(),
};
Expand Down
22 changes: 16 additions & 6 deletions tool/src/demo_gen/terminus.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::BTreeSet;
use std::collections::{BTreeSet, HashMap};

use diplomat_core::hir::{
self, DemoInfo, Method, OpaqueDef, StructDef, StructPath, TyPosition, Type, TypeContext,
Expand Down Expand Up @@ -54,6 +54,9 @@ pub(super) struct RenderTerminusContext<'ctx, 'tcx> {
pub errors: &'ctx ErrorStore<'tcx, String>,
pub terminus_info: TerminusInfo,

/// To avoid similar parameter names while we're collecting [`OutParam`]s.
pub out_param_collision: HashMap<String, i32>,

pub relative_import_path: String,
pub module_name: String,
}
Expand Down Expand Up @@ -236,8 +239,18 @@ impl<'ctx, 'tcx> RenderTerminusContext<'ctx, 'tcx> {
}
};

let (p, n) = if self.out_param_collision.contains_key(&param_name) {
let n = self.out_param_collision.get(&param_name).unwrap();

(format!("{param_name}_{n}"), n + 1)
} else {
(param_name.clone(), 1)
};

self.out_param_collision.insert(param_name, n);

let out_param = OutParam {
param_name,
param_name: p.clone(),
label,
type_name: type_name.clone(),
type_use,
Expand All @@ -246,10 +259,7 @@ impl<'ctx, 'tcx> RenderTerminusContext<'ctx, 'tcx> {

self.terminus_info.out_params.push(out_param);

let param_info = ParamInfo {
// Grab arguments without having to name them
js: format!("terminusArgs[{}]", self.terminus_info.out_params.len() - 1),
};
let param_info = ParamInfo { js: p };

node.params.push(param_info);
}
Expand Down
5 changes: 2 additions & 3 deletions tool/templates/demo_gen/terminus.js.jinja
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
export function {{function_name}}(
{%- if typescript %}{%- for param in out_params -%}
{%- for param in out_params -%}
{{param.param_name}}{% if typescript %}: {{param.type_name}}{% endif %}{% if !loop.last %}, {% endif %}
{%- endfor -%}{% endif -%}
{%- endfor -%}
){% if typescript %};{% else %} {
var terminusArgs = arguments;
return {{node_call_stack|indent(4)}};
}
{%- endif %}

0 comments on commit 9b4d901

Please sign in to comment.