Skip to content

Commit

Permalink
javascript: Add support for the const keyword
Browse files Browse the repository at this point in the history
`const` is not yet part of the current ECMAScript standard but is
part of the ECMAScript 6 draft and is supported by popular engines
including Mozilla and WebKit, and people already use it.

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const
  • Loading branch information
b4n committed Nov 24, 2014
1 parent b85d754 commit ef8c40f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
20 changes: 14 additions & 6 deletions tagmanager/ctags/js.c
Expand Up @@ -58,6 +58,7 @@ typedef enum eKeywordId {
KEYWORD_capital_object,
KEYWORD_prototype,
KEYWORD_var,
KEYWORD_const,
KEYWORD_new,
KEYWORD_this,
KEYWORD_for,
Expand Down Expand Up @@ -131,6 +132,7 @@ typedef enum {
JSTAG_CLASS,
JSTAG_METHOD,
JSTAG_PROPERTY,
JSTAG_CONSTANT,
JSTAG_VARIABLE,
JSTAG_COUNT
} jsKind;
Expand All @@ -140,6 +142,7 @@ static kindOption JsKinds [] = {
{ TRUE, 'c', "class", "classes" },
{ TRUE, 'm', "method", "methods" },
{ TRUE, 'p', "member", "properties" },
{ TRUE, 'C', "macro", "constants" },
{ TRUE, 'v', "variable", "global variables" }
};

Expand All @@ -150,6 +153,7 @@ static const keywordDesc JsKeywordTable [] = {
{ "Object", KEYWORD_capital_object },
{ "prototype", KEYWORD_prototype },
{ "var", KEYWORD_var },
{ "const", KEYWORD_const },
{ "new", KEYWORD_new },
{ "this", KEYWORD_this },
{ "for", KEYWORD_for },
Expand Down Expand Up @@ -1079,7 +1083,8 @@ static boolean parseBlock (tokenInfo *const token, tokenInfo *const orig_parent)

vStringCopy(token->scope, saveScope);
}
else if (isKeyword (token, KEYWORD_var))
else if (isKeyword (token, KEYWORD_var) ||
isKeyword (token, KEYWORD_const))
{
/*
* Potentially we have found an inner function.
Expand Down Expand Up @@ -1253,6 +1258,7 @@ static boolean parseStatement (tokenInfo *const token, tokenInfo *const parent,
vString * saveScope = vStringNew ();
boolean is_class = FALSE;
boolean is_var = FALSE;
boolean is_const = FALSE;
boolean is_terminated = TRUE;
boolean is_global = FALSE;
boolean has_methods = FALSE;
Expand Down Expand Up @@ -1292,8 +1298,10 @@ static boolean parseStatement (tokenInfo *const token, tokenInfo *const parent,
/*
* var can preceed an inner function
*/
if ( isKeyword(token, KEYWORD_var) )
if ( isKeyword(token, KEYWORD_var) ||
isKeyword(token, KEYWORD_const) )
{
is_const = isKeyword(token, KEYWORD_const);
/*
* Only create variables for global scope
*/
Expand Down Expand Up @@ -1482,7 +1490,7 @@ static boolean parseStatement (tokenInfo *const token, tokenInfo *const parent,
* var g_var2;
*/
if (isType (token, TOKEN_SEMICOLON))
makeJsTag (name, JSTAG_VARIABLE, NULL);
makeJsTag (name, is_const ? JSTAG_CONSTANT : JSTAG_VARIABLE, NULL);
}
/*
* Statement has ended.
Expand Down Expand Up @@ -1617,7 +1625,7 @@ static boolean parseStatement (tokenInfo *const token, tokenInfo *const parent,
if ( ! stringListHas(FunctionNames, vStringValue (fulltag)) &&
! stringListHas(ClassNames, vStringValue (fulltag)) )
{
makeJsTag (name, JSTAG_VARIABLE, NULL);
makeJsTag (name, is_const ? JSTAG_CONSTANT : JSTAG_VARIABLE, NULL);
}
vStringDelete (fulltag);
}
Expand Down Expand Up @@ -1653,7 +1661,7 @@ static boolean parseStatement (tokenInfo *const token, tokenInfo *const parent,
{
if ( is_var )
{
makeJsTag (name, JSTAG_VARIABLE, NULL);
makeJsTag (name, is_const ? JSTAG_CONSTANT : JSTAG_VARIABLE, NULL);
}
else
{
Expand Down Expand Up @@ -1708,7 +1716,7 @@ static boolean parseStatement (tokenInfo *const token, tokenInfo *const parent,
if ( ! stringListHas(FunctionNames, vStringValue (fulltag)) &&
! stringListHas(ClassNames, vStringValue (fulltag)) )
{
makeJsTag (name, JSTAG_VARIABLE, NULL);
makeJsTag (name, is_const ? JSTAG_CONSTANT : JSTAG_VARIABLE, NULL);
}
vStringDelete (fulltag);
}
Expand Down
1 change: 1 addition & 0 deletions tests/ctags/Makefile.am
Expand Up @@ -160,6 +160,7 @@ test_sources = \
invalid_name.f90 \
java_enum.java \
js-class-related-unterminated.js \
js-const.js \
js-implicit-semicolons.js \
js-scope.js \
js-signature.js \
Expand Down
10 changes: 10 additions & 0 deletions tests/ctags/js-const.js
@@ -0,0 +1,10 @@

const A = 1;
const B = 1;
const Group = {
X:1,
Y:2,
Z:3
};

const func = function () {}
8 changes: 8 additions & 0 deletions tests/ctags/js-const.js.tags
@@ -0,0 +1,8 @@
# format=tagmanager
A�65536�0
B�65536�0
Group�1�0
X�64�Group�0
Y�64�Group�0
Z�64�Group�0
func�16�()�0

0 comments on commit ef8c40f

Please sign in to comment.