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

cdocparser

Package Overview
Dependencies
Maintainers
1
Versions
52
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cdocparser - npm Package Compare versions

Comparing version 0.3.0-pre1 to 0.3.0-pre2

test/fixtures/block.test.scss

11

index.js

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

var CommentExtractor = (function () {
var docCommentRegEx = /(?:\s*\/\/\/.*\s*)+|^[ \t]*\/\*\*((?:[^*]|[\r\n]|(?:\*+(?:[^*/]|[\r\n])))*)(\*+)\//gm;
var docCommentRegEx = /(?:\/\/\/.*\S*[\s]?)+$|^[ \t]*\/\*\*((?:[^*]|[\r\n]|(?:\*+(?:[^*/]|[\r\n])))*)(\*+)\//gm;

@@ -28,3 +28,3 @@ var cleanBlockComment = function (comment) {

if (lines[0] !== undefined && lines[0].indexOf('**') === 0){
if (lines[0] !== undefined && lines[0].indexOf('/') === 0){
lines.shift(); // Remove line with stars

@@ -34,4 +34,8 @@ type = 'poster';

var removedCommentChars = lines.join('').replace(/\n$/, '');
// Remove indention and remove last element if empty
lines = stripIndent(removedCommentChars).split('\n');
return {
lines : stripIndent(lines.join('')).match(/[^\n]+/g),
lines : lines,
type : type

@@ -56,3 +60,2 @@ };

var commentType = 'normal';
var lines;

@@ -59,0 +62,0 @@ // Detect if line comment or block comment

{
"name": "cdocparser",
"version": "0.3.0-pre1",
"version": "0.3.0-pre2",
"description": "Extract C style comments and extract context from source",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -0,120 +1,79 @@

var fs = require('fs');
var assert = require("assert");
var docParser = require('../');
describe('CDocParser', function(){
var extractor = new docParser.CommentExtractor( function (){ return { type : 'testCtx'}; } );
var extractor = new docParser.CommentExtractor( function (){ return { type : 'testCtx'}; } );
describe('CommentExtractor', function(){
var getCommentsFrom = function(file){
return extractor.extract(fs.readFileSync(__dirname + '/fixtures/'+file, 'utf-8'));
};
describe('#extract', function(){
it('should extract 1 comment', function(){
var comments = extractor.extract('/**\n* Comment \n */');
assert.equal(comments.length, 1);
assert.equal(comments[0].lines.length, 1);
assert.equal(comments[0].lines[0], 'Comment ');
});
describe('CommentExtractor', function(){
describe('#extract', function(){
describe('Block comments', function(){
it('should extract block comments', function(){
var comments = getCommentsFrom('block.test.scss');
assert.equal(comments.length, 2);
assert.equal(comments[0].lines.length, 3);
assert.equal(comments[0].context.type, 'testCtx');
assert.deepEqual(comments[0].lines, ['', 'Block comment test', '']);
assert.deepEqual(comments[1].lines, ['', 'Block comment test 2', '']);
});
it('should work for line comments', function (){
var commentsLine = extractor.extract('/// Comment ');
assert.equal(commentsLine.length, 1);
assert.equal(commentsLine[0].lines.length, 1);
assert.equal(commentsLine[0].lines[0], 'Comment ');
it('should not split every "*"', function(){
var comments = getCommentsFrom('blockSplitCorrect.test.scss');
assert.deepEqual(comments[0].lines, [ '@param {*} $list - list to purge', 'Test', 'Test']);
});
var commentsLineMultiple = extractor.extract('/// Comment\n/// Comment 1\n/// Comment 2');
assert.equal(commentsLineMultiple.length, 1);
assert.equal(commentsLineMultiple[0].lines.length, 3);
assert.deepEqual(commentsLineMultiple[0].lines, ['Comment', 'Comment 1', 'Comment 2']);
});
it('should ignore block comments like /* comment */', function(){
var comments = getCommentsFrom('blockNested.test.scss');
assert.equal(comments.length, 2);
});
it('should work with more than one line comment', function (){
var comments = extractor.extract('/// First Comment\n [ other things] /// Second Comment\n/// Another Line');
assert.equal(comments.length, 2);
assert.equal(comments[0].lines.length, 1);
assert.equal(comments[1].lines.length, 2);
assert.deepEqual(comments[0].lines, ['First Comment']);
assert.deepEqual(comments[1].lines, ['Second Comment', 'Another Line']);
});
it('should normalize the indentation', function(){
var comments = getCommentsFrom('blockIndentation.test.scss');
assert.deepEqual(comments[0].lines, ['test', ' hello', ' world']);
});
});
it('should remove "*" in empty lines', function(){
var comments = extractor.extract('/**\n * Test\n * \n * Test\n */');
assert.deepEqual(comments[0].lines, [ "Test","","Test" ]);
});
describe('Line comments', function(){
it('should extract line comments', function (){
var comments = getCommentsFrom('line.test.scss');
assert.equal(comments.length, 2);
assert.deepEqual(comments[0].lines, ['More', '', 'than', 'one', 'line']);
assert.deepEqual(comments[1].lines, ['More', 'than', 'one','comment']);
});
it('shuld not split every "*"', function(){
var comments = extractor.extract('/**\n * @param {*} $list - list to purge\n * Test\n * \n * Test\n */');
assert.deepEqual(comments[0].lines, [ "@param {*} $list - list to purge","Test","","Test" ]);
});
it('should ignore block comments in line comments', function(){
var comments = getCommentsFrom('lineIgnoreBlock.test.scss');
assert.equal(comments.length, 1);
assert.deepEqual(comments[0].lines, ['', '/** test*/', '']);
});
it('should extract more than one comment', function(){
var comments = extractor.extract('/**\n * Comment\n */\n/**\n * Comment\n */\n/**\n* Comment\n */');
assert.equal(comments.length, 3);
});
it('should normalize the indentation', function(){
var comments = getCommentsFrom('lineIndentation.test.scss');
assert.deepEqual(comments[0].lines, ['', 'test', ' hello', ' world', '']);
});
});
it ('should ignore block comments like /* comment */', function(){
var comments = extractor.extract('/**\n* Comment\n */\n/*\n* Comment\n */\n/**\n* Comment\n */');
assert.equal(comments.length, 2);
});
describe('Mixed style comments', function(){
it('should extract comments', function (){
var comments = getCommentsFrom('mixed.test.scss');
assert.equal(comments.length, 2);
assert.deepEqual(comments[0].lines, ['', 'Block comment test', '']);
assert.deepEqual(comments[1].lines, ['', 'Line comment test', '']);
});
});
it('should include a context', function(){
var comments = extractor.extract('/**\n* Comment\n */');
assert.equal(comments.length, 1);
assert.equal(comments[0].context.type, 'testCtx');
});
it('should normalize the indentation', function(){
var comments = extractor.extract('/**\n * @example\n * @include chain(bright, ".test") {\n * color: #fff;\n * }\n */');
assert.deepEqual(comments[0].lines, ['@example', ' @include chain(bright, ".test") {', ' color: #fff;', ' }' ]);
});
});
});
describe('CommentParser', function(){
// Test comments
var comments = [
{ lines : ['test', 'test', '@test'], context : { type : 'testType1'} },
{ lines : ['test', 'test', ' @test'], context : { type : 'testType1'} },
{ lines : ['test', '@ignore ingore this', 'test', '@ignore ingore this'], context : { type : 'testType1'} },
{ lines : ['test', 'test', '@aliasTest'], context : { type : 'testType2'} },
{ lines : ['test', 'test', '@test'], context : { type : 'testType2'} },
{ lines : ['test', 'test', '@test'], context : { type : 'testType3'} },
{ lines : ['test', 'test', '@flag'], context : { type : 'testType3'} },
{ lines : ['@multiline\nThis is a\nmultiline\nannotation\n'], context : { type : 'testType3'} }
];
describe('CommentParser', function(){
// Test Annotations
var annotations = {
_ : {
alias : {
"aliasTest" : "test"
}
},
ignore : {
parse : function() {}
},
test : {
parse : function ( commentLine ){
return "Working";
},
default : function(){
return "Default";
}
},
flag : {
parse : function(){
return true;
}
},
multiline : {
parse : function(commentLine){
return commentLine;
}
}
};
var parser;
describe('#parse', function(){
var parser;

@@ -125,80 +84,123 @@ beforeEach(function(){

// Test comments
var comments = [
{ lines : ['test', 'test', '@test'], context : { type : 'testType1'} },
{ lines : ['test', 'test', ' @test'], context : { type : 'testType1'} },
{ lines : ['test', '@ignore ingore this', 'test', '@ignore ingore this'], context : { type : 'testType1'} },
{ lines : ['test', 'test', '@aliasTest'], context : { type : 'testType2'} },
{ lines : ['test', 'test', '@test'], context : { type : 'testType2'} },
{ lines : ['test', 'test', '@test'], context : { type : 'testType3'} },
{ lines : ['test', 'test', '@flag'], context : { type : 'testType3'} },
{ lines : ['@multiline\nThis is a\nmultiline\nannotation\n'], context : { type : 'testType3'} }
];
it('should group comments by context type', function(){
var result = parser.parse ( comments );
assert.equal(result.testType1.length , 3);
assert.equal(result.testType2.length , 2);
assert.equal(result.testType3.length , 3);
});
// Test Annotations
var annotations = {
_ : {
alias : {
"aliasTest" : "test"
}
},
ignore : {
parse : function() {}
},
test : {
parse : function ( commentLine ){
return "Working";
},
default : function(){
return "Default";
}
},
flag : {
parse : function(){
return true;
}
},
multiline : {
parse : function(commentLine){
return commentLine;
}
}
};
it('should add default values', function(){
var result = parser.parse ( comments );
assert.equal(result.testType3[1].test[0] , 'Default' );
});
it('should join lines without annotation into description', function(){
var result = parser.parse ( comments );
assert.equal(result.testType1[0].description , 'test\ntest\n');
});
describe('#parse', function(){
it('should group comments by context type', function(){
var result = parser.parse ( comments );
assert.equal(result.testType1.length , 3);
assert.equal(result.testType2.length , 2);
assert.equal(result.testType3.length , 3);
});
it('should resolve a alias to the real name', function(){
var result = parser.parse ( comments );
assert.equal(result.testType2.length , 2);
assert.equal(result.testType2[0].test[0] , 'Working' );
});
it('should add default values', function(){
var result = parser.parse ( comments );
assert.equal(result.testType3[1].test[0] , 'Default' );
});
it('should convert an annotation that returns a boolean to a flag', function(){
var result = parser.parse ( comments );
assert.equal(result.testType3[1].flag , true );
});
it('should join lines without annotation into description', function(){
var result = parser.parse ( comments );
assert.equal(result.testType1[0].description , 'test\ntest\n');
});
it('should parse a multiline annotation', function(){
var result = parser.parse ( comments );
assert.equal(result.testType3[2].multiline[0] , "\nThis is a\nmultiline\nannotation\n");
});
it('should resolve a alias to the real name', function(){
var result = parser.parse ( comments );
assert.equal(result.testType2.length , 2);
assert.equal(result.testType2[0].test[0] , 'Working' );
});
it('should ignore annotations that aren\'t at the start of the line', function(){
var result = parser.parse ( comments );
assert.equal(result.testType1[1].test[0] , 'Default');
});
it('should convert an annotation that returns a boolean to a flag', function(){
var result = parser.parse ( comments );
assert.equal(result.testType3[1].flag , true );
});
it('should ignore annotations that won\'t return anything', function(){
var result = parser.parse ( comments );
assert.deepEqual(result.testType1[2].ignore , []);
});
it('should parse a multiline annotation', function(){
var result = parser.parse ( comments );
assert.equal(result.testType3[2].multiline[0] , "\nThis is a\nmultiline\nannotation\n");
});
it('should emit an error if annotation was not found', function(done){
parser.on('warning', function(err){
assert.equal(err + '', 'Error: Parser for annotation `notFound` not found.');
done();
it('should ignore annotations that aren\'t at the start of the line', function(){
var result = parser.parse ( comments );
assert.equal(result.testType1[1].test[0] , 'Default');
});
var result = parser.parse ( [{ lines : ['@notFound'], context : { type : 'testType1'} }] );
});
it('should apply annotations in a block poster comment to each item', function () {
var comments = extractor.extract('/**\n * Hello world\n */\n@mixin mix(){\n\n}\n\n/**\n * Poster Comment\n * @flag\n **/\n\n\n\n /**\n * Hello world\n */\n@function test(){\n\n}');
var result = parser.parse ( comments );
assert.equal(result.testCtx[0].flag , undefined);
assert.equal(result.testCtx[1].flag , true);
});
it('should ignore annotations that won\'t return anything', function(){
var result = parser.parse ( comments );
assert.deepEqual(result.testType1[2].ignore , []);
});
it('should apply annotations in a line poster comment to each item', function () {
var comments = extractor.extract('/// Hello world\n@mixin mix(){\n\n}\n///**\n/// Poster Comment\n/// @flag\n@function test(){\n\n}\n\n\n/// Hello World\n@function test(){\n\n}');
var result = parser.parse ( comments );
assert.equal(result.testCtx[0].flag , undefined);
assert.equal(result.testCtx[1].flag , true);
});
it('should emit an error if annotation was not found', function(done){
parser.on('warning', function(err){
assert.equal(err + '', 'Error: Parser for annotation `notFound` not found.');
done();
});
var result = parser.parse ( [{ lines : ['@notFound'], context : { type : 'testType1'} }] );
});
it('should apply annotations in a block poster comment to each item', function () {
var comments = getCommentsFrom('blockPoster.test.scss');
var result = parser.parse ( comments );
assert.equal(result.testCtx[0].flag , undefined);
assert.equal(result.testCtx[1].flag , true);
});
it('should emit an error if more than one poster comment was used', function(done){
var comments = extractor.extract('/**\n * Poster Comment\n **/ \n\n\n /**\n * Poster Comment\n **/');
it('should apply annotations in a line poster comment to each item', function () {
var comments = getCommentsFrom('linePoster.test.scss');
var result = parser.parse ( comments );
assert.equal(result.testCtx[0].flag , undefined);
assert.equal(result.testCtx[1].flag , true);
});
parser.on('warning', function(err){
assert.equal(err + '', 'Error: You can\'t have more than one poster comment.');
done();
it('should emit an error if more than one poster comment was used', function(done){
var comments = extractor.extract('/**\n * Poster Comment\n **/ \n\n\n /**\n * Poster Comment\n **/');
parser.on('warning', function(err){
assert.equal(err + '', 'Error: You can\'t have more than one poster comment.');
done();
});
var result = parser.parse (comments);
});
var result = parser.parse (comments);
});
});
});
});
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