Skip to content

Commit

Permalink
feat: add support of oxc-parser
Browse files Browse the repository at this point in the history
  • Loading branch information
3cp committed May 12, 2024
1 parent 55e705c commit 54d0970
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 8 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ function extract(pattern, part) {

// if single __str_foo
} else if (term.type === STR) {
if (part.type === 'Literal') {
if (part.type === 'Literal' || part.type === 'StringLiteral') {
if (term.name) {
// get result {foo: value}
let r = {};
Expand All @@ -133,7 +133,7 @@ function extract(pattern, part) {
// if single __arr_foo
if (arrTerm.type === ARR) {
// find all or partial Literals in an array
let arr = part.filter(function(it) { return it.type === 'Literal'; })
let arr = part.filter(function(it) { return it.type === 'Literal' || it.type === 'StringLiteral'; })
.map(function(it) { return it.value; });
if (arr.length) {
if (arrTerm.name) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"espree": "^10.0.1",
"esprima": "^4.0.1",
"meriyah": "^4.4.2",
"oxc-parser": "^0.9.0",
"standard-changelog": "^6.0.0",
"tape": "^5.7.5"
}
Expand Down
3 changes: 2 additions & 1 deletion spec/all-browser-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ require('./babel-parser.spec.js');
require('./espree.spec.js');
require('./esprima.spec.js');
require('./index-exports.spec.js');
require('./meriyah.spec.js');
require('./meriyah.spec.js');
// require('./oxc-parser.spec.js');
5 changes: 5 additions & 0 deletions spec/oxc-parser.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';
const { parseSync } = require('oxc-parser');
const withParser = require('./with-parser');

withParser('oxc-parser', code => JSON.parse(parseSync(code).program));
10 changes: 5 additions & 5 deletions spec/with-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ module.exports = function (parserName, parser) {
testP('extract bare term has limited support', t => {
t.deepEqual(extractTest('__any', 'a(foo)'), {});
t.equal(extractTest('__anl', 'foo,bar'), false);
t.deepEqual(extractTest('__str', '"foo"'), {});
t.deepEqual(extractTest('__str_a', '"foo"'), {a: 'foo'});
t.deepEqual(extractTest('(__str)', '("foo")'), {});
t.deepEqual(extractTest('(__str_a)', '("foo")'), {a: 'foo'});
t.end();
});

Expand All @@ -80,14 +80,14 @@ module.exports = function (parserName, parser) {

r = extractTest('a(__any_a)', 'a("foo")');
t.deepEqual(Object.keys(r), ['a']);
t.equal(r.a.type, 'Literal');
t.ok(['Literal', 'StringLiteral'].includes(r.a.type));
t.equal(r.a.value, 'foo');

r = extractTest('a(__any_a,__any_b)', 'a("foo", "bar")');
t.deepEqual(Object.keys(r).sort(), ['a', 'b']);
t.equal(r.a.type, 'Literal');
t.ok(['Literal', 'StringLiteral'].includes(r.a.type));
t.equal(r.a.value, 'foo');
t.equal(r.b.type, 'Literal');
t.ok(['Literal', 'StringLiteral'].includes(r.b.type));
t.equal(r.b.value, 'bar');
t.end();
});
Expand Down

0 comments on commit 54d0970

Please sign in to comment.