Skip to content

Commit

Permalink
Replace invalid variable characters for import name (facebook#4293)
Browse files Browse the repository at this point in the history
Summary:
Closes: facebook#4294

When using provided variables, with ESM, the Relay compiler incorrectly assumes that the module path can also be reused for the default import name.

This is not valid Javascript as variables cannot use '.' or '-' characters.

To fix, simply replace '.' and '-' with underscores.

Before:
```
import include-can-edit-providers-three.relayprovider from './../include-can-edit-providers-three.relayprovider';
import include-can-edit-providers.relayprovider from './../include-can-edit-providers.relayprovider';
const providedVariablesDefinition: ProvidedVariablesType = {
  "__relay_internal__pv__includecaneditprovidersrelayprovider": include-can-edit-providers-three.relayprovider,
  "__relay_internal__pv__includecaneditprovidersthreerelayprovider": include-can-edit-providers.relayprovider
};
```

After:
```
import include_can_edit_providers_two_relayprovider from './../include-can-edit-providers-two.relayprovider';
import include_can_edit_providers_relayprovider from './../include-can-edit-providers.relayprovider';
const providedVariablesDefinition: ProvidedVariablesType = {
  "__relay_internal__pv__includecaneditprovidersrelayprovider": include_can_edit_providers_relayprovider,
  "__relay_internal__pv__includecaneditproviderstworelayprovider": include_can_edit_providers_two_relayprovider
};
```

I'm unfamiliar with Rust so am unsure if there is a better approach.

Pull Request resolved: facebook#4293

Reviewed By: tyao1

Differential Revision: D46360937

Pulled By: captbaritone

fbshipit-source-id: bb8383f0fd97a8510ccf4dff29eefbaddee33be1
  • Loading branch information
JingLi1998 authored and facebook-github-bot committed Jun 2, 2023
1 parent e9150cc commit 04005db
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion compiler/crates/relay-codegen/src/build_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1813,11 +1813,14 @@ impl<'schema, 'builder, 'config> CodegenBuilder<'schema, 'builder, 'config> {
)
};

let variable_name =
(provider.original_variable_name.to_string() + "_provider").intern();

Some(ObjectEntry {
key: def.name.item.0,
value: Primitive::JSModuleDependency(JSModuleDependency {
path: provider_module,
import_name: ModuleImportName::Default(provider.module_name),
import_name: ModuleImportName::Default(variable_name),
}),
})
})
Expand Down

0 comments on commit 04005db

Please sign in to comment.