Skip to content

Commit

Permalink
Add makeDiagnostic util
Browse files Browse the repository at this point in the history
  • Loading branch information
SamVerschueren committed Oct 16, 2019
1 parent 3bb25ef commit 30860fe
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
13 changes: 3 additions & 10 deletions source/lib/assertions/handlers/strict-assertion.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {TypeChecker, CallExpression} from '../../../../libraries/typescript/lib/typescript';
import {Diagnostic} from '../../interfaces';
import {makeDiagnostic} from '../../utils';

/**
* Performs strict type assertion between the argument if the assertion, and the generic type of the assertion.
Expand Down Expand Up @@ -32,18 +33,10 @@ export const strictAssertion = (checker: TypeChecker, nodes: Set<CallExpression>

if (!checker.isAssignableTo(expectedType, argumentType)) { // tslint:disable-line:early-exit
/**
* At this point, the expected type is not assignable to the argument type, but the argument type is
* The expected type is not assignable to the argument type, but the argument type is
* assignable to the expected type. This means our type is too wide.
*/
const position = node.getSourceFile().getLineAndCharacterOfPosition(node.getStart());

diagnostics.push({
fileName: node.getSourceFile().fileName,
message: `Parameter type \`${checker.typeToString(expectedType)}\` is declared too wide for argument type \`${checker.typeToString(argumentType)}\`.`,
severity: 'error',
line: position.line + 1,
column: position.character,
});
diagnostics.push(makeDiagnostic(node, `Parameter type \`${checker.typeToString(expectedType)}\` is declared too wide for argument type \`${checker.typeToString(argumentType)}\`.`));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @param property - Property to search for.
* @returns Position of the property or `undefined` if the property could not be found.
*/
export const getJSONPropertyPosition = (content: string, property: string) => {
export default (content: string, property: string) => {
const match = new RegExp(`([\\s\\S]*?)"${property}"`, 'm').exec(content);

if (!match) {
Expand Down
7 changes: 7 additions & 0 deletions source/lib/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import makeDiagnostic from './make-diagnostic';
import getJSONPropertyPosition from './get-json-property-position';

export {
getJSONPropertyPosition,
makeDiagnostic
};
21 changes: 21 additions & 0 deletions source/lib/utils/make-diagnostic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {Node} from '../../../libraries/typescript/lib/typescript';
import {Diagnostic} from '../interfaces';

/**
* Create a diagnostic from the given `node`, `message` and optional `severity`.
*
* @param node - The TypeScript Node where this diagnostic occurs.
* @param message - Message of the diagnostic.
* @param severity - Severity of the diagnostic.
*/
export default (node: Node, message: string, severity: 'error' | 'warning' = 'error'): Diagnostic => {
const position = node.getSourceFile().getLineAndCharacterOfPosition(node.getStart());

return {
fileName: node.getSourceFile().fileName,
message,
severity,
line: position.line + 1,
column: position.character,
};
};

0 comments on commit 30860fe

Please sign in to comment.