chenyc
2025-12-09 545c24c6a711d71b65f3d4e8122fee3837fb1edc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"use strict";
 
var ensureString = require("type/string/ensure")
  , isValue      = require("type/value/is")
  , esniff       = require("./");
 
module.exports = function (name/*, options*/) {
    var options = Object(arguments[1])
      , asProperty = options.asProperty
      , asPlain = isValue(options.asPlain) ? options.asPlain : true;
    var length, names;
    name = ensureString(name);
    names = name.split(".").map(function (prop) {
        prop = prop.trim();
        if (!prop) throw new TypeError(name + " is not valid function name");
        return prop;
    });
    length = names.length;
    return function (code) {
        code = ensureString(code);
        return esniff(code, function (emitter) {
            emitter.on("trigger:" + names[0][0], function (accessor) {
                if (accessor.previousToken === ".") {
                    if (!asProperty) return;
                } else if (!asPlain) {
                    return;
                }
                for (var i = 0, propertyName; (propertyName = names[i]); ++i) {
                    if (!accessor.skipCodePart(propertyName)) return;
                    accessor.skipWhitespace();
                    if (i < length - 1) {
                        if (!accessor.skipCodePart(".")) return;
                        accessor.skipWhitespace();
                    }
                }
                accessor.collectScope();
            });
        });
    };
};