highlight.js icon indicating copy to clipboard operation
highlight.js copied to clipboard

(Javascript) A non keyword gets highlighted

Open riteshpanchal opened this issue 1 year ago • 11 comments

Describe the issue The first word of a statement that is capitalized is getting highlighted when the language is set to Javascript. I have only tested it with this language.

For example, no syntax to highlight --> does not get hlghlighted. No syntax to highlight --> The word 'No' gets highlighted.

Which language seems to have the issue? Javascript.

Are you using highlight or highlightAuto? No. I am using highlightAll.

** NOTE: I am able to see the same behavior on the demo site too.

Steps to re-produce:

  1. https://highlightjs.org/demo
  2. Select language as javascript
  3. Enter the above 2 statements one by one in the Code section of the page.
  4. Check the result on the right hand side.

Expected behavior Since the word is not a keyword it should not get highlighted.

riteshpanchal avatar Oct 05 '23 22:10 riteshpanchal

@riteshpanchal I'm interested in fixing on this bug. Can you assign to me?

Aijeyomah avatar Oct 06 '23 01:10 Aijeyomah

Please assign this issue to me. I have already solved this issue. I am very much excited to make my first contribution.

jatin-2708 avatar Oct 06 '23 07:10 jatin-2708

Describe the issue The first word of a statement that is capitalized is getting highlighted when the language is set to Javascript. I have only tested it with this language.

For example, no syntax to highlight --> does not get hlghlighted. No syntax to highlight --> The word 'No' gets highlighted.

Which language seems to have the issue? Javascript.

Are you using highlight or highlightAuto? No. I am using highlightAll.

** NOTE: I am able to see the same behavior on the demo site too.

Steps to re-produce:

  1. https://highlightjs.org/demo
  2. Select language as javascript
  3. Enter the above 2 statements one by one in the Code section of the page.
  4. Check the result on the right hand side.

Expected behavior Since the word is not a keyword it should not get highlighted.

All the words which are starting with capital letters are being highlighted not just the first one. This is not a bug maybe, this is feature that all the words with capital letters will be highlighted ....

luv2027 avatar Oct 06 '23 13:10 luv2027

Describe the issue The first word of a statement that is capitalized is getting highlighted when the language is set to Javascript. I have only tested it with this language. For example, no syntax to highlight --> does not get hlghlighted. No syntax to highlight --> The word 'No' gets highlighted. Which language seems to have the issue? Javascript. Are you using highlight or highlightAuto? No. I am using highlightAll. ** NOTE: I am able to see the same behavior on the demo site too. Steps to re-produce:

  1. https://highlightjs.org/demo
  2. Select language as javascript
  3. Enter the above 2 statements one by one in the Code section of the page.
  4. Check the result on the right hand side.

Expected behavior Since the word is not a keyword it should not get highlighted.

All the words which are starting with capital letters are being highlighted not just the first one. This is not a bug maybe, this is feature that all the words with capital letters will be highlighted ....

@luv2027 -- Can you please elaborate in what scenarios this is considered valid?

riteshpanchal avatar Oct 06 '23 15:10 riteshpanchal

@riteshpanchal I'm interested in fixing on this bug. Can you assign to me?

I don't have access to assign this issue to anybody. Maybe the contributors have to assign it.

riteshpanchal avatar Oct 06 '23 15:10 riteshpanchal

@cobtributer please assign this to me

jatin-2708 avatar Oct 07 '23 12:10 jatin-2708

We're always happy to see a PR for an issue, it doesn't need to first be assigned.

joshgoebel avatar Oct 08 '23 01:10 joshgoebel

Describe the issue The first word of a statement that is capitalized is getting highlighted when the language is set to Javascript. I have only tested it with this language. For example, no syntax to highlight --> does not get hlghlighted. No syntax to highlight --> The word 'No' gets highlighted. Which language seems to have the issue? Javascript. Are you using highlight or highlightAuto? No. I am using highlightAll. ** NOTE: I am able to see the same behavior on the demo site too. Steps to re-produce:

  1. https://highlightjs.org/demo
  2. Select language as javascript
  3. Enter the above 2 statements one by one in the Code section of the page.
  4. Check the result on the right hand side.

Expected behavior Since the word is not a keyword it should not get highlighted.

All the words which are starting with capital letters are being highlighted not just the first one. This is not a bug maybe, this is feature that all the words with capital letters will be highlighted ....

@luv2027 -- Can you please elaborate in what scenarios this is considered valid?

I'm not luv2027 but I'm guessing it's trying to predict that all words that are uppercased might be a name for a Class what do you think?

carlqt avatar Oct 20 '23 04:10 carlqt

Oh my bad for not reading this fully. No is CamelCase, which is convention for classes in JavaScript. So highlighting it is the intended behavior. If you want a constant use NO, a variable use no. Yes, our highlighter has opinions about some very common coding conventions.

joshgoebel avatar Oct 20 '23 18:10 joshgoebel

I agree that class names should be CamelCase. Wouldn't the parser take into consideration the "token" in order to determine if the word succeeding the token needs to be in particular case ?

riteshpanchal avatar Nov 03 '23 16:11 riteshpanchal

no syntax to highlight --> does not get hlghlighted. No syntax to highlight --> The word 'No' gets highlighted.

These are NOT valid Javascript examples, so we do not care how poorly they are highlighted if you choose to highlight them as JavaScript. Please first provide actual Javascript examples.

Wouldn't the parser take into consideration the "token" in order to determine if the word succeeding the token needs to be in particular case ?

In some cases... but we are not a full parser and not interested in trying to be. That's a much harder problem... classes are just objects in JS and you can use them almost anywhere... we might do a bit more of this [context awareness] once version 12 comes out and we're allowed to used backwards looking regex... but generally we try and pattern match text - not fully parse code.

CamelCasing is a very STRONG JS convention...

joshgoebel avatar Nov 04 '23 02:11 joshgoebel

These are NOT valid Javascript examples, so we do not care how poorly they are highlighted if you choose to highlight them as JavaScript. Please first provide actual Javascript examples.

@joshgoebel

Here is the Demo: Demo

var no = 5;
var No = 6;

This code highlights a normal variable as class.


There are two ways in javascript to create a class:
  1. Using the class keyword:
class ClassName {
  // Class code goes here
}
class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }

  greet() {
    console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
  }
}
  1. Using the constructor function:
function ClassName(constructorArgs) {
  // Class properties and methods go here
}
function Person(name, age) {
  this.name = name;
  this.age = age;

  this.greet = function() {
    console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
  };
}
function Person(name, age) {
  this.name = name;
  this.age = age;
}

Person.prototype.greet = function() {
  console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
};

And i think "variant" mode will help here. Anything I am missing??

akhtarmdsaad avatar Mar 02 '24 16:03 akhtarmdsaad

Using the constructor function:

Of which var Klass = function() {} is perfectly valid I believe. I think you'll want to read the second part of my response which adds color to the first.

joshgoebel avatar Mar 03 '24 01:03 joshgoebel

Closing as wontfix. Our detection of both CamelCase classes and SCREAM_CASE constants is intentional.

joshgoebel avatar Mar 03 '24 01:03 joshgoebel