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

'new' plugin #150

Merged
merged 2 commits into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ Plugins are registered by calling `jsep.plugins.register()` with the plugin(s) a
#### JSEP-provided plugins:
* `jsepTernary`: Built-in by default, adds support for ternary `a ? b : c` expressions
* `jsepComment`: Adds support for ignoring comments: `a /* ignore this */ > 1 // ignore this too`
* `jsepNew`: Adds 'new' keyword support: `new Date()`
* `jsepObject`: Adds object expression support: `{ a: 1, b: { c }}`

#### How to add plugins:
Expand Down
1 change: 1 addition & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export default [
},
...[
'jsepComment',
'jsepNew',
'jsepObject',
].map(name => ({
input: `src/plugins/${name}.js`,
Expand Down
18 changes: 18 additions & 0 deletions src/plugins/jsepNew.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export default {
name: 'jsepNew',

init(jsep) {
jsep.addUnaryOp('new');

jsep.hooks.add('after-token', function gobbleNew(env) {
const node = env.node;
if (node && node.operator === 'new') {
if (!node.argument || ![jsep.CALL_EXP, jsep.MEMBER_EXP].includes(node.argument.type)) {
this.throwError('Expected new function()');
}
env.node = node.argument;
env.node.type = 'NewExpression';
}
});
}
};
55 changes: 55 additions & 0 deletions test/plugins/jsepNew.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import jsep from '../../src/index.js';
import jsepNew from '../../src/plugins/jsepNew.js';
import { testParser, resetJsepHooks } from '../test_utils.js';

const { test } = QUnit;

(function () {
QUnit.module('Plugin:New', (qunit) => {
qunit.before(() => jsep.plugins.register(jsepNew));
qunit.after(() => {
jsep.removeUnaryOp('new');
resetJsepHooks();
});

test('should parse basic "new" expression', (assert) => {
testParser('new Date(123)', {
type: "NewExpression",
arguments: [
{
type: "Literal",
value: 123,
raw: "123"
}
],
callee: {
type: "Identifier",
name: "Date"
}
}, assert);
});

[
'new A().b',
'new A() != null',
'new A(), new B()',
'[new A(), new A()]',
'new A("1")',
'new A(1, 2)',
].forEach(expr => test(`should not throw any errors for ${expr}`, (assert) => {
testParser(expr, {}, assert);
}));

[
'new A',
'new A,new B',
'fn(new A)',
'!new A',
'new 123',
'new (a > 2 ? A : B)',
'new (a > 1)',
].forEach(expr => test(`should give an error for invalid expression ${expr}`, (assert) => {
assert.throws(() => jsep(expr));
}));
});
}());
1 change: 1 addition & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export * from './hooks.test.js';
export * from './plugins.test.js';
export * from './plugins/jsepTernary.test.js';
export * from './plugins/jsepComment.test.js';
export * from './plugins/jsepNew.test.js';
export * from './plugins/jsepObject.test.js';