From 661398c16b8d15954fd30d5f7e13926e4b2fafd2 Mon Sep 17 00:00:00 2001 From: John Lindal Date: Thu, 7 Apr 2022 13:44:45 -0700 Subject: [PATCH] javascript: patch to detect class methods `get` and `set` methods, separate from getters and setters --- .../js-get-and-set.d/args.ctags | 1 + .../js-get-and-set.d/expected.tags | 6 ++++ .../js-get-and-set.d/input.js | 20 ++++++++++++ parsers/jscript.c | 31 ++++++++++++++----- 4 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 Units/parser-javascript.r/js-get-and-set.d/args.ctags create mode 100644 Units/parser-javascript.r/js-get-and-set.d/expected.tags create mode 100644 Units/parser-javascript.r/js-get-and-set.d/input.js diff --git a/Units/parser-javascript.r/js-get-and-set.d/args.ctags b/Units/parser-javascript.r/js-get-and-set.d/args.ctags new file mode 100644 index 0000000000..e6561a52bb --- /dev/null +++ b/Units/parser-javascript.r/js-get-and-set.d/args.ctags @@ -0,0 +1 @@ +--fields=+K diff --git a/Units/parser-javascript.r/js-get-and-set.d/expected.tags b/Units/parser-javascript.r/js-get-and-set.d/expected.tags new file mode 100644 index 0000000000..683e4fb85f --- /dev/null +++ b/Units/parser-javascript.r/js-get-and-set.d/expected.tags @@ -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 diff --git a/Units/parser-javascript.r/js-get-and-set.d/input.js b/Units/parser-javascript.r/js-get-and-set.d/input.js new file mode 100644 index 0000000000..137f4819b8 --- /dev/null +++ b/Units/parser-javascript.r/js-get-and-set.d/input.js @@ -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: [] +} diff --git a/parsers/jscript.c b/parsers/jscript.c index df572df1cf..699242a4d3 100644 --- a/parsers/jscript.c +++ b/parsers/jscript.c @@ -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(...) {} @@ -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) &&