| 886 | // The key difference between this and SourceNode.toStringWithSourceMap() is that SourceNodes with null line/column |
| 887 | // will not generate 'empty' mappings in the source map that point to nothing in the original TS. |
| 888 | private buildSourceMap(sourceRoot: string, rootSourceNode: SourceNode): SourceMapGenerator { |
| 889 | const map = new SourceMapGenerator({ |
| 890 | file: path.basename(this.luaFile), |
| 891 | sourceRoot, |
| 892 | }); |
| 893 | |
| 894 | let generatedLine = 1; |
| 895 | let generatedColumn = 0; |
| 896 | let currentMapping: Mapping | undefined; |
| 897 | |
| 898 | const isNewMapping = (sourceNode: SourceNode) => { |
| 899 | if (sourceNode.line === null) { |
| 900 | return false; |
| 901 | } |
| 902 | if (currentMapping === undefined) { |
| 903 | return true; |
| 904 | } |
| 905 | if ( |
| 906 | currentMapping.generated.line === generatedLine && |
| 907 | currentMapping.generated.column === generatedColumn && |
| 908 | currentMapping.name === sourceNode.name |
| 909 | ) { |
| 910 | return false; |
| 911 | } |
| 912 | return ( |
| 913 | currentMapping.original.line !== sourceNode.line || |
| 914 | currentMapping.original.column !== sourceNode.column || |
| 915 | currentMapping.name !== sourceNode.name |
| 916 | ); |
| 917 | }; |
| 918 | |
| 919 | const build = (sourceNode: SourceNode) => { |
| 920 | if (isNewMapping(sourceNode)) { |
| 921 | currentMapping = { |
| 922 | source: sourceNode.source, |
| 923 | original: { line: sourceNode.line, column: sourceNode.column }, |
| 924 | generated: { line: generatedLine, column: generatedColumn }, |
| 925 | name: sourceNode.name, |
| 926 | }; |
| 927 | map.addMapping(currentMapping); |
| 928 | } |
| 929 | |
| 930 | for (const chunk of sourceNode.children as SourceChunk[]) { |
| 931 | if (typeof chunk === "string") { |
| 932 | const lines = chunk.split("\n"); |
| 933 | if (lines.length > 1) { |
| 934 | generatedLine += lines.length - 1; |
| 935 | generatedColumn = 0; |
| 936 | currentMapping = undefined; // Mappings end at newlines |
| 937 | } |
| 938 | generatedColumn += lines[lines.length - 1].length; |
| 939 | } else { |
| 940 | build(chunk); |
| 941 | } |
| 942 | } |
| 943 | }; |
| 944 | build(rootSourceNode); |
| 945 | |