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 1 commit
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
39 changes: 22 additions & 17 deletions lib/espree.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@ export default () => Parser => {
? new TokenTranslator(tokTypes, code)
: null;

/*
* Data that is unique to Espree and is not represented internally in
* Acorn.
*/
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 +98,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]
);
});
});