Skip to content

Highlighting Upper Camelized Variables as Constants #633

@jfelchner

Description

@jfelchner

I'm creating a new issue from #631. This was a point of blockage to getting that PR merged and @amadeus thought it best if we moved it into another issue so that we could discuss it further.

My feelings are that, in most programming languages (and even JS now with ES6 classes and imports) that if you have a variable which is UpperCamelized that it denotes a thing you would like to think of as a constant. Be it a class or a constant variable.

I understand that the syntax stance that you all are taking here is a "semantic" one for the most part. Or: What does the interpreter see this as?

However from my point of view, syntax highlighting isn't for the interpreter, it's for the developer.

While it may be the case that:

import HelloThere from 'hello';

class FooBar extends HelloThere {
  doSomething() {
    // ...
  }
}

the interpreter sees FooBar and doSomething as the same type of object, from the developer's perspective particularly if they're writing class-based syntax, they want to think about those things differently. Otherwise, why wouldn't everything be lower-camelized like standard JS variables?

There has to be a reason why you would choose to go FooBar instead of fooBar and in my experience it's because you want to change the way you think about that variable. Which is what syntax highlighting is for.

So it seems like there's not going to be a consensus on whether to do this or not, but I would suggest a compromise. An option to allow this behavior should the user want it.

if exists('g:javascript_highlight_upper_camel_as_constants')
  syntax match   jsConstant           "\<[A-Z][A-Za-z]\+\>"

  syntax region  jsImportContainer    add=jsConstant
  syntax region  jsDestructuringBlock add=jsConstant
  syntax cluster jsExpression         add=jsConstant
  syntax region  jsDestructuringBlock add=jsConstant

  HiLink jsClassDefinition      Constant
  HiLink jsConstant             Constant
else
  HiLink jsClassDefinition      jsFuncName
endif

The above isn't final but simply an example.

The benefits are that now users can opt-in with a single config variable rather than having to copy and paste this same code everywhere. Additionally, if bugs are found, they can be corrected once and allow everyone to benefit.

The downsides are that it's more code and the conditional causes the syntax to behave differently depending on the settings, which could make debugging slightly more difficult.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions