-
-
Save thejh/72f7acd0bbfb07a908e3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var journey = require('../') | |
, fs = require('fs') | |
, test = require('tap').test | |
, util = require('util') | |
test('locate-names', function (t) { | |
var ast = journey.makeAst(fs.readFileSync(__dirname+'/data/requires.js', 'utf8')) | |
var wanted = | |
{ baz: {line: 4, col: 12} | |
, qux: {line: 9, col: 2} | |
} | |
t.plan(Object.keys(wanted).length) | |
journey.visitAll(ast, function(node) { | |
if (node.type === 'name' && Object.keys(wanted).indexOf(node.value) > -1) { | |
t.has(node.position.startToken, wanted[node.value], JSON.stringify(node.value)+' has the correct position') | |
delete wanted[node.value] | |
} | |
}) | |
Object.keys(wanted).forEach(function(wantedKey) { | |
t.ok(false, JSON.stringify(wantedKey)+" wasn't found") | |
}) | |
}) | |
test('check whether position data is present in own source', function (t) { | |
var ast = journey.makeAst(fs.readFileSync(__dirname+'/../index.js', 'utf8')) | |
journey.updateParentData(ast) | |
var nodes = [] | |
journey.visitAll(ast, function(node) { | |
if (node.type === 'toplevel') return | |
nodes.push(node) | |
}) | |
nodes.forEach(function(node) { | |
t.ok(node.position != null, 'node with loc data (type: '+node.type+', parent type: '+node.parent.type | |
+', index in parent: '+node.parent.rawNode.indexOf(node.rawNode)+')') | |
if (node.position != null) { | |
// pos and endpos | |
if (node.position.startToken.pos <= node.position.endToken.pos) { | |
t.ok(true, 'node starts before it ends (or at the same time)') | |
} else { | |
t.ok(false, 'node starts before it ends (or at the same time): '+util.inspect(node, false, null, false)) | |
} | |
} | |
}) | |
t.end() | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment