Skip to content

Support special characters in module names#428

Merged
Perryvw merged 3 commits intoTypeScriptToLua:masterfrom
thatcosmonaut:fix-kebab-case
Feb 20, 2019
Merged

Support special characters in module names#428
Perryvw merged 3 commits intoTypeScriptToLua:masterfrom
thatcosmonaut:fix-kebab-case

Conversation

@thatcosmonaut
Copy link
Copy Markdown
Contributor

Lua hates hyphens in variable names, so this fixes cases where an imported TypeScript module is named in kebab case.

Fixes #403

@hazzard993
Copy link
Copy Markdown
Contributor

Would it be ok to extend this PR to handle some other invalid characters in variable names?

You can get away with single quotes, hashes, spaces, $ as well as hypens in file names and Lua and TypeScript are ok with it. Lua just gets upset with the resulting variable names.

Extending your RegExp you can use this:

const tstlIdentifier = (name: string) => "__TSTL_" + name
    .replace(new RegExp("-|\\$| |#|'", "g"), "_");

The translation tests could also be extended to cover these cases.

import {TestClass} from "kebab-module"
import {TestClass} from "dollar$module"
import {TestClass} from "singlequote'module"
import {TestClass} from "hash#module"
import {TestClass} from "space module"
import {TestClass as RenamedClass} from "kebab-module"
import {TestClass as RenamedClass} from "dollar$module"
import {TestClass as RenamedClass} from "singlequote'module"
import {TestClass as RenamedClass} from "hash#module"
import {TestClass as RenamedClass} from "space module"

@thatcosmonaut
Copy link
Copy Markdown
Contributor Author

I guess there's no harm in being thorough.

@thatcosmonaut thatcosmonaut changed the title Module name uses underscore instead of hyphen when transpiling Support special characters in module names Feb 20, 2019
@thatcosmonaut
Copy link
Copy Markdown
Contributor Author

This now works with different special characters and not just hyphen.

@Perryvw Perryvw merged commit b5dd8ef into TypeScriptToLua:master Feb 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Imports that use hyphen in filename break when transpiled to Lua

4 participants