diff --git a/src/getImports/packageJsonParser.js b/src/getImports/packageJsonParser.js index 7ffd7e5..25a5dbc 100644 --- a/src/getImports/packageJsonParser.js +++ b/src/getImports/packageJsonParser.js @@ -7,22 +7,38 @@ export function getPackages(fileName, source) { }); const pjson = JSON.parse(source); + const depLines = findDependecyLines(pjson.dependencies, lines); for (var dep in pjson.dependencies) { var p = {}; p.fileName = fileName; p.name = dep; - p.loc = findLoc(dep, lines); + p.loc = findLoc(dep, depLines.lines, depLines.offset); p.line = p.loc.start.line; packages.push(p); } - return packages; } -function findLoc(dep, lines) { +function findDependecyLines(dependencies, lines) { + const depStartLine = lines.find(x => x.includes('"dependencies"')); + const depLineIndex = lines.indexOf(depStartLine) + 1; + + const depLines = lines.slice( + depLineIndex, + depLineIndex + Object.keys(dependencies).length + ); + console.log(depLines); + + return { + lines: depLines, + offset: depLineIndex, + }; +} + +function findLoc(dep, lines, offset) { const line = lines.find(x => x.includes('"' + dep + '"')); - const index = lines.indexOf(line) + 1; + const index = offset + lines.indexOf(line) + 1; var loc = { start: {},