Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cst

Package Overview
Dependencies
Maintainers
1
Versions
54
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cst - npm Package Compare versions

Comparing version 0.0.4 to 0.0.5

74

lib/elements/Element.js

@@ -13,2 +13,4 @@ 'use strict';

var _utilsLines = require('../utils/lines');
var _ElementAssert = require('./ElementAssert');

@@ -747,2 +749,46 @@

}, {
key: 'loc',
/**
* Calculates and returns Element loc.
*
* @returns {Object}
*/
get: function get() {
var prevToken = this.previousToken;
var startColumn = 0;
var startLine = 1;
while (prevToken) {
var lines = prevToken.sourceCodeLines;
startColumn += lines[lines.length - 1].length;
if (lines.length > 1) {
while (prevToken) {
startLine += prevToken.newlineCount;
prevToken = prevToken.previousToken;
}
break;
}
prevToken = prevToken.previousToken;
}
var elementLines = this.sourceCodeLines;
var endLine = startLine + elementLines.length - 1;
var endColumn = elementLines[elementLines.length - 1].length;
if (startLine === endLine) {
endColumn += startColumn;
}
return {
start: {
line: startLine,
column: startColumn
},
end: {
line: endLine,
column: endColumn
}
};
}
}, {
key: 'sourceCodeLength',

@@ -783,2 +829,30 @@

}
}, {
key: 'sourceCodeLines',
/**
* Generated source code lines.
*
* @returns {String[]}
*/
get: function get() {
return (0, _utilsLines.getLines)(this.sourceCode);
}
}, {
key: 'newlineCount',
/**
* Generated source code line break count.
*
* @returns {Number}
*/
get: function get() {
var count = 0;
var child = this._firstChild;
while (child) {
count += child.newlineCount;
child = child._nextSibling;
}
return count;
}
}]);

@@ -785,0 +859,0 @@

29

lib/elements/ElementAssert.js

