-
Notifications
You must be signed in to change notification settings - Fork 636
JavaScript: # indicates a private class field or method #4269
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
base: master
Are you sure you want to change the base?
Conversation
@masatake How can I indicate that a private field is also static? |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #4269 +/- ##
=======================================
Coverage 85.95% 85.96%
=======================================
Files 246 246
Lines 63434 63454 +20
=======================================
+ Hits 54525 54546 +21
+ Misses 8909 8908 -1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Interesting topic. The main part doesn't provide a standard way for representing static methods (or class methods). There are two approaches:
Let's see some parsers. Python uses a parser-specific field named
C++ also uses a parser-specific field named
Ruby uses a specialized kind named
|
@masatake I am hoping to do it the C++ way. From studying the code, however, I couldn't figure out how it was done. I found |
I recommend you read the code diff --git a/parsers/jscript.c b/parsers/jscript.c
index 168dfb533..869daac0a 100644
--- a/parsers/jscript.c
+++ b/parsers/jscript.c
@@ -41,6 +41,7 @@
#include "debug.h"
#include "dependency.h"
#include "entry.h"
+#include "field.h"
#include "keyword.h"
#include "numarray.h"
#include "parse.h"
@@ -251,6 +252,10 @@ typedef enum {
JS_CLASS_CHAINELT,
} jsClassRole;
+typedef enum {
+ F_THEFIELD,
+} jsField;
+
static roleDefinition JsFunctionRoles [] = {
/* Currently V parser wants this items. */
{ true, "foreigndecl", "declared in foreign languages" },
@@ -280,6 +285,14 @@ static kindDefinition JsKinds [] = {
{ true, 'M', "field", "fields" },
};
+static fieldDefinition JsFields[] = {
+ {
+ .name = "theNameOfField",
+ .description = "...description...",
+ .enabled = true,
+ },
+};
+
static const keywordTable JsKeywordTable [] = {
/* keyword keyword ID */
{ "function", KEYWORD_function },
@@ -527,6 +540,14 @@ static int makeJsTagCommon (const tokenInfo *const token, const jsKind kind,
updateTagLine (&e, token->lineNumber, token->filePosition);
e.extensionFields.scopeIndex = scope;
+ /* There are two ways (A and B) to attach a value to field.
+ *
+ * This is the way A. A. is suitable for a static value.
+ * Attaching after initTagEntry() before makeTagEntry(): */
+ if (is_static)
+ attachParserField (&e, JsFields[F_THEFIELD].ftype,
+ "value");
+
if (is_private)
e.extensionFields.access = "private";
@@ -565,6 +586,21 @@ static int makeJsTagCommon (const tokenInfo *const token, const jsKind kind,
e.allowNullTag = 1;
index = makeTagEntry (&e);
+
+ /*
+ * This is the way B. B. is suitable for dynamic value.
+ * Attaching after makeTagEntry():
+ *
+ * Choose A or B.
+ */
+ {
+ vString *tmp = vStringNew();
+ /* build the value for the field */
+ attachParserFieldToCorkEntry (index, JsFields[F_THEFIELD].ftype,
+ vStringValue (tmp));
+ vStrngDelete (tmp);
+ }
+
/* We shold remove This condition. We should fix the callers passing
* an empty name instead. makeTagEntry() returns CORK_NIL if the tag
* name is empty. */
@@ -3797,6 +3833,8 @@ extern parserDefinition* JavaScriptParser (void)
*/
def->kindTable = JsKinds;
def->kindCount = ARRAY_SIZE (JsKinds);
+ def->fieldTable = JsFields;
+ def->fieldCount = ARRAY_SIZE (JsFields);
def->parser = findJsTags;
def->initialize = initialize;
def->finalize = finalize; |
parsers/jscript.c
Outdated
if (is_private) | ||
{ | ||
vString * s = vStringNew (); | ||
vStringPut (s, '#'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can unify the two function calls into one with:
vString * s = vStringNewInit ("#");
I don't say this is efficient:-).
parsers/jscript.c
Outdated
if (is_private) | ||
{ | ||
vString * s = vStringNew (); | ||
vStringPut (s, '#'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
vStringNewInit() can be used here.
parsers/jscript.c
Outdated
vStringPut (s, '#'); | ||
vStringCat (s, name->string); | ||
vStringCopy (name->string, s); | ||
vStringDelete (s); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of updating the string
member of the name
token, we can make the #
prefixed private name in makeJsTagCommon.
diff --git a/parsers/jscript.c b/parsers/jscript.c
index 168dfb533..e88799c2e 100644
--- a/parsers/jscript.c
+++ b/parsers/jscript.c
@@ -486,12 +499,21 @@ static int makeJsRefTagsForNameChain (char *name_chain, const tokenInfo *token,
: index;
}
+static vString *makPrivateName (vString *orignal)
+{
+ vString * s = vStringNewInit ("#");
+ vStringCopy (s, orignal);
+ return s;
+}
+
static int makeJsTagCommon (const tokenInfo *const token, const jsKind kind,
vString *const signature, vString *const inheritance,
bool anonymous, bool is_private, bool nulltag)
{
int index = CORK_NIL;
- const char *name = vStringValue (token->string);
+ vString *vname = is_private? makePrivateName (vname): token->string;
+ const char *name = vStringValue (vname);
+
const char *p;
char *name_chain = NULL;
@@ -501,7 +523,11 @@ static int makeJsTagCommon (const tokenInfo *const token, const jsKind kind,
name_chain = eStrndup (name, (size_t) (p - name));
name = p + 1;
if (name[0] == '\0')
+ {
+ if (is_private)
+ vStringDelete (vname);
return CORK_NIL;
+ }
}
int scope = token->scope;
@@ -565,8 +599,11 @@ static int makeJsTagCommon (const tokenInfo *const token, const jsKind kind,
* an empty name instead. makeTagEntry() returns CORK_NIL if the tag
* name is empty. */
if (index != CORK_NIL)
registerEntry (index);
+ if (is_private)
+ vStringDelete (vname);
+
return index;
}
Just an idea.
Whether Thinking about shell scripts, do you think Even if a name doesn't include These are just inputs to you. I have no answer for you. Please, make JavaScript people and developers of client tools comfortable! |
Absolutely. In JavaScript, a class can have both a public function |
e5dff62
to
538cd02
Compare
@masatake Thanks for the help. I have updated the PR. |
Unlike C++, JavaScript itself has the concept named properties. I'm afraid that using the name The test is failed because you added a new field.
When adding/removing kinds, roles, parser-specific fields, and/or parser-specific extras (KRFX or KRFE), I would like you to update versionCurrent and versionAge of the parser like:
See main/parse.h when we should increment the members. |
I think it's good to be consistent with the rest of the languages that use "static". I suppose we could use "attributes", though that is also a keyword. |
My alternative ideas are |
538cd02
to
4512da5
Compare
Sorry to be late in saying the following:
|
4512da5
to
69108e1
Compare
No problem! Updated. |
No description provided.