Skip to content

Commit 397f675

Browse files
authored
Merge pull request #1047 from NativeScript/trifonov/keyword-namespace
Add $ in front when the root namespace matches JS keyword
2 parents 952a7fd + 14985c0 commit 397f675

File tree

4 files changed

+36
-13
lines changed

4 files changed

+36
-13
lines changed

test-app/app/src/main/assets/app/tests/testMetadata.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
describe("Tests metadata", function () {
2-
2+
33
it("Can_access_protected_static_members_of_protected_interface", function () {
44
var accountType = android.provider.ContactsContract.SyncColumns.ACCOUNT_TYPE;
55
expect(accountType).toBeTruthy(accountType !== undefined);
66
});
7-
7+
88
it("should access public methods of non-public base classes", function () {
99
var c = new com.tns.tests.MyTestDerivedClass();
1010
var exceptionCaught = false;
@@ -23,10 +23,16 @@ describe("Tests metadata", function () {
2323

2424
it("should be able to access static fields declared in interface from implementing class", function () {
2525
var c = new com.tns.tests.MyTestDerivedClass();
26-
26+
2727
var staticFieldFromInterface = android.app.Activity.TRIM_MEMORY_RUNNING_MODERATE;
2828
var expected = 5;
29-
29+
3030
expect(staticFieldFromInterface).toBe(expected);
3131
});
32+
33+
it("should be able to access keyword namespace bu prefixing the namespace with $", function () {
34+
var keywordClass = new $in.tns.tests.JavascriptKeywordClass();
35+
var expected = 5;
36+
expect(keywordClass.getValue5()).toBe(expected);
37+
});
3238
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package in.tns.tests;
2+
3+
public class JavascriptKeywordClass {
4+
public int getValue5()
5+
{
6+
return 5;
7+
}
8+
}

test-app/runtime/src/main/cpp/MetadataNode.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,7 +1619,12 @@ void MetadataNode::CreateTopLevelNamespaces(Isolate* isolate, const Local<Object
16191619
auto node = GetOrCreateInternal(treeNode);
16201620

16211621
auto packageObj = node->CreateWrapper(isolate);
1622-
global->Set(ArgConverter::ConvertToV8String(isolate, node->m_treeNode->name), packageObj);
1622+
string nameSpace = node->m_treeNode->name;
1623+
// if the namespaces matches a javascript keyword, prefix it with $ to avoid TypeScript and JavaScript errors
1624+
if(IsJavascriptKeyword(nameSpace)) {
1625+
nameSpace = "$" + nameSpace;
1626+
}
1627+
global->Set(ArgConverter::ConvertToV8String(isolate, nameSpace), packageObj);
16231628
}
16241629
}
16251630
}
@@ -1640,25 +1645,29 @@ void MetadataNode::EnableProfiler(bool enableProfiler) {
16401645
s_profilerEnabled = enableProfiler;
16411646
}
16421647

1643-
Local<Function> MetadataNode::Wrap(Isolate* isolate, const Local<Function>& function, const string& name, const string& origin, bool isCtorFunc) {
1644-
if (!s_profilerEnabled || name == "<init>") {
1645-
return function;
1646-
}
1647-
1648+
bool MetadataNode::IsJavascriptKeyword(std::string word) {
16481649
static set<string> keywords;
16491650

16501651
if (keywords.empty()) {
16511652
string kw[] { "abstract", "arguments", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "debugger", "default", "delete", "do",
16521653
"double", "else", "enum", "eval", "export", "extends", "false", "final", "finally", "float", "for", "function", "goto", "if", "implements",
16531654
"import", "in", "instanceof", "int", "interface", "let", "long", "native", "new", "null", "package", "private", "protected", "public", "return",
16541655
"short", "static", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "true", "try", "typeof", "var", "void", "volatile", "while", "with", "yield"
1655-
};
1656+
};
16561657

16571658
keywords = set<string>(kw, kw + sizeof(kw)/sizeof(kw[0]));
16581659
}
16591660

1661+
return keywords.find(word) != keywords.end();
1662+
}
1663+
1664+
Local<Function> MetadataNode::Wrap(Isolate* isolate, const Local<Function>& function, const string& name, const string& origin, bool isCtorFunc) {
1665+
if (!s_profilerEnabled || name == "<init>") {
1666+
return function;
1667+
}
1668+
16601669
string actualName = name;
1661-
while (keywords.find(actualName) != keywords.end()) {
1670+
while (IsJavascriptKeyword(actualName)) {
16621671
actualName.append("_");
16631672
}
16641673

test-app/runtime/src/main/cpp/MetadataNode.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ namespace tns {
5656
static MetadataNode* GetOrCreate(const std::string& className);
5757

5858
static std::string GetTypeMetadataName(v8::Isolate* isolate, v8::Local<v8::Value>& value);
59-
6059
private:
6160
struct MethodCallbackData;
6261

@@ -70,6 +69,7 @@ namespace tns {
7069

7170
MetadataNode(MetadataTreeNode* treeNode);
7271

72+
static bool IsJavascriptKeyword(std::string word);
7373
v8::Local<v8::Object> CreatePackageObject(v8::Isolate* isolate);
7474

7575
v8::Local<v8::Function> GetConstructorFunction(v8::Isolate* isolate);

0 commit comments

Comments
 (0)