GithubHelp home page GithubHelp logo

Comments (3)

marijnh avatar marijnh commented on August 28, 2024

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.

namukang avatar namukang commented on August 28, 2024

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.

marijnh avatar marijnh commented on August 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.