Skip to content

Commit e5dff62

Browse files
committed
JavaScript: # indicates a private class field or method
1 parent c06d333 commit e5dff62

File tree

4 files changed

+128
-13
lines changed

4 files changed

+128
-13
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--fields=+a
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#method12 input.js /^ #method12() {};$/;" m class:Class5 access:private
2+
#method2 input.js /^ #method2(arg1,arg2)$/;" m class:Class1 access:private
3+
#method3 input.js /^ static #method3(arg = 10)$/;" m class:Class2 access:private
4+
#method4 input.js /^ #method4(){}$/;" m class:Class3 access:private
5+
#method9 input.js /^ #method9() {$/;" m class:func1.InnerClass1 access:private
6+
#value1 input.js /^ #value1$/;" M class:Class1 access:private
7+
#value2 input.js /^ static #value2$/;" M class:Class1 access:private
8+
AnonymousClass1fa6c9a30101 input.js /^class {$/;" c
9+
Class1 input.js /^class Class1$/;" c
10+
Class2 input.js /^class Class2$/;" c
11+
Class3 input.js /^var Class3 = class {$/;" c
12+
Class4 input.js /^var Class4 = class Class4_2 {$/;" c
13+
Class4_2 input.js /^var Class4 = class Class4_2 {$/;" c
14+
Class5 input.js /^class Class5 {$/;" c
15+
InnerClass1 input.js /^ class InnerClass1 {$/;" c function:func1
16+
InnerClass2 input.js /^ class InnerClass2 {$/;" c function:func1
17+
func1 input.js /^function func1() {$/;" f
18+
func2 input.js /^function func2() {$/;" f
19+
method1 input.js /^ method1(arg1,arg2)$/;" m class:Class1
20+
method10 input.js /^ method10() {$/;" m class:func1.InnerClass2
21+
method11 input.js /^ method11() {};$/;" m class:Class5
22+
method5 input.js /^ static method5(){}$/;" m class:Class3
23+
method6 input.js /^ method6(){}$/;" m class:Class4
24+
method7 input.js /^ static method7(){}$/;" m class:Class4
25+
method8 input.js /^ method8(n) { return n * n; }$/;" m class:AnonymousClass1fa6c9a30101
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
class Class1
3+
{
4+
#value1
5+
static #value2
6+
7+
method1(arg1,arg2)
8+
{
9+
}
10+
11+
#method2(arg1,arg2)
12+
{
13+
}
14+
}
15+
16+
class Class2
17+
{
18+
static #method3(arg = 10)
19+
{
20+
}
21+
}
22+
23+
var Class3 = class {
24+
#method4(){}
25+
static method5(){}
26+
}
27+
28+
var Class4 = class Class4_2 {
29+
method6(){}
30+
static method7(){}
31+
}
32+
33+
class {
34+
method8(n) { return n * n; }
35+
}
36+
37+
function func1() {
38+
class InnerClass1 {
39+
#method9() {
40+
}
41+
}
42+
class InnerClass2 {
43+
method10() {
44+
}
45+
}
46+
}
47+
48+
class Class5 {
49+
method11() {};
50+
#method12() {};
51+
}
52+
53+
function func2() {
54+
}

parsers/jscript.c

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ typedef enum eTokenType {
123123
TOKEN_REGEXP,
124124
TOKEN_POSTFIX_OPERATOR,
125125
TOKEN_STAR,
126+
TOKEN_HASH,
126127
/* To handle Babel's decorators.
127128
* Used only in readTokenFull or lower functions. */
128129
TOKEN_ATMARK,
@@ -487,7 +488,7 @@ static int makeJsRefTagsForNameChain (char *name_chain, const tokenInfo *token,
487488

488489
static int makeJsTagCommon (const tokenInfo *const token, const jsKind kind,
489490
vString *const signature, vString *const inheritance,
490-
bool anonymous, bool nulltag)
491+
bool anonymous, bool is_private, bool nulltag)
491492
{
492493
int index = CORK_NIL;
493494
const char *name = vStringValue (token->string);
@@ -526,6 +527,9 @@ static int makeJsTagCommon (const tokenInfo *const token, const jsKind kind,
526527
updateTagLine (&e, token->lineNumber, token->filePosition);
527528
e.extensionFields.scopeIndex = scope;
528529

530+
if (is_private)
531+
e.extensionFields.access = "private";
532+
529533
#ifdef DO_TRACING
530534
{
531535
const char *scope_str = getNameStringForCorkIndex (scope);
@@ -573,19 +577,26 @@ static int makeJsTagCommon (const tokenInfo *const token, const jsKind kind,
573577
static int makeJsTag (const tokenInfo *const token, const jsKind kind,
574578
vString *const signature, vString *const inheritance)
575579
{
576-
return makeJsTagCommon (token, kind, signature, inheritance, false, false);
580+
return makeJsTagCommon (token, kind, signature, inheritance, false, false, false);
581+
}
582+
583+
static int makeJsTagMaybePrivate (const tokenInfo *const token, const jsKind kind,
584+
vString *const signature, vString *const inheritance,
585+
const bool is_private)
586+
{
587+
return makeJsTagCommon (token, kind, signature, inheritance, false, is_private, false);
577588
}
578589

579590
static int makeJsNullTag (const tokenInfo *const token, const jsKind kind,
580591
vString *const signature, vString *const inheritance)
581592
{
582-
return makeJsTagCommon (token, kind, signature, inheritance, false, true);
593+
return makeJsTagCommon (token, kind, signature, inheritance, false, false, true);
583594
}
584595

585596
static int makeClassTagCommon (tokenInfo *const token, vString *const signature,
586597
vString *const inheritance, bool anonymous)
587598
{
588-
return makeJsTagCommon (token, JSTAG_CLASS, signature, inheritance, anonymous, false);
599+
return makeJsTagCommon (token, JSTAG_CLASS, signature, inheritance, anonymous, false, false);
589600
}
590601

591602
static int makeClassTag (tokenInfo *const token, vString *const signature,
@@ -598,7 +609,7 @@ static int makeFunctionTagCommon (tokenInfo *const token, vString *const signatu
598609
bool generator, bool anonymous)
599610
{
600611
return makeJsTagCommon (token, generator ? JSTAG_GENERATOR : JSTAG_FUNCTION, signature, NULL,
601-
anonymous, false);
612+
anonymous, false, false);
602613
}
603614

604615
static int makeFunctionTag (tokenInfo *const token, vString *const signature, bool generator)
@@ -1300,11 +1311,11 @@ static void readTokenFullRaw (tokenInfo *const token, bool include_newlines, vSt
13001311
case '#':
13011312
/* skip shebang in case of e.g. Node.js scripts */
13021313
if (token->lineNumber > 1)
1303-
token->type = TOKEN_UNDEFINED;
1314+
token->type = TOKEN_HASH;
13041315
else if ((c = getcFromInputFile ()) != '!')
13051316
{
13061317
ungetcToInputFile (c);
1307-
token->type = TOKEN_UNDEFINED;
1318+
token->type = TOKEN_HASH;
13081319
}
13091320
else
13101321
{
@@ -1530,7 +1541,7 @@ static int parseMethodsInAnonymousObject (tokenInfo *const token)
15301541
anonGenerate (anon_object->string, "anonymousObject", JSTAG_VARIABLE);
15311542
anon_object->type = TOKEN_IDENTIFIER;
15321543

1533-
index = makeJsTagCommon (anon_object, JSTAG_VARIABLE, NULL, NULL, true, false);
1544+
index = makeJsTagCommon (anon_object, JSTAG_VARIABLE, NULL, NULL, true, false, false);
15341545
if (! parseMethods (token, index, false))
15351546
{
15361547
/* If no method is found, the anonymous object
@@ -2222,6 +2233,7 @@ static bool parseMethods (tokenInfo *const token, int class_index,
22222233
! isType (token, TOKEN_SEMICOLON))
22232234
{
22242235
bool is_generator = false;
2236+
bool is_private = false;
22252237
bool is_shorthand = false; /* ES6 shorthand syntax */
22262238
bool is_computed_name = false; /* ES6 computed property name */
22272239
bool is_dynamic_prop = false;
@@ -2235,6 +2247,11 @@ static bool parseMethods (tokenInfo *const token, int class_index,
22352247
is_generator = true;
22362248
readToken (token);
22372249
}
2250+
else if (isType (token, TOKEN_HASH))
2251+
{
2252+
is_private = true;
2253+
readToken (token);
2254+
}
22382255

22392256
if (isType (token, TOKEN_OPEN_SQUARE))
22402257
{
@@ -2352,7 +2369,16 @@ static bool parseMethods (tokenInfo *const token, int class_index,
23522369
else if (is_setter)
23532370
kind = JSTAG_SETTER;
23542371

2355-
index_for_name = makeJsTag (name, kind, signature, NULL);
2372+
if (is_private)
2373+
{
2374+
vString * s = vStringNew ();
2375+
vStringPut (s, '#');
2376+
vStringCat (s, name->string);
2377+
vStringCopy (name->string, s);
2378+
vStringDelete (s);
2379+
}
2380+
2381+
index_for_name = makeJsTagMaybePrivate (name, kind, signature, NULL, is_private);
23562382
parseBlock (token, index_for_name);
23572383

23582384
/*
@@ -2447,8 +2473,17 @@ static bool parseMethods (tokenInfo *const token, int class_index,
24472473
}
24482474
else
24492475
{
2476+
if (is_private)
2477+
{
2478+
vString * s = vStringNew ();
2479+
vStringPut (s, '#');
2480+
vStringCat (s, name->string);
2481+
vStringCopy (name->string, s);
2482+
vStringDelete (s);
2483+
}
2484+
24502485
bool is_property = isType (token, TOKEN_COMMA);
2451-
makeJsTag (name, is_property ? JSTAG_PROPERTY : JSTAG_FIELD, NULL, NULL);
2486+
makeJsTagMaybePrivate (name, is_property ? JSTAG_PROPERTY : JSTAG_FIELD, NULL, NULL, is_private);
24522487
if (!isType (token, TOKEN_SEMICOLON) && !is_property)
24532488
dont_read = true;
24542489
}
@@ -2517,7 +2552,7 @@ static bool parseES6Class (tokenInfo *const token, const tokenInfo *target_name)
25172552
TRACE_PRINT("Emitting tag for class '%s'", vStringValue(target_name->string));
25182553

25192554
int r = makeJsTagCommon (target_name, JSTAG_CLASS, NULL, inheritance,
2520-
(is_anonymous && (target_name == class_name)), false);
2555+
(is_anonymous && (target_name == class_name)), false, false);
25212556

25222557
if (! is_anonymous && target_name != class_name)
25232558
{
@@ -2855,7 +2890,7 @@ static bool parseStatementRHS (tokenInfo *const name, tokenInfo *const token, st
28552890
if ( parseMethods(token, p, false) )
28562891
{
28572892
jsKind kind = state->foundThis || strchr (vStringValue(name->string), '.') != NULL ? JSTAG_PROPERTY : JSTAG_VARIABLE;
2858-
state->indexForName = makeJsTagCommon (name, kind, NULL, NULL, anon_object, false);
2893+
state->indexForName = makeJsTagCommon (name, kind, NULL, NULL, anon_object, false, false);
28592894
moveChildren (p, state->indexForName);
28602895
}
28612896
else if ( token->nestLevel == 0 && state->isGlobal )
@@ -2902,7 +2937,7 @@ static bool parseStatementRHS (tokenInfo *const name, tokenInfo *const token, st
29022937
*/
29032938
if ( ( token->nestLevel == 0 && state->isGlobal ) || kind == JSTAG_PROPERTY )
29042939
{
2905-
state->indexForName = makeJsTagCommon (name, kind, NULL, NULL, false, false);
2940+
state->indexForName = makeJsTagCommon (name, kind, NULL, NULL, false, false, false);
29062941
}
29072942
}
29082943
else if (isKeyword (token, KEYWORD_new))

0 commit comments

Comments
 (0)