Comments (3)
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 Literal
. Exporting it would mean documenting all that and, probably, giving it a type of any
. What are you using the value for?
from acorn.
I can share my application's use of token.value
.
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 const url = $pageUrl;
and due to $pageUrl
starting with a $
, my application detects that this is a special variable that should be defined before executing the user's script.
In order to detect all uses of special variables, I use acorn.tokenizer
and check the token's value to see if it's a special variable:
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 value
defined on the Token
type (even it's to any
) to know that the property exists and to detect breaking changes in the future where the property may be removed.
If you think there's a better way to achieve what I'm looking to do without accessing token.value
, feel free to let me know. Also, thanks for all your work on acorn
and other projects!
from acorn.
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).
from acorn.
Related Issues (20)
- Acorn fails with nullish coalescing HOT 5
- Incorrect line calculation after invalid escape sequence in tagged template HOT 3
- Strict mode detection logic
- SKG HOT 4
- How does it compare to the speed of vanilla JavaScript? HOT 1
- TypeScript complains when extending Parser HOT 1
- Justin Durand
- Only fans.com
- Unsupported platform for [email protected] HOT 1
- Import Attributes
- Feature Request: ES2025 RegExp Duplicate named capturing groups HOT 6
- Acorn rejects call expression on the lhs of assignment. HOT 8
- [acorn-walk] Type error when using VariableDeclarator with walk.simple HOT 1
- Property and AssignmentProperty have the same type HOT 1
- Peer dependency on acorn for acorn-walk types HOT 4
- Open
- Broken expression is parsed without errors HOT 2
- विश्व
- [Regression] Does not work in older Node HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from acorn.