@@ -216,2 +216,17 @@ /**

}, {
key: 'isStatement',
/**
* Checks if the current element is a statement.
*
* @returns {Boolean}
*/
value: function isStatement() {
var _ref9 = this._currentElement || {};
var isStatement = _ref9.isStatement;
return isStatement;
}
}, {
key: 'passToken',

@@ -348,6 +363,6 @@

while (true) {
var _ref9 = this._currentElement || {};
var _ref10 = this._currentElement || {};
var isWhitespace = _ref9.isWhitespace;
var isComment = _ref9.isComment;
var isWhitespace = _ref10.isWhitespace;
var isComment = _ref10.isComment;

@@ -368,6 +383,6 @@ if (!isWhitespace && !isComment) {

while (true) {
var _ref10 = this._currentElement || {};
var _ref11 = this._currentElement || {};
var isWhitespace = _ref10.isWhitespace;
var isComment = _ref10.isComment;
var isWhitespace = _ref11.isWhitespace;
var isComment = _ref11.isComment;

@@ -378,3 +393,3 @@ if (!isWhitespace && !isComment) {

if (this._currentElement.lineBreakCount > 0) {
if (this._currentElement.newlineCount > 0) {
break;

@@ -381,0 +396,0 @@ }

@@ -33,7 +33,5 @@ 'use strict';

case 'LineComment':
code = '//' + code;
isComment = true;
break;
case 'BlockComment':
code = '/*' + code + '*/';
isComment = true;

@@ -48,2 +46,3 @@ break;

this._sourceCodeLength = code.length;
this._sourceCodeLines = (0, _utilsLines.getLines)(code);
this._isComment = isComment;

@@ -116,9 +115,11 @@ this._isWhitespace = isWhitespace;

}, {
key: 'lineBreakCount',
key: 'sourceCodeLines',
get: function get() {
if (this._newLines === undefined) {
this._newLines = (0, _utilsLines.countLineBreaks)(this._sourceCode);
}
return this._newLines;
return this._sourceCodeLines.concat();
}
}, {
key: 'newlineCount',
get: function get() {
return this._sourceCodeLines.length - 1;
}
}]);

@@ -125,0 +126,0 @@

@@ -44,3 +44,3 @@ 'use strict';

var body = [];
while (!children.isEnd) {
while (children.isStatement()) {
body.push(children.passStatement());

@@ -50,2 +50,3 @@ children.skipNonCode();

children.passToken('EOF');
children.assertEnd();

@@ -52,0 +53,0 @@

@@ -120,14 +120,10 @@ 'use strict';

function buildElementTree(ast, tokens) {
if (tokens.length > 0) {
var firstToken = tokens[0];
ast.start = firstToken.start;
ast.end = tokens[tokens.length - 1].end;
return buildElementTreeItem(ast, {
tokens: tokens,
token: firstToken,
pos: 0
});
} else {
return new _elementsElementIndex2['default'].Program([]);
}
var firstToken = tokens[0];
ast.start = firstToken.start;
ast.end = tokens[tokens.length - 1].end;
return buildElementTreeItem(ast, {
tokens: tokens,
token: firstToken,
pos: 0
});
}

@@ -141,6 +137,7 @@

function buildElementTreeItem(ast, state) {
var childProps = visitorKeys[ast.type];
var elementType = ast.type;
var childProps = visitorKeys[elementType];
if (!childProps) {
throw new Error('Cannot iterate using ' + ast.type);
throw new Error('Cannot iterate using ' + elementType);
}

@@ -150,3 +147,3 @@

// the next case or till the end of the switch statement.
if (ast.type === 'SwitchStatement') {
if (elementType === 'SwitchStatement') {
for (var i = 0; i < ast.cases.length; i++) {

@@ -179,6 +176,6 @@ var switchCase = ast.cases[i];

var NodeClass = _elementsElementIndex2['default'][ast.type];
var NodeClass = _elementsElementIndex2['default'][elementType];
if (!NodeClass) {
throw new Error('Cannot create ' + ast.type + ' instance');
throw new Error('Cannot create ' + elementType + ' instance');
}

@@ -195,3 +192,3 @@

if (!state.token || state.token.start === end) {
if (!state.token || state.token.start === end && (state.token.end !== end || elementType !== 'Program')) {
return new NodeClass(children);

@@ -201,8 +198,9 @@ }

var endOfAstReached = state.token.end === end;
var addedTokenType = state.token.type;
if (endOfAstReached && ast.type === 'Identifier' && state.token.type === 'Keyword') {
state.token.type = 'Identifier';
if (endOfAstReached && ast.type === 'Identifier' && addedTokenType === 'Keyword') {
addedTokenType = 'Identifier';
}
children[children.length] = new _elementsToken2['default'](state.token.type, state.token.value);
children[children.length] = new _elementsToken2['default'](addedTokenType, state.token.value);

@@ -212,2 +210,6 @@ state.pos++;

if (elementType === 'Program' && addedTokenType !== 'EOF') {
continue;
}
if (endOfAstReached) {

@@ -244,4 +246,3 @@ return new NodeClass(children);

if (commentToken && codeToken.start > commentToken.start) {
token = commentToken;
token.type += 'Comment';
token = processCommentToken(commentToken);
commentToken = commentTokens[++commentPtr];

@@ -255,4 +256,3 @@ } else {

if (commentToken) {
token = commentToken;
token.type += 'Comment';
token = processCommentToken(commentToken);
commentToken = commentTokens[++commentPtr];

@@ -333,5 +333,24 @@ } else {

token.value = source.slice(token.start, token.end);
} else if (type === tt.eof) {
token.type = 'EOF';
token.value = '';
}
return token;
}
/**
* Babel does not add // and /*..*\/ to the token value.
* Fixing this.
*
* @param {Object} token
*/
function processCommentToken(token) {
if (token.type === 'Line') {
token.value = '//' + token.value;
} else {
token.value = '/*' + token.value + '*/';
}
token.type += 'Comment';
return token;
}

@@ -55,3 +55,2 @@ 'use strict';

var ast = (0, _babelCore.parse)(code, opts);
tokens.pop();
ast.tokens = tokens;

@@ -58,0 +57,0 @@ ast.comments = comments;

@@ -6,8 +6,7 @@ "use strict";

});
exports.countLineBreaks = countLineBreaks;
var lineBreakRegex = /\r\n|\r|\n/g;
exports.getLines = getLines;
var lineBreakRegex = /\r\n|\r|\n/;
function countLineBreaks(input) {
var match = input.match(lineBreakRegex);
return match ? match.length : 0;
function getLines(input) {
return input.split(lineBreakRegex);
}
{
"name": "cst",
"version": "0.0.4",
"version": "0.0.5",
"description": "JavaScript CST Implementation",

@@ -28,3 +28,3 @@ "author": "Marat Dulin",

"devDependencies": {
"babel": "^5.5.8",
"babel": "5.5.8",
"benchmark": "^1.0.0",

@@ -41,4 +41,4 @@ "chai": "^3.0.0",

"dependencies": {
"babel-core": "^5.5.5"
"babel-core": "5.5.5"
}
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc