1 | var util = require("util");
|
2 | var FileSignature = require("./file-signature");
|
3 | var AstNodeTypes = require("./ast-node-types");
|
4 | var MethodSignature = require("./method-signature");
|
5 | var SlJsInfra = require("sl-js-infra").SlJsInfra;
|
6 | var SourceMapHandler = SlJsInfra.SourceMapHandler;
|
7 | var globalErrorHandler = require('../global-error-handler');
|
8 | var globalMethodIndexer = require("./globalMethodIndexer");
|
9 | var SourcePositionMapper = require("./source-position-mapper");
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | function GeneratedFileSignature() {
|
18 | FileSignature.apply(this, arguments);
|
19 | this.sourcePositionMappers = {};
|
20 | }
|
21 |
|
22 | util.inherits(GeneratedFileSignature, FileSignature);
|
23 |
|
24 | GeneratedFileSignature.prototype.addMethod = function (node, parentNode, nodeToParent) {
|
25 | var method = MethodSignature.create(node, parentNode, nodeToParent, this.absolutePath,
|
26 | this.relativePath, this.logger);
|
27 | var sourceMapData = this.getSourceMapData(method.generatedData.position, method.generatedData.endPosition);
|
28 | if (!sourceMapData) {
|
29 | this.logger.debug("Method in file '%s' at position '%s' is auto generated and ignored", this.absolutePath, method.generatedData.position);
|
30 | globalMethodIndexer.setIgnoredMethod(method.idxInMapping);
|
31 | return;
|
32 | }
|
33 | method.srcData.position = formatLocationToArray(sourceMapData.position);
|
34 | method.srcData.endPosition = formatLocationToArray(sourceMapData.endPosition);
|
35 | method.srcData.absoluteFilename = sourceMapData.absolutePath;
|
36 | method.srcData.relativeFilename = sourceMapData.relativePath;
|
37 | method.srcData.displayName = sourceMapData.displayName || method.generatedData.displayName;
|
38 | method.hasSourceData = true;
|
39 | var absoluteSourcePath = method.srcData.absoluteFilename;
|
40 | var sourcePosition = method.srcData.position;
|
41 | this.mapToSourceElement(absoluteSourcePath, sourcePosition, this.methodIndex, false);
|
42 | this.methods.push(method.createDTO());
|
43 | this.methodIndex++;
|
44 | };
|
45 |
|
46 | GeneratedFileSignature.prototype.addBranch = function (node, parentNode, isGlobalBranch, activeMethodIndex) {
|
47 | var context = this;
|
48 | var branches = [];
|
49 | if (node.type !== AstNodeTypes.LogicalExpression) {
|
50 | branches = branches.concat(this.getBranchesToAdd(node, isGlobalBranch, activeMethodIndex));
|
51 | }
|
52 |
|
53 |
|
54 | if (node.type == AstNodeTypes.LogicalExpression && (parentNode == undefined || parentNode.type !== AstNodeTypes.LogicalExpression)) {
|
55 | branches = branches.concat(this.getBranchesToAdd(node, isGlobalBranch, activeMethodIndex));
|
56 | }
|
57 |
|
58 | branches.forEach(function (branch) {
|
59 | var sourceMapData = context.getSourceMapData(branch.generatedData.position, branch.generatedData.endPosition);
|
60 | if (!sourceMapData) {
|
61 | context.logger.debug("Branch in file '%s' at position '%s' is auto generated and ignored", context.absolutePath, branch.generatedData.position);
|
62 | return;
|
63 | }
|
64 | var parentPosition = context.sourceMaps.originalPositionFor(branch.resolveParentPosition());
|
65 | branch.srcData.position = formatLocationToArray(sourceMapData.position);
|
66 | branch.srcData.endPosition = formatLocationToArray(sourceMapData.endPosition);
|
67 | branch.srcData.parentPosition = formatLocationToArray(parentPosition);
|
68 | branch.srcData.absoluteFilename = sourceMapData.absolutePath;
|
69 | branch.srcData.relativeFilename = sourceMapData.relativePath;
|
70 | if(context.isBranchInsideGeneratedMethod(branch.srcData)){
|
71 | return;
|
72 | }
|
73 | var absoluteSourcePath = branch.srcData.absoluteFilename;
|
74 | var sourcePosition = branch.srcData.position;
|
75 | context.mapToSourceElement(absoluteSourcePath, sourcePosition, context.branchIndex, true);
|
76 | branch.hasSourceData = true;
|
77 | context.branches.push(branch.createDTO());
|
78 | context.branchIndex ++;
|
79 | })
|
80 | };
|
81 |
|
82 | GeneratedFileSignature.prototype.mapToSourceElement = function(absoluteSourcePath, sourcePosition, index, isBranch) {
|
83 | this.sourcePositionMappers[absoluteSourcePath] = this.sourcePositionMappers[absoluteSourcePath] || new SourcePositionMapper(absoluteSourcePath);
|
84 | if(isBranch) {
|
85 | this.sourcePositionMappers[absoluteSourcePath].addBranch(sourcePosition, index);
|
86 | }else {
|
87 | this.sourcePositionMappers[absoluteSourcePath].addMethod(sourcePosition, index);
|
88 | }
|
89 | }
|
90 |
|
91 | GeneratedFileSignature.prototype.getSourceMapData = function (position, endPosition) {
|
92 | try {
|
93 | var handler = new SourceMapHandler(this.sourceMaps, this.absolutePath, position, endPosition, this.opts.projectRoot, this.logger);
|
94 | handler.setScmParams(this.scmFilesContainer, this.opts.scmRootDir);
|
95 | handler.resolvePaths();
|
96 | if (!handler.shouldSkipped) {
|
97 | return {
|
98 | position: handler.start,
|
99 | endPosition: handler.end,
|
100 | absolutePath: handler.absolutePath,
|
101 | relativePath: handler.relativePath,
|
102 | displayName: handler.originalName
|
103 |
|
104 | };
|
105 | } else {
|
106 | return null;
|
107 | }
|
108 | } catch (e) {
|
109 | globalErrorHandler.setLastError(e, 'Error calculating source maps:');
|
110 | this.logger.error('Error calculating source maps:' + e);
|
111 | return null;
|
112 | }
|
113 | }
|
114 |
|
115 | GeneratedFileSignature.prototype.isBranchInsideGeneratedMethod = function (srcData) {
|
116 | if (srcData && srcData.parentPosition && srcData.parentPosition[0] == null && srcData.parentPosition[1] == null) {
|
117 | this.logger.info("Branch in file '%s' at position '%s', has no parent position. seems to be inside generator or async method, skipped",
|
118 | srcData.absoluteFilename, srcData.position);
|
119 | return true;
|
120 | }
|
121 | return false;
|
122 | };
|
123 | GeneratedFileSignature.prototype.getSourceFilesPath = function () {
|
124 | return Object.keys(this.sourcePositionMappers)
|
125 | }
|
126 |
|
127 | GeneratedFileSignature.create = function (absolutePtah, relativePath, content, sourceMaps, opts, scmFilesContainer) {
|
128 | var signature = new this(absolutePtah, relativePath, content, sourceMaps, opts, scmFilesContainer);
|
129 | signature._createSignature();
|
130 | return signature;
|
131 | }
|
132 |
|
133 | function formatLocationToArray(loc) {
|
134 | return [loc.line, loc.column];
|
135 | }
|
136 |
|
137 | module.exports = GeneratedFileSignature;
|
138 |
|