1+ var assert = require ( 'assert' ) ;
12var commentParser = require ( 'comment-parser' ) ;
23var TypeParser = require ( 'jsdoctypeparser' ) . Parser ;
34var TypeBuilder = require ( 'jsdoctypeparser' ) . Builder ;
@@ -8,45 +9,45 @@ TypeBuilder.ENABLE_EXCEPTIONS = true;
89
910module . exports = {
1011 /**
11- * @param {string } comment
12+ * @param {string } commentNode
1213 * @returns {DocComment }
1314 */
14- createDocComment : function ( comment ) {
15- return new DocComment ( comment ) ;
15+ createDocCommentByCommentNode : function ( commentNode ) {
16+ var loc = commentNode . loc ;
17+ var lines = [ Array ( loc . start . column + 1 ) . join ( ' ' ) , '/*' , commentNode . value , '*/' ]
18+ . join ( '' ) . split ( '\n' ) . map ( function ( v ) {
19+ return v . substr ( loc . start . column ) ;
20+ } ) ;
21+ var value = lines . join ( '\n' ) ;
22+ return new DocComment ( value , loc ) ;
1623 } ,
1724
1825 doc : DocComment ,
1926 tag : DocTag ,
20- type : DocType
27+ type : DocType ,
28+ location : DocLocation
2129} ;
2230
2331/**
2432 * jsdoc comment object
25- * @param {object } commentNode
33+ * @param {string } value
34+ * @param {{start: DocLocation} } loc
2635 * @constructor
2736 */
28- function DocComment ( commentNode ) {
29- // normalize data
30- var loc = commentNode . loc ;
31- var lines = [ Array ( loc . start . column + 1 ) . join ( ' ' ) , '/*' , commentNode . value , '*/' ]
32- . join ( '' ) . split ( '\n' ) . map ( function ( v ) {
33- return v . substr ( loc . start . column ) ;
34- } ) ;
35- var value = lines . join ( '\n' ) ;
36-
37+ function DocComment ( value , loc ) {
3738 // parse comments
3839 var _parsed = _parseComment ( value ) || { } ;
3940
4041 // fill up fields
4142 this . loc = loc ;
4243 this . value = value ;
43- this . lines = lines ;
44+ this . lines = value . split ( '\n' ) ;
4445 this . valid = _parsed . hasOwnProperty ( 'line' ) ;
4546
4647 // doc parsed data
4748 this . description = _parsed . description || null ;
4849 this . tags = ( _parsed . tags || [ ] ) . map ( function ( tag ) {
49- return new DocTag ( tag ) ;
50+ return new DocTag ( tag , new DocLocation ( tag . line , 3 , loc . start ) ) ;
5051 } ) ;
5152
5253 /**
@@ -79,9 +80,10 @@ function DocComment(commentNode) {
7980/**
8081 * Simple jsdoc tag object
8182 * @param {Object } tag
83+ * @param {DocLocation } loc
8284 * @constructor
8385 */
84- function DocTag ( tag ) {
86+ function DocTag ( tag , loc ) {
8587 this . id = tag . tag ;
8688 this . line = tag . line ;
8789 this . value = tag . value ;
@@ -90,21 +92,31 @@ function DocTag(tag) {
9092 this . optional = tag . optional ;
9193 this . default = tag . default ;
9294
95+ this . loc = loc ;
96+
9397 if ( tag . name ) {
94- this . name = tag . name ;
98+ this . name = {
99+ loc : this . loc . shift ( 0 , tag . value . indexOf ( tag . name ) ) ,
100+ value : tag . name
101+ } ;
95102 }
96103 if ( tag . type ) {
97- this . type = new DocType ( tag . type ) ;
104+ this . type = new DocType ( tag . type , this . loc . shift ( 0 , tag . value . indexOf ( tag . type ) ) ) ;
98105 }
99106}
100107
101108/**
102109 * Parses jsdoctype string and provides several methods to work with it
103110 * @param {string } type
111+ * @param {DocLocation } loc
104112 * @constructor
105113 */
106- function DocType ( type ) {
114+ function DocType ( type , loc ) {
115+ assert ( type , 'type can\'t be empty' ) ;
116+ assert ( loc , 'location should be passed' ) ;
117+
107118 this . value = type ;
119+ this . loc = loc ;
108120
109121 var parsed = _parseDocType ( type ) ;
110122 var data = parsed . valid ? _simplifyType ( parsed ) : [ ] ;
@@ -122,6 +134,35 @@ function DocType(type) {
122134 } ;
123135}
124136
137+ /**
138+ * DocLocation
139+ * @constructor
140+ * @param {Number } line
141+ * @param {Number } column
142+ * @param {(Object|DocLocation) } [rel]
143+ */
144+ function DocLocation ( line , column , rel ) {
145+ assert ( Number . isFinite ( line ) && line >= 0 , 'line should be greater than zero' ) ;
146+ assert ( Number . isFinite ( column ) && column >= 0 , 'column should be greater than zero' ) ;
147+ rel = rel || { } ;
148+ this . line = Number ( line ) + Number ( rel . line || 0 ) ;
149+ this . column = Number ( column ) + Number ( rel . column || 0 ) ;
150+ }
151+
152+ /**
153+ * Shift location by line and column
154+ * @param {Number|Object } line
155+ * @param {Number } [column]
156+ * @return {DocLocation }
157+ */
158+ DocLocation . prototype . shift = function ( line , column ) {
159+ if ( typeof line === 'object' ) {
160+ column = line . column ;
161+ line = line . line ;
162+ }
163+ return new DocLocation ( line , column , this ) ;
164+ } ;
165+
125166/**
126167 * Comment parsing helper
127168 * @param {String } comment
0 commit comments