Skip to content

Commit

Permalink
Merge pull request #270 from EricSmekens/es2021
Browse files Browse the repository at this point in the history
ES updates
  • Loading branch information
6utt3rfly authored Nov 5, 2024
2 parents 0497757 + 7f38f12 commit d8e1ead
Show file tree
Hide file tree
Showing 22 changed files with 108 additions and 47 deletions.
10 changes: 1 addition & 9 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"ignorePatterns": ["**/dist/**/*.js"],
"parserOptions": {
"ecmaVersion": 6,
"ecmaVersion": 2020,
"sourceType": "module",
"ecmaFeatures": {
"impliedStrict": true
Expand All @@ -11,14 +11,6 @@
"browser": true,
"node": true
},
"overrides": [
{
"files": ["test/**/*.js", "*.test.js"],
"parserOptions": {
"ecmaVersion": 7
}
}
],
"rules": {
"semi": 1,
"no-dupe-args": 1,
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:

strategy:
matrix:
node-version: [16.x, 18.x, 20.x]
node-version: [18.x, 20.x, 22.x]

steps:
- uses: actions/checkout@v4
Expand Down
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const parse_tree = Jsep.parse('1 + 1');
jsep.addBinaryOp("^", 10);

// Add exponentiation operator (right-to-left)
jsep.addBinaryOp('**', 11, true);
jsep.addBinaryOp('**', 11, true); // now included by default

// Add a custom @ unary operator
jsep.addUnaryOp('@');
Expand All @@ -89,6 +89,19 @@ jsep.addIdentifierChar("@");
jsep.removeIdentifierChar('@');
```

#### Custom Literals

You can add or remove additional valid literals. By default, only `true`, `false`, and `null` are defined
```javascript
// Add standard JS literals:
jsep.addLiteral('undefined', undefined);
jsep.addLiteral('Infinity', Infinity);
jsep.addLiteral('NaN', NaN);

// Remove "null" literal from default definition
jsep.removeLiteral('null');
```

### Plugins
JSEP supports defining custom hooks for extending or modifying the expression parsing.
Plugins are registered by calling `jsep.plugins.register()` with the plugin(s) as the argument(s).
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"@semantic-release/exec": "^6.0.3",
"@semantic-release/git": "^9.0.0",
"benchmark": "^2.1.4",
"docco": "^0.8.1",
"docco": "^0.9.1",
"eslint": "^7.23.0",
"http-server": "^14.1.1",
"husky": "^7.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/arrow/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"node": ">= 10.16.0"
},
"scripts": {
"build": "rollup -c ../../plugin.rollup.config.js && cp ../../package-cjs.json dist/cjs/package.json",
"build": "rollup -c ../../plugin.rollup.config.js && cp ../../package-cjs.json dist/cjs/package.json && cp ../../LICENSE ./",
"test": "cd ../../ && http-server -p 49649 --silent & node-qunit-puppeteer http://localhost:49649/packages/arrow/test/unit_tests.html",
"lint": "eslint src/**/*.js test/**/*.js"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/assignment/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"node": ">= 10.16.0"
},
"scripts": {
"build": "rollup -c ../../plugin.rollup.config.js && cp ../../package-cjs.json dist/cjs/package.json",
"build": "rollup -c ../../plugin.rollup.config.js && cp ../../package-cjs.json dist/cjs/package.json && cp ../../LICENSE ./",
"test": "cd ../../ && http-server -p 49649 --silent & node-qunit-puppeteer http://localhost:49649/packages/assignment/test/unit_tests.html",
"lint": "eslint src/**/*.js test/**/*.js"
}
Expand Down
3 changes: 3 additions & 0 deletions packages/assignment/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ const plugin = {
'&=',
'^=',
'|=',
'||=',
'&&=',
'??=',
]),
updateOperators: [PLUS_CODE, MINUS_CODE],
assignmentPrecedence: 0.9,
Expand Down
5 changes: 4 additions & 1 deletion packages/assignment/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ const { test } = QUnit;
'&=',
'^=',
'|=',
'||=',
'&&=',
'??=',
];

qunit.before(() => jsep.plugins.register(assignment));
Expand Down Expand Up @@ -234,7 +237,7 @@ const { test } = QUnit;

[
...operators
.filter(op => op !== '**=') // not supported by esprima
.filter(op => !['**=', '||=', '&&=', '??='].includes(op)) // not supported by esprima
.map(op => `a ${op} 2`),
'a++',
'++a',
Expand Down
5 changes: 4 additions & 1 deletion packages/assignment/types/tsd.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ export interface AssignmentExpression extends Expression {
| '>>>='
| '&='
| '^='
| '|=';
| '|='
| '||='
| '&&='
| '??=';
left: Expression;
right: Expression;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/async-await/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"node": ">= 10.16.0"
},
"scripts": {
"build": "rollup -c ../../plugin.rollup.config.js && cp ../../package-cjs.json dist/cjs/package.json",
"build": "rollup -c ../../plugin.rollup.config.js && cp ../../package-cjs.json dist/cjs/package.json && cp ../../LICENSE ./",
"test": "cd ../../ && http-server -p 49649 --silent & node-qunit-puppeteer http://localhost:49649/packages/async-await/test/unit_tests.html",
"lint": "eslint src/**/*.js test/**/*.js"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/comment/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"node": ">= 10.16.0"
},
"scripts": {
"build": "rollup -c ../../plugin.rollup.config.js && cp ../../package-cjs.json dist/cjs/package.json",
"build": "rollup -c ../../plugin.rollup.config.js && cp ../../package-cjs.json dist/cjs/package.json && cp ../../LICENSE ./",
"test": "cd ../../ && http-server -p 49649 --silent & node-qunit-puppeteer http://localhost:49649/packages/comment/test/unit_tests.html",
"lint": "eslint src/**/*.js test/**/*.js"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/new/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"node": ">= 10.16.0"
},
"scripts": {
"build": "rollup -c ../../plugin.rollup.config.js && cp ../../package-cjs.json dist/cjs/package.json",
"build": "rollup -c ../../plugin.rollup.config.js && cp ../../package-cjs.json dist/cjs/package.json && cp ../../LICENSE ./",
"test": "cd ../../ && http-server -p 49649 --silent & node-qunit-puppeteer http://localhost:49649/packages/new/test/unit_tests.html",
"lint": "eslint src/**/*.js test/**/*.js"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/numbers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"node": ">= 10.16.0"
},
"scripts": {
"build": "rollup -c ../../plugin.rollup.config.js && cp ../../package-cjs.json dist/cjs/package.json",
"build": "rollup -c ../../plugin.rollup.config.js && cp ../../package-cjs.json dist/cjs/package.json && cp ../../LICENSE ./",
"test": "cd ../../ && http-server -p 49649 --silent & node-qunit-puppeteer http://localhost:49649/packages/numbers/test/unit_tests.html",
"lint": "eslint src/**/*.js test/**/*.js"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/object/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"node": ">= 10.16.0"
},
"scripts": {
"build": "rollup -c ../../plugin.rollup.config.js && cp ../../package-cjs.json dist/cjs/package.json",
"build": "rollup -c ../../plugin.rollup.config.js && cp ../../package-cjs.json dist/cjs/package.json && cp ../../LICENSE ./",
"test": "cd ../../ && http-server -p 49649 --silent & node-qunit-puppeteer http://localhost:49649/packages/object/test/unit_tests.html",
"lint": "eslint src/**/*.js test/**/*.js"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/regex/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"node": ">= 10.16.0"
},
"scripts": {
"build": "rollup -c ../../plugin.rollup.config.js && cp ../../package-cjs.json dist/cjs/package.json",
"build": "rollup -c ../../plugin.rollup.config.js && cp ../../package-cjs.json dist/cjs/package.json && cp ../../LICENSE ./",
"test": "cd ../../ && http-server -p 49649 --silent & node-qunit-puppeteer http://localhost:49649/packages/regex/test/unit_tests.html",
"lint": "eslint src/**/*.js test/**/*.js"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/spread/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"node": ">= 10.16.0"
},
"scripts": {
"build": "rollup -c ../../plugin.rollup.config.js && cp ../../package-cjs.json dist/cjs/package.json",
"build": "rollup -c ../../plugin.rollup.config.js && cp ../../package-cjs.json dist/cjs/package.json && cp ../../LICENSE ./",
"test": "cd ../../ && http-server -p 49649 --silent & node-qunit-puppeteer http://localhost:49649/packages/spread/test/unit_tests.html",
"lint": "eslint src/**/*.js test/**/*.js"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"node": ">= 10.16.0"
},
"scripts": {
"build": "rollup -c ../../plugin.rollup.config.js && cp ../../package-cjs.json dist/cjs/package.json",
"build": "rollup -c ../../plugin.rollup.config.js && cp ../../package-cjs.json dist/cjs/package.json && cp ../../LICENSE ./",
"test": "cd ../../ && http-server -p 49649 --silent & node-qunit-puppeteer http://localhost:49649/packages/template/test/unit_tests.html",
"lint": "eslint src/**/*.js test/**/*.js"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/ternary/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"node": ">= 10.16.0"
},
"scripts": {
"build": "rollup -c ../../plugin.rollup.config.js && cp ../../package-cjs.json dist/cjs/package.json",
"build": "rollup -c ../../plugin.rollup.config.js && cp ../../package-cjs.json dist/cjs/package.json && cp ../../LICENSE ./",
"test": "cd ../../ && http-server -p 49649 --silent & node-qunit-puppeteer http://localhost:49649/packages/ternary/test/unit_tests.html",
"lint": "eslint src/**/*.js test/**/*.js"
}
Expand Down
32 changes: 16 additions & 16 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions src/jsep.js
Original file line number Diff line number Diff line change
Expand Up @@ -935,16 +935,18 @@ Object.assign(Jsep, {
// binary precedence for quick reference (higher number = higher precedence)
// see [Order of operations](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence)
binary_ops: {
'||': 1, '&&': 2, '|': 3, '^': 4, '&': 5,
'||': 1, '??': 1,
'&&': 2, '|': 3, '^': 4, '&': 5,
'==': 6, '!=': 6, '===': 6, '!==': 6,
'<': 7, '>': 7, '<=': 7, '>=': 7,
'<<': 8, '>>': 8, '>>>': 8,
'+': 9, '-': 9,
'*': 10, '/': 10, '%': 10
'*': 10, '/': 10, '%': 10,
'**': 11,
},

// sets specific binary_ops as right-associative
right_associative: new Set(),
right_associative: new Set(['**']),

// Additional valid identifier chars, apart from a-z, A-Z and 0-9 (except on the starting char)
additional_identifier_chars: new Set(['$', '_']),
Expand Down
50 changes: 47 additions & 3 deletions test/jsep.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,6 @@ import {testParser, testOpExpression, esprimaComparisonTest, resetJsepDefaults}
});

QUnit.module('Ops', function (qunit) {
qunit.before(() => {
jsep.addBinaryOp('**', 11, true); // ES2016, right-associative
});
qunit.after(resetJsepDefaults);

[
Expand All @@ -93,6 +90,7 @@ import {testParser, testOpExpression, esprimaComparisonTest, resetJsepDefaults}
'2 ** 3 ** 4',
'2 ** 3 ** 4 * 5 ** 6 ** 7 * (8 + 9)',
'(2 ** 3) ** 4 * (5 ** 6 ** 7) * (8 + 9)',
'null ?? 1',
].forEach(expr => QUnit.test(`Expr: ${expr}`, assert => testOpExpression(expr, assert)));
});

Expand Down Expand Up @@ -153,6 +151,51 @@ import {testParser, testOpExpression, esprimaComparisonTest, resetJsepDefaults}
jsep.removeUnaryOp('notes');
});

QUnit.test('Right-Associative Operators', function (assert) {
testParser('a ** b ** c', { // right-associative
type: 'BinaryExpression',
operator: '**',
left: {
type: 'Identifier',
name: 'a',
},
right: {
type: 'BinaryExpression',
operator: '**',
left: {
type: 'Identifier',
name: 'b',
},
right: {
type: 'Identifier',
name: 'c'
}
}
}, assert);

const expr = 'a * b * c';
testParser(expr, {
type: 'BinaryExpression',
operator: '*',
left: {
type: 'BinaryExpression',
operator: '*',
left: {
type: 'Identifier',
name: 'a'
},
right: {
type: 'Identifier',
name: 'b'
}
},
right: {
type: 'Identifier',
name: 'c',
}
}, assert);
});

QUnit.test('Custom alphanumeric operators', function (assert) {
jsep.addBinaryOp('and', 2);
testParser('a and b', {
Expand Down Expand Up @@ -231,6 +274,7 @@ import {testParser, testOpExpression, esprimaComparisonTest, resetJsepDefaults}
'*x',
'||x',
'?a:b',
'a ??',
'.',
'()()',
// '()', should throw 'unexpected )'...
Expand Down
1 change: 1 addition & 0 deletions test/test_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const binOps = {
'/': (a, b) => a / b,
'%': (a, b) => a % b,
'**': (a, b) => a ** b, // ES2016
'??': (a, b) => a ?? b, // ES2020
};

export const unOps = {
Expand Down

0 comments on commit d8e1ead

Please sign in to comment.