Skip to content

Commit

Permalink
Merge pull request #3329 from jafl/javascript-allow-class-methods-get…
Browse files Browse the repository at this point in the history
…-and-set

JavaScript: allow class methods get and set
  • Loading branch information
masatake authored Apr 15, 2022
2 parents 62d489a + 661398c commit fc561fb
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
1 change: 1 addition & 0 deletions Units/parser-javascript.r/js-get-and-set.d/args.ctags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--fields=+K
6 changes: 6 additions & 0 deletions Units/parser-javascript.r/js-get-and-set.d/expected.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
get input.js /^ get() {$/;" method class:obj
log input.js /^ log: ['a', 'b', 'c'],$/;" property class:obj
log input.js /^ log: []$/;" property class:o
o input.js /^var o = {$/;" class
obj input.js /^var obj = {$/;" class
set input.js /^ set(str) {$/;" method class:o
20 changes: 20 additions & 0 deletions Units/parser-javascript.r/js-get-and-set.d/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Modified from
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get
var obj = {
log: ['a', 'b', 'c'],
get() {
if (this.log.length == 0) {
return undefined;
}
return this.log[this.log.length - 1];
}
}

// Modified from
// https://developer.mozilla.org/en/docs/JavaScript/Reference/Operators/set
var o = {
set(str) {
this.log[this.log.length] = str;
},
log: []
}
31 changes: 24 additions & 7 deletions parsers/jscript.c
Original file line number Diff line number Diff line change
Expand Up @@ -1618,6 +1618,8 @@ static bool parseMethods (tokenInfo *const token, const tokenInfo *const class,
* container.dirtyTab = {'url': false, 'title':false, 'snapshot':false, '*': false}
* get prop() {}
* set prop(val) {}
* get(...) {}
* set(...) {}
*
* ES6 methods:
* property(...) {}
Expand Down Expand Up @@ -1653,15 +1655,30 @@ static bool parseMethods (tokenInfo *const token, const tokenInfo *const class,

if (isKeyword (token, KEYWORD_async))
readToken (token);
else if (isType(token, TOKEN_KEYWORD) && isKeyword (token, KEYWORD_get))
else if (isType (token, TOKEN_KEYWORD) &&
(isKeyword (token, KEYWORD_get) || isKeyword (token, KEYWORD_set)))
{
is_getter = true;
readToken (token);
}
else if (isType(token, TOKEN_KEYWORD) && isKeyword (token, KEYWORD_set))
{
is_setter = true;
tokenInfo *savedToken = newToken ();
copyToken (savedToken, token, true);
readToken (token);
if (isType(token, TOKEN_OPEN_PAREN))
{
Assert (NextToken == NULL);
NextToken = newToken ();
copyToken (NextToken, token, false); /* save token for next read */
copyToken (token, savedToken, true); /* restore token to process */
token->type = TOKEN_IDENTIFIER; /* process as identifier */
token->keyword = KEYWORD_NONE;
}
else if (isKeyword (savedToken, KEYWORD_get))
{
is_getter = true;
}
else
{
is_setter = true;
}
deleteToken (savedToken);
}

if (! isType (token, TOKEN_KEYWORD) &&
Expand Down

0 comments on commit fc561fb

Please sign in to comment.