File size: 5,955 Bytes
bc20498
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import _defineProperty from "@babel/runtime/helpers/defineProperty";import _classCallCheck from "@babel/runtime/helpers/classCallCheck";import _createClass from "@babel/runtime/helpers/createClass";import _inherits from "@babel/runtime/helpers/inherits";import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";function _createForOfIteratorHelper(o, allowArrayLike) {var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];if (!it) {if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {if (it) o = it;var i = 0;var F = function F() {};return { s: F, n: function n() {if (i >= o.length) return { done: true };return { done: false, value: o[i++] };}, e: function e(_e) {throw _e;}, f: F };}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");}var normalCompletion = true,didErr = false,err;return { s: function s() {it = it.call(o);}, n: function n() {var step = it.next();normalCompletion = step.done;return step;}, e: function e(_e2) {didErr = true;err = _e2;}, f: function f() {try {if (!normalCompletion && it["return"] != null) it["return"]();} finally {if (didErr) throw err;}} };}function _unsupportedIterableToArray(o, minLen) {if (!o) return;if (typeof o === "string") return _arrayLikeToArray(o, minLen);var n = Object.prototype.toString.call(o).slice(8, -1);if (n === "Object" && o.constructor) n = o.constructor.name;if (n === "Map" || n === "Set") return Array.from(o);if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);}function _arrayLikeToArray(arr, len) {if (len == null || len > arr.length) len = arr.length;for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];return arr2;}function ownKeys(e, r) {var t = Object.keys(e);if (Object.getOwnPropertySymbols) {var o = Object.getOwnPropertySymbols(e);r && (o = o.filter(function (r) {return Object.getOwnPropertyDescriptor(e, r).enumerable;})), t.push.apply(t, o);}return t;}function _objectSpread(e) {for (var r = 1; r < arguments.length; r++) {var t = null != arguments[r] ? arguments[r] : {};r % 2 ? ownKeys(Object(t), !0).forEach(function (r) {_defineProperty(e, r, t[r]);}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));});}return e;}function _createSuper(Derived) {var hasNativeReflectConstruct = _isNativeReflectConstruct();return function _createSuperInternal() {var Super = _getPrototypeOf(Derived),result;if (hasNativeReflectConstruct) {var NewTarget = _getPrototypeOf(this).constructor;result = Reflect.construct(Super, arguments, NewTarget);} else {result = Super.apply(this, arguments);}return _possibleConstructorReturn(this, result);};}function _isNativeReflectConstruct() {try {var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));} catch (t) {}return (_isNativeReflectConstruct = function _isNativeReflectConstruct() {return !!t;})();}import path from 'path';
import EventEmitter from 'events';
import HandlebarsLexer from './lexers/handlebars-lexer.js';
import HTMLLexer from './lexers/html-lexer.js';
import JavascriptLexer from './lexers/javascript-lexer.js';
import JsxLexer from './lexers/jsx-lexer.js';

var lexers = {
  hbs: ['HandlebarsLexer'],
  handlebars: ['HandlebarsLexer'],

  htm: ['HTMLLexer'],
  html: ['HTMLLexer'],

  mjs: ['JavascriptLexer'],
  js: ['JavascriptLexer'],
  ts: ['JavascriptLexer'],
  jsx: ['JsxLexer'],
  tsx: ['JsxLexer'],

  vue: ['JavascriptLexer'],

  "default": ['JavascriptLexer']
};

var lexersMap = {
  HandlebarsLexer: HandlebarsLexer,
  HTMLLexer: HTMLLexer,
  JavascriptLexer: JavascriptLexer,
  JsxLexer: JsxLexer
};var

Parser = /*#__PURE__*/function (_EventEmitter) {_inherits(Parser, _EventEmitter);var _super = _createSuper(Parser);
  function Parser() {var _this;var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};_classCallCheck(this, Parser);
    _this = _super.call(this, options);
    _this.options = options;
    _this.lexers = _objectSpread(_objectSpread({}, lexers), options.lexers);return _this;
  }_createClass(Parser, [{ key: "parse", value:

    function parse(content, filename) {var _this2 = this;
      var keys = [];
      var extension = path.extname(filename).substr(1);
      var lexers = this.lexers[extension] || this.lexers["default"];var _iterator = _createForOfIteratorHelper(

          lexers),_step;try {for (_iterator.s(); !(_step = _iterator.n()).done;) {var lexerConfig = _step.value;
          var lexerName = void 0;
          var lexerOptions = void 0;

          if (
          typeof lexerConfig === 'string' ||
          typeof lexerConfig === 'function')
          {
            lexerName = lexerConfig;
            lexerOptions = {};
          } else {
            lexerName = lexerConfig.lexer;
            lexerOptions = lexerConfig;
          }

          var Lexer = void 0;
          if (typeof lexerName === 'function') {
            Lexer = lexerName;
          } else {
            if (!lexersMap[lexerName]) {
              this.emit('error', new Error("Lexer '".concat(lexerName, "' does not exist")));
            }

            Lexer = lexersMap[lexerName];
          }

          var lexer = new Lexer(lexerOptions);
          lexer.on('warning', function (warning) {return _this2.emit('warning', warning);});
          keys = keys.concat(lexer.extract(content, filename));
        }} catch (err) {_iterator.e(err);} finally {_iterator.f();}

      return keys;
    } }]);return Parser;}(EventEmitter);export { Parser as default };
//# sourceMappingURL=parser.js.map