Code Monkey home page Code Monkey logo

acorn-visualforce's Introduction

Acorn-VisualForce

Build Status NPM version

This is plugin for Acorn - a tiny, fast JavaScript parser, written completely in JavaScript.

It was created as an experimental parser for VisualForce inline JS.

Usage

You can use module directly in order to get Acorn instance with plugin installed:

const acorn = require('@salesforce/acorn-visualforce');

Or you can use inject.js for injecting plugin into your own version of Acorn like following:

const acorn = require('@salesforce/acorn-visualforce/dist/inject')(require('./custom-acorn'));

Then, use plugins option whenever you need to support VisualForce merge fields while parsing:

const code = 'var foo = {! JSENCODE(someApexVariable) };'
const ast = acorn.parse(code, {
  plugins: {
    jsx: true, // make sure that jsx appears BEFORE vfel to work correctly
    vfel: true,
  }
});

Produces:

{
    "type": "Program",
    "start": 0,
    "end": 42,
    "body": [{
        "type": "VariableDeclaration",
        "start": 0,
        "end": 42,
        "declarations": [{
            "type": "VariableDeclarator",
            "start": 4,
            "end": 41,
            "id": {
                "type": "Identifier",
                "start": 4,
                "end": 7,
                "name": "foo"
            },
            "init": {
                "type": "VFELExpression",
                "start": 10,
                "end": 41,
                "value": {
                    "type": "CallExpression",
                    "start": 12,
                    "end": 39,
                    "callee": {
                        "type": "Identifier",
                        "start": 12,
                        "end": 21,
                        "name": "JSENCODE"
                    },
                    "arguments": [{
                        "type": "Identifier",
                        "start": 22,
                        "end": 38,
                        "name": "someApexVariable"
                    }]
                }
            }
        }],
        "kind": "var"
    }],
    "sourceType": "script"
}

The injector function also supports an optional second parameter that allows to force-inject the plugin, so that there is no need to specify the plugins options to parse or tokenize functions. This is useful for monkey-patching other parser wrappers such as espree that force their own plugin options.

const acorn = require('@salesforce/acorn-visualforce/dist/inject')(require('./custom-acorn'), true);
const code = 'var foo = {! JSENCODE(someApexVariable) };'
const ast = acorn.parse(code);

License

This plugin is issued under the BSD-3-Clause license.

acorn-visualforce's People

Contributors

svc-scm avatar touzoku avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

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.