acornjs / acorn-class-fields Goto Github PK
View Code? Open in Web Editor NEWClass fields support for acorn
License: MIT License
Class fields support for acorn
License: MIT License
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)
.
https://npm.runkit.com/acorn-class-fields
Throws syntax error on:
var acornClassFields = require("acorn-class-fields")
var ast = acornClassFields.parse(`
class Test {
x = 10;
constructor(){
}
}
`, {
plugins: { classFields: true }
});
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",
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.
class Sample {
class = 1;
}
'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
'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
'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
There is no error when parse class X { static 1 = 1; }
.
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
The 'this' keyword is equivalent to 'undefined' at the top level of an ES module, and has been rewritten
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
Currently using is as a rollup plugin, with rollup 2.1.0.
rollup bundling works
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
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 :)
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
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(); }');
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.