Skip to content

Commit

Permalink
fix: support matching class body with __anl
Browse files Browse the repository at this point in the history
  • Loading branch information
3cp committed Jul 4, 2019
1 parent af2919b commit d1d215c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ function matchTerm(pattern) {
} else if (pattern.type === 'ExpressionStatement' &&
pattern.expression.type === 'Identifier') {
possible = pattern.expression.name.toString();
} else if ((pattern.type === 'FieldDefinition' || pattern.type === 'ClassProperty') &&
pattern.key.type === 'Identifier') {
possible = pattern.key.name.toString();
}

if (!possible || !possible.startsWith('__')) return;
Expand Down
2 changes: 1 addition & 1 deletion spec/cherow.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
if (!process.version.startsWith('v4')) {
const cherow = require('cherow');
const withParser = require('./with-parser');
withParser('cherow', cherow.parseScript);
withParser('cherow', code => cherow.parse(code, {module: true, next: true, experimental: true}));
}
34 changes: 34 additions & 0 deletions spec/with-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,40 @@ module.exports = function (parserName, parser) {
t.end();
});

if (parserName === 'cherow') {
testP('matcher built by astMatcher supports class body with __anl', t => {
let m = astMatcher('export class __any_name { __anl_body }');
let r = m(`
export class Foo {
name = 'ok';
bar() {}
get loo() {}
}
`);
t.equal(r.length, 1);
t.equal(r[0].match.name.name, 'Foo');
t.equal(r[0].match.body.length, 3);
t.equal(r[0].match.body[0].key.name, 'name');
t.equal(r[0].match.body[1].key.name, 'bar');
t.equal(r[0].match.body[2].key.name, 'loo');
t.end();
});

testP('matcher built by astMatcher supports class body with __anl case 2', t => {
let m = astMatcher('class __any_name { __anl }');
let r = m(`
class Foo {
name = 'ok';
bar() {}
get loo() {}
}
`);
t.equal(r.length, 1);
t.equal(r[0].match.name.name, 'Foo');
t.end();
});
}

testP('matcher built by astMatcher continues to match even after match found', t => {
let m = astMatcher('__any.__any_m()');
let r = m('a.m1().m2()');
Expand Down

0 comments on commit d1d215c

Please sign in to comment.