|
1 | | -import { BlockFactory, CssBlockError, ErrorWithPosition, Importer, NodeJsImporter, Preprocessors, hasErrorPosition } from "@css-blocks/core"; |
| 1 | +import { |
| 2 | + BlockFactory, |
| 3 | + CssBlockError, |
| 4 | + ErrorWithPosition, |
| 5 | + Importer, |
| 6 | + NodeJsImporter, |
| 7 | + Preprocessors, |
| 8 | + hasErrorPosition, |
| 9 | + hasMappedPosition, |
| 10 | +} from "@css-blocks/core"; |
2 | 11 | import chalk = require("chalk"); |
3 | 12 | import fse = require("fs-extra"); |
4 | 13 | import path = require("path"); |
@@ -162,32 +171,40 @@ export class CLI { |
162 | 171 | if (!hasErrorPosition(loc)) { |
163 | 172 | return; |
164 | 173 | } |
165 | | - loc.end.line = 4; |
166 | 174 | let filename = path.relative(process.cwd(), path.resolve(loc && loc.filename || blockFileRelative)); |
167 | | - let context: ExtractionResult | undefined; |
168 | | - let lineNumber: number | undefined; |
169 | | - context = extractLinesFromSource(loc, 1, 1); |
170 | | - lineNumber = loc.start.line - context.additionalLines.before; |
171 | | - if (context) { |
| 175 | + this.println("\t" + this.chalk.bold.redBright(e.origMessage)); |
| 176 | + if (hasMappedPosition(loc)) { |
172 | 177 | this.println( |
173 | | - this.chalk.bold.white("\tAt"), |
174 | | - this.chalk.bold.whiteBright(`${filename}:${loc.start.line}:${loc.start.column}`), |
175 | | - `${e.origMessage}`, |
| 178 | + this.chalk.bold.white("\tAt compiled output of"), |
| 179 | + this.chalk.bold.whiteBright(`${loc.generated.filename}:${loc.generated.start.line}:${loc.generated.start.column}`), |
176 | 180 | ); |
177 | | - for (let i = 0; i < context.lines.length; i++) { |
178 | | - let prefix; |
179 | | - let line = context.lines[i]; |
180 | | - if (i < context.additionalLines.before || |
181 | | - i >= context.lines.length - context.additionalLines.after) { |
182 | | - prefix = this.chalk.bold(`${lineNumber}: `); |
183 | | - } else { |
184 | | - prefix = this.chalk.bold.redBright(`${lineNumber}: `); |
185 | | - let {before, during, after } = this.splitLineOnErrorRange(line, lineNumber, loc); |
186 | | - line = `${before}${this.chalk.underline.redBright(during)}${after}`; |
187 | | - } |
188 | | - this.println("\t" + prefix + line); |
189 | | - if (lineNumber) lineNumber++; |
| 181 | + this.displaySnippet(extractLinesFromSource(loc.generated, 1, 1), loc.generated); |
| 182 | + } |
| 183 | + this.println( |
| 184 | + this.chalk.bold.white(hasMappedPosition(loc) ? "\tSource Mapped to" : "\tAt"), |
| 185 | + this.chalk.bold.whiteBright(`${filename}:${loc.start.line}:${loc.start.column}`), |
| 186 | + ); |
| 187 | + this.displaySnippet(extractLinesFromSource(loc, 1, 1), loc); |
| 188 | + } |
| 189 | + |
| 190 | + displaySnippet(context: ExtractionResult | undefined, loc: ErrorWithPosition) { |
| 191 | + if (!context) return; |
| 192 | + let lineNumber: number | undefined; |
| 193 | + lineNumber = loc.start.line - context.additionalLines.before; |
| 194 | + |
| 195 | + for (let i = 0; i < context.lines.length; i++) { |
| 196 | + let prefix; |
| 197 | + let line = context.lines[i]; |
| 198 | + if (i < context.additionalLines.before || |
| 199 | + i >= context.lines.length - context.additionalLines.after) { |
| 200 | + prefix = this.chalk.bold(`${lineNumber}:${line ? " " : ""}`); |
| 201 | + } else { |
| 202 | + prefix = this.chalk.bold.redBright(`${lineNumber}:${line ? " " : ""}`); |
| 203 | + let {before, during, after } = this.splitLineOnErrorRange(line, lineNumber, loc); |
| 204 | + line = `${before}${this.chalk.underline.redBright(during)}${after}`; |
190 | 205 | } |
| 206 | + this.println("\t" + prefix + line); |
| 207 | + if (lineNumber) lineNumber++; |
191 | 208 | } |
192 | 209 | } |
193 | 210 |
|
|
0 commit comments