UNPKG

cst

Version:

JavaScript CST Implementation

199 lines (171 loc) 6.72 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var _Element2 = require('./Element'); var _Element3 = _interopRequireDefault(_Element2); var _utilsLines = require('../utils/lines'); var Token = (function (_Element) { _inherits(Token, _Element); _createClass(Token, null, [{ key: 'create', /** * Generic token constructor. * * @param {String} type * @param {*} value */ value: function create(type, value) { return new Token(type, value, valueToSourceCode(type, value)); } /** * Creates new token using babel/acorn parser token. * * @param {{type: String, value: String, sourceCode: String}} token */ }, { key: 'createFromToken', value: function createFromToken(token) { return new Token(token.type, token.value, token.sourceCode); } /** * @param {String} type * @param {String} value * @param {String} _sourceCode private source code argument */ }]); function Token(type, value, _sourceCode) { _classCallCheck(this, Token); _get(Object.getPrototypeOf(Token.prototype), 'constructor', this).call(this, type, []); if (arguments.length === 2) { _sourceCode = valueToSourceCode(type, value); } var isComment = false; var isWhitespace = false; var isCode = true; switch (type) { case 'CommentLine': isComment = true; isCode = false; break; case 'CommentBlock': isComment = true; isCode = false; break; case 'Whitespace': isWhitespace = true; isCode = false; break; case 'AppleInstrumentationDirective': case 'GritDirective': case 'Hashbang': isCode = false; break; } this._value = value; this._sourceCode = _sourceCode; this._sourceCodeLength = _sourceCode.length; this._sourceCodeLines = (0, _utilsLines.getLines)(_sourceCode); this._isComment = isComment; this._isWhitespace = isWhitespace; this._isCode = isCode; } _createClass(Token, [{ key: '_setChildren', value: function _setChildren(newChildren) { if (newChildren.length > 0) { throw new Error('Token nodes cannot contain child nodes'); } this._childElements = newChildren; } /** * Clones current Element structure. * * @returns {Element} */ }, { key: 'cloneElement', value: function cloneElement() { return new Token(this._type, this._value, this._sourceCode); } }, { key: 'firstToken', get: function get() { return this; } }, { key: 'lastToken', get: function get() { return this; } }, { key: 'isToken', get: function get() { return true; } }, { key: 'isComment', get: function get() { return this._isComment; } }, { key: 'isCode', get: function get() { return this._isCode; } }, { key: 'isWhitespace', get: function get() { return this._isWhitespace; } }, { key: 'value', get: function get() { return this._value; } }, { key: 'sourceCode', get: function get() { return this._sourceCode; } }, { key: 'sourceCodeLength', get: function get() { return this._sourceCodeLength; } }, { key: 'sourceCodeLines', get: function get() { return this._sourceCodeLines.concat(); } }, { key: 'newlineCount', get: function get() { return this._sourceCodeLines.length - 1; } }]); return Token; })(_Element3['default']); exports['default'] = Token; function valueToSourceCode(type, value) { switch (type) { case 'CommentLine': return '//' + value; case 'CommentBlock': return '/*' + value + '*/'; case 'RegularExpression': return String(value); case 'Numeric': case 'Boolean': case 'Null': return String(value); default: return value; } } module.exports = exports['default'];