Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support ES2023 and hashbangs #556

Merged
merged 5 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,17 +231,11 @@ We are building on top of Acorn, however, so that we can contribute back and hel

### What ECMAScript features do you support?

Espree supports all ECMAScript 2021 features and partially supports ECMAScript 2022 features.
Espree supports all ECMAScript 2022 features and partially supports ECMAScript 2023 features.

Because ECMAScript 2022 is still under development, we are implementing features as they are finalized. Currently, Espree supports:
Because ECMAScript 2023 is still under development, we are implementing features as they are finalized. Currently, Espree supports:

* [Class instance fields](https://github.com/tc39/proposal-class-fields)
* [Class private instance methods and accessors](https://github.com/tc39/proposal-private-methods)
* [Class static fields, static private methods and accessors](https://github.com/tc39/proposal-static-class-features)
* [RegExp match indices](https://github.com/tc39/proposal-regexp-match-indices)
* [Top-level await](https://github.com/tc39/proposal-top-level-await)
* [Class static initialization blocks](https://github.com/tc39/proposal-class-static-block)
* [Ergonomic brand checks for Private Fields](https://github.com/tc39/proposal-private-fields-in-in)
* [Hashbang grammar](https://github.com/tc39/proposal-hashbang)

See [finished-proposals.md](https://github.com/tc39/proposals/blob/master/finished-proposals.md) to know what features are finalized.

Expand Down
43 changes: 26 additions & 17 deletions lib/espree.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,25 @@ export default () => Parser => {
? new TokenTranslator(tokTypes, code)
: null;

/*
* Data that is unique to Espree and is not represented internally
* in Acorn.
*
* For ES2023 hashbangs, Espree will call `onComment()` during the
* constructor, so we must define state before having access to
* `this`.
*/
const state = {
originalSourceType: originalSourceType || options.sourceType,
tokens: tokenTranslator ? [] : null,
comments: options.comment === true ? [] : null,
impliedStrict: ecmaFeatures.impliedStrict === true && options.ecmaVersion >= 5,
ecmaVersion: options.ecmaVersion,
jsxAttrValueToken: false,
lastToken: null,
templateElements: []
};

// Initialize acorn parser.
super({

Expand All @@ -83,38 +102,28 @@ export default () => Parser => {
if (tokenTranslator) {

// Use `tokens`, `ecmaVersion`, and `jsxAttrValueToken` in the state.
tokenTranslator.onToken(token, this[STATE]);
tokenTranslator.onToken(token, state);
}
if (token.type !== tokTypes.eof) {
this[STATE].lastToken = token;
state.lastToken = token;
}
},

// Collect comments
onComment: (block, text, start, end, startLoc, endLoc) => {
if (this[STATE].comments) {
if (state.comments) {
const comment = convertAcornCommentToEsprimaComment(block, text, start, end, startLoc, endLoc);

this[STATE].comments.push(comment);
state.comments.push(comment);
}
}
}, code);

/*
* Data that is unique to Espree and is not represented internally in
* Acorn. We put all of this data into a symbol property as a way to
* avoid potential naming conflicts with future versions of Acorn.
* We put all of this data into a symbol property as a way to avoid
* potential naming conflicts with future versions of Acorn.
*/
this[STATE] = {
originalSourceType: originalSourceType || options.sourceType,
tokens: tokenTranslator ? [] : null,
comments: options.comment === true ? [] : null,
impliedStrict: ecmaFeatures.impliedStrict === true && this.options.ecmaVersion >= 5,
ecmaVersion: this.options.ecmaVersion,
jsxAttrValueToken: false,
lastToken: null,
templateElements: []
};
this[STATE] = state;
}

tokenize() {
Expand Down
17 changes: 9 additions & 8 deletions lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
const SUPPORTED_VERSIONS = [
3,
5,
6,
7,
8,
9,
10,
11,
12,
13
6, // 2015
7, // 2016
8, // 2017
9, // 2018
10, // 2019
11, // 2020
12, // 2021
13, // 2022
14 // 2023
];

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
"index": 15,
"lineNumber": 2,
"column": 2,
"message": "Unexpected character '!'"
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
'use strict';
#!/usr/bin/env node
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export default {
"type": "Program",
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 0
}
},
"range": [
0,
20
],
"body": [],
"sourceType": "script",
"tokens": []
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#!/usr/bin/env node
4 changes: 2 additions & 2 deletions tests/lib/supported-ecmaversions.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ import * as espree from "../../espree.js";

describe("latestEcmaVersion", () => {
it("should return the latest supported ecmaVersion", () => {
assert.strictEqual(espree.latestEcmaVersion, 13);
assert.strictEqual(espree.latestEcmaVersion, 14);
});
});

describe("supportedEcmaVersions", () => {
it("should return an array of all supported versions", () => {
assert.deepStrictEqual(
espree.supportedEcmaVersions,
[3, 5, 6, 7, 8, 9, 10, 11, 12, 13]
[3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
);
});
});