diff --git a/index.js b/index.js index de02784..0d97984 100644 --- a/index.js +++ b/index.js @@ -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; diff --git a/spec/cherow.spec.js b/spec/cherow.spec.js index da27da6..b758ea6 100644 --- a/spec/cherow.spec.js +++ b/spec/cherow.spec.js @@ -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})); } diff --git a/spec/with-parser.js b/spec/with-parser.js index b246ba7..8e042bc 100644 --- a/spec/with-parser.js +++ b/spec/with-parser.js @@ -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()');