@vkontakte/graph-cache-js
Advanced tools
Comparing version 2.0.0 to 2.0.1
@@ -129,28 +129,49 @@ const Graph = require('graphlib').Graph; | ||
function _addEdge(resolveName, g, filePath, edgePath) { | ||
return resolveName(filePath, edgePath).then(async (newName) => { | ||
const exist = await fileExist(newName); | ||
if (!exist) { | ||
const parsed = path.parse(newName); | ||
newName = parsed.dir + path.sep + parsed.name + path.sep + 'index.js'; | ||
} | ||
// we already handled this file as dep | ||
if (g.hasEdge(newName, filePath)) { | ||
return false; | ||
} | ||
if (!g.hasEdge(filePath, newName)) { | ||
g.setEdge(newName, filePath); | ||
return newName; | ||
} | ||
// this is a cyclic dep | ||
g.setEdge(newName, filePath); | ||
return false; | ||
}) | ||
} | ||
function buildTree(resolveName, ast, g, filePath) { | ||
const state = []; | ||
const addEdge = _addEdge.bind(null, resolveName, g) | ||
walk.simple(ast, { | ||
ImportDeclaration(node, state) { | ||
state.push(resolveName(filePath, node.source.value).then(async (newName) => { | ||
const exist = await fileExist(newName); | ||
state.push(addEdge(filePath, node.source.value)); | ||
}, | ||
ExpressionStatement(node, state) { | ||
const { expression } = node; | ||
if (!exist) { | ||
const parsed = path.parse(newName); | ||
newName = parsed.dir + path.sep + parsed.name + path.sep + 'index.js'; | ||
} | ||
if (node.expression.callee.name === 'require') { | ||
state.push(addEdge(filePath, expression.arguments[0].value)); | ||
} | ||
}, | ||
VariableDeclaration(node, state) { | ||
const expression = node.declarations[0].init; | ||
// we already handled this file as dep | ||
if (g.hasEdge(newName, filePath)) { | ||
return false; | ||
} | ||
if (!g.hasEdge(filePath, newName)) { | ||
g.setEdge(newName, filePath); | ||
return newName; | ||
} | ||
// this is a cyclic dep | ||
g.setEdge(newName, filePath); | ||
return false; | ||
})); | ||
if (expression.callee && expression.callee.name === 'require') { | ||
state.push(addEdge(filePath, expression.arguments[0].value)); | ||
} | ||
} | ||
@@ -157,0 +178,0 @@ }, state); |
{ | ||
"name": "@vkontakte/graph-cache-js", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -192,3 +192,21 @@ /* eslint-env mocha */ | ||
}); | ||
it('supports require statement', () => { | ||
return createGraphFromFile(createPath('test_require'), s, {}) | ||
.then((g) => verifyGraph(g, [ | ||
'test_require', 'test2' | ||
], [ | ||
{ v: 'test2', w: 'test_require' } | ||
])); | ||
}); | ||
it('supports require statement with declaration', () => { | ||
return createGraphFromFile(createPath('test_require2'), s, {}) | ||
.then((g) => verifyGraph(g, [ | ||
'test_require2', 'test2' | ||
], [ | ||
{ v: 'test2', w: 'test_require2' } | ||
])); | ||
}); | ||
}); | ||
}); |
475
15299