-
Notifications
You must be signed in to change notification settings - Fork 891
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
[acorn] Missing value
property in Token
(TypeScript)
#1285
Comments
There's such a thing as private properties—the types not describing every property in the code does not mean the types are incorrect. I guess this one can be useful to external code in some situations. It is hidden because its type is a complete mess—what kind of value it holds depends on the token type, and even varies within a token type like |
I can share my application's use of My application supports users providing custom scripts that may include special variables that are automatically provided by the application. For example, the user can pass In order to detect all uses of special variables, I use function getScriptVariables(code: string): string[] {
const tokens = [...acorn.tokenizer(code, { ecmaVersion: 2020 })];
const variables = tokens.reduce<string[]>((variables, token) => {
const isVariable =
token.type.label === "name" &&
typeof token.value === "string" &&
isVariableName(token.value);
return isVariable ? [...variables, token.value] : variables;
}, []);
return Array.from(new Set(variables));
} I'd also like to have If you think there's a better way to achieve what I'm looking to do without accessing |
I would recommend doing a regular parse and walking the resulting tree to do this kind of thing. Tokenizing JavaScript without parsing it is problematic in general (there are valid scripts our tokenizer can't handle because it is unable to disambiguate things like regexp vs division operator in some circumstances). |
If you compile a file like the above with
tsc
, you will get the following error:Since the
value
property actually exists, the type definition is considered incorrect.The text was updated successfully, but these errors were encountered: