Giter VIP home page Giter VIP logo

acorn-class-fields's Introduction

Class fields support for Acorn

NPM version

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

It implements support for class fields as defined in the stage 3 proposal Class field declarations for JavaScript. The emitted AST follows the ESTree experimental Class Features design.

Usage

This module provides a plugin that can be used to extend the Acorn Parser class:

const {Parser} = require('acorn');
const classFields = require('acorn-class-fields');
Parser.extend(classFields).parse('class X { x = 0 }');

License

This plugin is released under an MIT License.

acorn-class-fields's People

Contributors

adrianheine avatar guybedford avatar marijnh avatar tchetwin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

acorn-class-fields's Issues

parse error when propertyName is number

field error ( acorn-class-fields )

'use strict';
Error.stackTraceLimit = 100;
require('acorn').Parser.extend( require('acorn-class-fields')                                         ).parse('class X { 1 = 1; }');
SyntaxError: Unexpected token (1:12)
    at Object.pp$4.raise (/node_modules/acorn/dist/acorn.js:2825:15)
    at Object.pp.unexpected (/node_modules/acorn/dist/acorn.js:689:10)
    at Object.pp.expect (/node_modules/acorn/dist/acorn.js:683:28)
    at Object.pp$3.parseMethod (/node_modules/acorn/dist/acorn.js:2609:10)
    at Object.pp$1.parseClassMethod (/node_modules/acorn/dist/acorn.js:1387:25)
    
    at Object.pp$1.parseClassElement (/node_modules/acorn/dist/acorn.js:1376:10)
    
    at Object.parseClassElement (/node_modules/acorn-class-fields/index.js:49:38)
    
    at Object.pp$1.parseClass (/node_modules/acorn/dist/acorn.js:1315:26)
    at Object.parseClass (/node_modules/acorn-class-fields/node_modules/acorn-private-class-elements/index.js:78:29)
    at Object.pp$1.parseStatement (/node_modules/acorn/dist/acorn.js:833:19)
    at Object.pp$1.parseTopLevel (/node_modules/acorn/dist/acorn.js:746:23)
    at Object.parse (/node_modules/acorn/dist/acorn.js:553:17)
    at Function.parse (/node_modules/acorn/dist/acorn.js:576:37)
    at Object.<anonymous> (/TEST.js:3:105)
    at Module._compile (internal/modules/cjs/loader.js:955:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
    at Module.load (internal/modules/cjs/loader.js:811:32)
    at Function.Module._load (internal/modules/cjs/loader.js:723:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)
    at internal/main/run_main_module.js:17:11

field error ( acorn-class-fields, acorn-static-class-features )

'use strict';
Error.stackTraceLimit = 100;
require('acorn').Parser.extend( require('acorn-class-fields'), require('acorn-static-class-features') ).parse('class X { 1 = 1; }');
SyntaxError: Unexpected token (1:12)
    at Object.pp$4.raise (/node_modules/acorn/dist/acorn.js:2825:15)
    at Object.pp.unexpected (/node_modules/acorn/dist/acorn.js:689:10)
    at Object.pp.expect (/node_modules/acorn/dist/acorn.js:683:28)
    at Object.pp$3.parseMethod (/node_modules/acorn/dist/acorn.js:2609:10)
    at Object.pp$1.parseClassMethod (/node_modules/acorn/dist/acorn.js:1387:25)
    at Object.parseClassMethod (/node_modules/acorn-static-class-features/index.js:110:39)
    at Object.pp$1.parseClassElement (/node_modules/acorn/dist/acorn.js:1376:10)
    
    at Object.parseClassElement (/node_modules/acorn-class-fields/index.js:49:38)
    at Object.parseClassElement (/node_modules/acorn-static-class-features/index.js:43:56)
    at Object.pp$1.parseClass (/node_modules/acorn/dist/acorn.js:1315:26)
    at Object.parseClass (/node_modules/acorn-class-fields/node_modules/acorn-private-class-elements/index.js:78:29)
    at Object.pp$1.parseStatement (/node_modules/acorn/dist/acorn.js:833:19)
    at Object.pp$1.parseTopLevel (/node_modules/acorn/dist/acorn.js:746:23)
    at Object.parse (/node_modules/acorn/dist/acorn.js:553:17)
    at Function.parse (/node_modules/acorn/dist/acorn.js:576:37)
    at Object.<anonymous> (/TEST.js:3:105)
    at Module._compile (internal/modules/cjs/loader.js:955:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
    at Module.load (internal/modules/cjs/loader.js:811:32)
    at Function.Module._load (internal/modules/cjs/loader.js:723:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)
    at internal/main/run_main_module.js:17:11

field error ( acorn-static-class-features, acorn-class-fields )

'use strict';
Error.stackTraceLimit = 100;
require('acorn').Parser.extend( require('acorn-static-class-features'), require('acorn-class-fields') ).parse('class X { 1 = 1; }');
SyntaxError: Unexpected token (1:12)
    at Object.pp$4.raise (/node_modules/acorn/dist/acorn.js:2825:15)
    at Object.pp.unexpected (/node_modules/acorn/dist/acorn.js:689:10)
    at Object.pp.expect (/node_modules/acorn/dist/acorn.js:683:28)
    at Object.pp$3.parseMethod (/node_modules/acorn/dist/acorn.js:2609:10)
    at Object.pp$1.parseClassMethod (/node_modules/acorn/dist/acorn.js:1387:25)
    at Object.parseClassMethod (/node_modules/acorn-static-class-features/index.js:110:39)
    at Object.pp$1.parseClassElement (/node_modules/acorn/dist/acorn.js:1376:10)
    at Object.parseClassElement (/node_modules/acorn-static-class-features/index.js:43:56)
    at Object.parseClassElement (/node_modules/acorn-class-fields/index.js:49:38)
    
    at Object.pp$1.parseClass (/node_modules/acorn/dist/acorn.js:1315:26)
    at Object.parseClass (/node_modules/acorn-static-class-features/node_modules/acorn-private-class-elements/index.js:78:29)
    at Object.pp$1.parseStatement (/node_modules/acorn/dist/acorn.js:833:19)
    at Object.pp$1.parseTopLevel (/node_modules/acorn/dist/acorn.js:746:23)
    at Object.parse (/node_modules/acorn/dist/acorn.js:553:17)
    at Function.parse (/node_modules/acorn/dist/acorn.js:576:37)
    at Object.<anonymous> (/TEST.js:3:105)
    at Module._compile (internal/modules/cjs/loader.js:955:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
    at Module.load (internal/modules/cjs/loader.js:811:32)
    at Function.Module._load (internal/modules/cjs/loader.js:723:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)
    at internal/main/run_main_module.js:17:11

static right

There is no error when parse class X { static 1 = 1; }.

Call to acorn-private-class-elements fails with "does not support mixing different acorn copies"

Currently using is as a rollup plugin, with rollup 2.1.0.

Expected behaviour:

rollup bundling works

Actual behaviour:

acorn-class-fields fails in index.js at line 17:
Parser = privateClassElements(Parser)

and this Error is thrown:

[!] Error: acorn-private-class-elements does not support mixing different acorn copies Error: acorn-private-class-elements does not support mixing different acorn copies

Proposed solution

This happens because acorn-private-class-elements expects the Parser object from acorn, so calling it like this

Parser = privateClassElements(acorn.Parser)

solved the issue for me. I hope I'm on the right track and that this can be fixed! Cheers :)

Can I use it as an Injector ?

Hi.

I'm trying to run this plugin with https://github.com/i18next/i18next-parser

Like this

const injectAcornStaticClassPropertyInitializer = require('acorn-static-class-property-initializer/inject');
const classFields = require('acorn-class-fields');

const js = [
    {
        lexer: 'JsxLexer',
        attr: 'i18nKey', // Attribute for the keys

        // acorn config (for more information on the acorn options, see here: https://github.com/acornjs/acorn#main-parser)
        acorn: {
            sourceType: 'module',
            ecmaVersion: 10, // forward compatibility
            injectors: [classFields, injectAcornStaticClassPropertyInitializer],
            plugins: { staticClassPropertyInitializer: true, classFields: true }
        }
    }
];

...

And I have this error


  [read]  /project/frontend/src/app/Market/Chart/index.js
/project/node_modules/i18next-parser/dist/parser.js:70
        }} catch (err) {_didIteratorError = true;_iteratorError = err;} finally {try {if (!_iteratorNormalCompletion && _iterator.return) {_iterator.return();}} finally {if (_didIteratorError) {throw _iteratorError;}}}
                                                                                                                                                                                                  ^

TypeError: Class extends value #<Object> is not a constructor or null
    at module.exports (/project/node_modules/acorn-class-fields/index.js:30:3)
    at /project/node_modules/i18next-parser/dist/lexers/javascript-lexer.js:31:48
    at Array.reduce (<anonymous>)
    at JsxLexer.JavascriptLexer (/project/node_modules/i18next-parser/dist/lexers/javascript-lexer.js:30:34)
    at new JsxLexer (/project/node_modules/i18next-parser/dist/lexers/jsx-lexer.js:36:106)
    at Parser.parse (/project/node_modules/i18next-parser/dist/parser.js:67:23)
    at i18nTransform._transform (/project/node_modules/i18next-parser/dist/transform.js:70:33)
    at i18nTransform.Transform._read (_stream_transform.js:190:10)
    at i18nTransform.Transform._write (_stream_transform.js:178:12)
    at doWrite (_stream_writable.js:410:12)

Is this because of a way acorn-class-fields initializes ?
My acorn deps are:

    "acorn-class-fields": "^0.2.1",
    "acorn-jsx": "^4.1.1",
    "acorn-stage3": "^0.6.0",
    "acorn-static-class-property-initializer": "^1.0.0",

Parse error when "super" is used

Originally via rollup/rollup#3505, which bundles this plugin at the moment:

It is not possible to use super in class properties even though it probably should, at least according to https://2ality.com/2019/07/public-class-fields.html#the-scope-of-field-initializers (I know this is not official documentation...)

At the moment the following throws with a SyntaxError: 'super' keyword outside a method:

require('acorn')
  .Parser.extend(require('acorn-class-fields'), require('acorn-static-class-features'))
  .parse('class SubClass extends SuperClass { prop = super.getValue(); }');

Does not support top-level await

For example the following code:

export default class RequestParams {
    constructor(storage) {
        this._storage = storage;
    }

    headers = {
        'Authorization': await this._storage.getItem(`${domainPrefix}.auth.lockchain`),
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        'X-Device-Version': '49365f68-42e1-11e8-842f-0ed5f89f718b'
    }
}

will throw an error due to the await keyword.

feature-request: add support for private methods

โš ๏ธ Note: I've no knowledge of how this library works, I got some issues with vitest and was redirected to here.

Vitest isn't able to parse the # symbol of #private fields in a class. It seems that vitest uses this plugin under the hood, so it would be great if you can add support for private methods.

I've created an mcve for this issue that you can find here: stackblitz.com/edit/node-stkuzb

Breaking change: support new AST shape defined in estree/experimental

Now that class features reaches stage 3 and shipped in many browsers, people begin trying on this feature. It will be great if acorn can implement https://github.com/estree/estree/blob/master/experimental/class-features.md (even in a stage 3 plugin) so @babel/eslint-parser can align to that AST shape, too.

Lesson from optional chaining: The earlier we begin to work on consensual AST shape, the less the friction we introduced to the communities.

Context: babel/babel#12282

Line break between method name and parenthesis breaks plugin

This works:

const {Parser} = require('acorn');

Parser.parse(`
class X { 
    x
    () {} 
}`);

This fails:

const {Parser} = require('acorn');
const classFields = require('acorn-class-fields');
Parser.extend(classFields).parse(`
class X { 
    x
    () {} 
}`);

with Unexpected token (4:4).

Losing `this` reference from arrow functions with acorn + Rollup

class X {
  arrFn = () => {
    return this.#privateField; // any this references here, private field is just an example
  };
  #privateField = 42;
}

const fn = new X().arrFn;

fn() // expected: 42

Actual result is an error:

The 'this' keyword is equivalent to 'undefined' at the top level of an ES module, and has been rewritten

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.