Skip to content

Commit 6dfaedb

Browse files
committed
handle position nodes
1 parent 3e42e36 commit 6dfaedb

3 files changed

Lines changed: 30 additions & 45 deletions

File tree

src/node.js

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ var comment = require('./comment');
1010

1111
/**
1212
* Generic node object (inherited by all objects)
13-
*
13+
*
1414
* @public @constructor {node}
1515
* @param {node} parent The parent node
1616
* @param {array} ast The current AST node
17-
*
17+
*
1818
* @property {node} parent Parent node instance
1919
* @property {relation[]} relations {@link RELATION.md|:link:} List of linked nodes
2020
* @property {position|null} position {@link POSITION.md|:link:} Current node position
@@ -32,24 +32,8 @@ var node = function(parent, ast) {
3232
this.relations = [];
3333

3434
// check if contains a position node
35-
if (ast[0] === 'position') {
36-
this.position = new position(ast);
37-
ast = ast[3];
38-
}
39-
// check if doc block with inner component
40-
if (ast[0] === 'doc' && ast.length === 3) {
41-
this.doc = comment.create(this, ast);
42-
ast = ast[2];
43-
if (this.position) {
44-
// attach position node to comment
45-
this.doc.position = this.position;
46-
if (ast[0] === 'position') {
47-
this.position = new position(ast);
48-
ast = ast[3];
49-
} else {
50-
delete this.position;
51-
}
52-
}
35+
if (ast.loc) {
36+
this.position = new position(ast.loc);
5337
}
5438

5539
if (this.type !== 'file') {
@@ -223,30 +207,30 @@ node.prototype.export = function() {
223207

224208
/**
225209
* Use this function to extend a node into a specific object.
226-
*
210+
*
227211
* In order to make the cache wording automatically, the class
228212
* name must be the same as the filename `foo` class into `./foo.js`
229-
*
213+
*
230214
* *WARNING* : It you pass a constructor, make sur it's named in order to
231215
* automatically retrieve it's classname (used by the caching system)
232-
*
216+
*
233217
* @public
234218
* @param {constructor|string} ctor Define the named constructor, or the class name
235219
* @return {constructor}
236-
*
220+
*
237221
* @example Create with a constructor
238222
* var block = require('./block');
239223
* var child = block.extends(function className(parent, ast) {
240224
* block.apply(this, [parent, ast]);
241225
* // customized init code
242226
* });
243227
* child.prototype.foo = function() ...
244-
*
228+
*
245229
* @example Create with a generic class name
246230
* var block = require('./block');
247231
* var child = block.extends('className');
248232
* child.prototype.foo = function() ...
249-
*
233+
*
250234
*/
251235
node.extends = declareExtends(node);
252236

@@ -296,4 +280,4 @@ function declareExtends(base) {
296280
}
297281
;
298282

299-
module.exports = node;
283+
module.exports = node;

src/position.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,19 @@
33
* @constructor
44
*/
55
var position = function(node) {
6-
if (node[0] !== 'position') {
7-
throw new Error('Bad node type');
8-
}
96
this.start = {
10-
line: node[1][0],
11-
column: node[1][1]
7+
line: node.start.line,
8+
column: node.start.column
129
}
1310
this.end = {
14-
line: node[2][0],
15-
column: node[2][1]
11+
line: node.end.line,
12+
column: node.end.column
1613
}
1714
this.offset = {
18-
start: node[1][2],
19-
end: node[2][2]
15+
start: node.start.offset,
16+
end: node.end.offset
2017
};
18+
console.log(this);
2119
};
2220

2321
/**
@@ -36,4 +34,4 @@ position.prototype.hit = function(offset) {
3634
return offset >= this.offset.start && offset <= this.offset.end;
3735
}
3836

39-
module.exports = position;
37+
module.exports = position;

src/repository.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ var repository = function(directory, options) {
106106
util.inherits(repository, EventEmitter);
107107

108108
/**
109-
* Starts to read a file in order to parse it. This event is emited from
109+
* Starts to read a file in order to parse it. This event is emited from
110110
* parse or refresh methods.
111-
*
111+
*
112112
* @event repository#read
113113
* @type {object}
114114
* @property {string} name - The filename that will be parsed
@@ -117,24 +117,24 @@ util.inherits(repository, EventEmitter);
117117

118118
/**
119119
* Cache hit event, file is already updated
120-
*
120+
*
121121
* @event repository#cache
122122
* @type {object}
123123
* @property {string} name - The filename that was found in cache
124124
*/
125125

126126
/**
127127
* The specified file is parsed.
128-
*
128+
*
129129
* @event repository#parse
130130
* @type {object}
131131
* @property {string} name - The filename that will be parsed
132132
* @property {file} file - The file
133133
*/
134134

135135
/**
136-
*
137-
*
136+
*
137+
*
138138
* @event repository#error
139139
* @type {object}
140140
* @property {string} name - The filename that triggered the error
@@ -304,17 +304,20 @@ repository.prototype.parse = function(filename, encoding, stat) {
304304
fs.readFile(
305305
path.resolve(self.directory, filename),
306306
encoding, function(err, data) {
307+
var ast;
307308
if (!err) {
308309
try {
309310
var reader = new parser({
311+
ast: {
312+
withPositions: true
313+
},
310314
parser: {
311-
locations: true,
312315
extractDoc: true,
313316
suppressErrors: true
314317
}
315318
});
316319
data = data.toString(encoding);
317-
var ast = reader.parseCode(data);
320+
ast = reader.parseCode(data);
318321
} catch (e) {
319322
err = e;
320323
}

0 commit comments

Comments
 (0)