-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Tokens for Import and Export Names in JS? (Local, Import and Export Names) #2528
Comments
What's the use case for this? Also, we usually highlight concepts, so what is the general idea here? Syntax-wise, from the |
Use case is being able to be more specific about import names for themes like this:
Right ok, I suppose the overarching concept is actually "all identifiers". In addition to the variables in the issue description, I would think that As for a concrete use case about that (also irrespective of the Maybe for import and export names they could be subclassed as If it makes sense, I can change this issue to be about "identifiers" in general. |
Maybe a bit on how VSC highlights variables in JS/TS. VSC uses the TS language server to parse and analyze JS and TS files (among other things). A while ago they introduced sematic highlighting. This means, that variables are not highlighted by syntax alone but with a deeper understanding of the type and other properties of the variable (powered by the TS language server). This is why VSC can highlight VSC can do this not only for the type of a variable (class/function/other) but also for its mutability. Readonly variables/properties will be highlighted with a darker shade of blue: This is incredibly useful because it gives a lot of information to the developer that is not obvious from syntax alone. However, that's the problem for us. We need more information than just syntax to handle the nested properties correctly. For flat variables, syntax alone is technically enough, but...
No. Tracking variables requires an understanding of scope. This is hard enough with an AST but we only have a token stream to work with. This is one of the things that Prism simply can't do. |
Right yeah, I understand that we won't get to 100% of the capabilities of an editor with type information. But for the following, maybe regexes / config like the following would be possible: const a = 1;
let b = 2,
c = 3;
export const foo = 1; Config:
For the more complex examples, maybe the contents inside the parentheses of function calls could be parsed with a similar pattern to Technically, it's an const d = e * f;
console.log(g.z);
otherFn(h * i); |
This will be hard because we have to deal with JS' semicolon insertion. Basically, we don't know where a variable declaration ends. To get this right, we'd have to match a single valid JS expression. The formal language of a single JS expression is context-free and cannot be expressed by a regex. We can usually work around this because we usually know the next character after the expression (e.g. a comma or closing bracket). If we assume semicolons, it's doable but otherwise, ... I doubt it. We could settle for the first declaration. That's easy and, hopefully, nobody uses these merged variable declarations. However, even then: Will this be beneficial? If I remember correctly, VSC used to highlight all variable declarations in a different color but then dropped it again in the next release (I assume for similar reasons). |
Ok, I can understand that this goes a bit far then. You're right that the benefit is small here and I agree that it would be confusing. Maybe we stick with import v from "mod";
import {x as v2} from "mod";
import * as ns from "mod";
export * as ns from "mod";
export {v as x} from "mod";
export {x} from "mod";
export {v as x};
export {x}; |
Right so if you look at the demo, the |
I suppose an alternative that may work could be ( .token.keyword.module + .token.maybe-class-name,
.token.keyword.module + .token.punctuation + .token.maybe-class-name {
color: #9cdcfe;
} |
I guess the same token name should be fine? |
Addressed in #2533 🙌 🚀 |
Closed by #2533. |
Original issue: PrismJS/prism#2528 Pull request: PrismJS/prism#2533
Thanks @RunDevelopment ! |
Information
Does the problem still occur in the latest version of Prism? Yes
Description
Would it be possible to add tokens for the import and export names in JavaScript? (see patterns below taken from the import examples and export examples in the ECMAScript spec)
See below - currently they are not receiving any tokenization. To be clear, I'm referring to the local names, import names and export names with names such as
v
,x
,v2
andns
:I also tried using the
js-extras
language as follows, but that didn't seem to help:https://codesandbox.io/s/prismjs-forked-wu91d
Code snippet
The code being highlighted incorrectly:
The text was updated successfully, but these errors were encountered: