Skip to content

Commit

Permalink
feat: #338 - Add Diagnostic.getLineNumber()
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Jun 2, 2018
1 parent a7864f1 commit 4cba457
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 2 deletions.
5 changes: 3 additions & 2 deletions docs/setup/diagnostics.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ Source file the diagnostic occurs in:
const sourceFile = diagnostic.getSourceFile(); // returns: SourceFile | undefined
```

#### Start & length
#### Start, line number, & length

Position in the file and length of the diagnostic:
Position in the file, the line number, and length of the diagnostic:

```ts
const start = diagnostic.getStart(); // returns: number
const lineNumber = diagnostic.getLineNumber(); // returns: number
const length = diagnostic.getLength(); // returns: number
```

Expand Down
8 changes: 8 additions & 0 deletions lib/ts-simple-ast.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8203,6 +8203,10 @@ export declare class Diagnostic<TCompilerObject extends ts.Diagnostic = ts.Diagn
* Gets the message text.
*/
getMessageText(): string | DiagnosticMessageChain;
/**
* Gets the line number.
*/
getLineNumber(): number | undefined;
/**
* Gets the start.
*/
Expand All @@ -8226,6 +8230,10 @@ export declare class Diagnostic<TCompilerObject extends ts.Diagnostic = ts.Diagn
}

export declare class DiagnosticWithLocation extends Diagnostic<ts.DiagnosticWithLocation> {
/**
* Gets the line number.
*/
getLineNumber(): number;
/**
* Gets the start.
*/
Expand Down
11 changes: 11 additions & 0 deletions src/compiler/tools/results/Diagnostic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ export class Diagnostic<TCompilerObject extends ts.Diagnostic = ts.Diagnostic> {
return this.global.compilerFactory.getDiagnosticMessageChain(messageText);
}

/**
* Gets the line number.
*/
getLineNumber() {
const sourceFile = this.getSourceFile();
const start = this.getStart();
if (sourceFile == null || start == null)
return undefined;
return sourceFile.getLineNumberFromPos(start);
}

/**
* Gets the start.
*/
Expand Down
7 changes: 7 additions & 0 deletions src/compiler/tools/results/DiagnosticWithLocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ export class DiagnosticWithLocation extends Diagnostic<ts.DiagnosticWithLocation
super(global, compilerObject);
}

/**
* Gets the line number.
*/
getLineNumber() {
return super.getLineNumber()!;
}

/**
* Gets the start.
*/
Expand Down
6 changes: 6 additions & 0 deletions src/tests/compiler/tools/diagnosticTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ describe(nameof(Diagnostic), () => {
});
});

describe(nameof<Diagnostic>(d => d.getLineNumber), () => {
it("should get the line number", () => {
expect(constError.getLineNumber()).to.equal(1);
});
});

describe(nameof<Diagnostic>(d => d.getLength), () => {
it("should get the length", () => {
expect(constError.getLength()).to.equal(1);
Expand Down

0 comments on commit 4cba457

Please sign in to comment.