highlightjs / highlight.js

JavaScript syntax highlighter with language auto-detection and zero dependencies.
https://highlightjs.org/
BSD 3-Clause "New" or "Revised" License
23.52k stars 3.57k forks source link

(Javascript) A non keyword gets highlighted #3880

Closed riteshpanchal closed 6 months ago

riteshpanchal commented 11 months ago

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.

Aijeyomah commented 11 months ago

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

jatin-2708 commented 11 months ago

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

luv2027 commented 11 months ago

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 ....

riteshpanchal commented 11 months ago

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 commented 11 months ago

@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.

jatin-2708 commented 11 months ago

@cobtributer please assign this to me

joshgoebel commented 11 months ago

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

carlqt commented 10 months ago

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?

joshgoebel commented 10 months ago

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.

riteshpanchal commented 10 months ago

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 ?

joshgoebel commented 10 months ago

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...

akhtarmdsaad commented 6 months ago

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??

joshgoebel commented 6 months ago

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 commented 6 months ago

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