This repository has been archived by the owner on Jan 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add errorOnTypeScriptSyntaticAndSemanticIssues option (#57)
- Loading branch information
1 parent
969c0d3
commit 36d2681
Showing
15 changed files
with
4,369 additions
and
951 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import ts from 'typescript'; | ||
|
||
interface SemanticOrSyntacticError extends ts.Diagnostic { | ||
message: string; | ||
} | ||
|
||
/** | ||
* By default, diagnostics from the TypeScript compiler contain all errors - regardless of whether | ||
* they are related to generic ECMAScript standards, or TypeScript-specific constructs. | ||
* | ||
* Therefore, we filter out all diagnostics, except for the ones we explicitly want to consider when | ||
* the user opts in to throwing errors on semantic issues. | ||
*/ | ||
export function getFirstSemanticOrSyntacticError( | ||
program: ts.Program, | ||
ast: ts.SourceFile | ||
): SemanticOrSyntacticError | undefined { | ||
const supportedSyntacticDiagnostics = whitelistSupportedDiagnostics( | ||
program.getSyntacticDiagnostics(ast) | ||
); | ||
if (supportedSyntacticDiagnostics.length) { | ||
return convertDiagnosticToSemanticOrSyntacticError( | ||
supportedSyntacticDiagnostics[0] | ||
); | ||
} | ||
const supportedSemanticDiagnostics = whitelistSupportedDiagnostics( | ||
program.getSemanticDiagnostics(ast) | ||
); | ||
if (supportedSemanticDiagnostics.length) { | ||
return convertDiagnosticToSemanticOrSyntacticError( | ||
supportedSemanticDiagnostics[0] | ||
); | ||
} | ||
return undefined; | ||
} | ||
|
||
function whitelistSupportedDiagnostics( | ||
diagnostics: ReadonlyArray<ts.DiagnosticWithLocation | ts.Diagnostic> | ||
): ReadonlyArray<ts.DiagnosticWithLocation | ts.Diagnostic> { | ||
return diagnostics.filter(diagnostic => { | ||
switch (diagnostic.code) { | ||
case 1123: // ts 3.2: "Variable declaration list cannot be empty." | ||
return true; | ||
} | ||
return false; | ||
}); | ||
} | ||
|
||
function convertDiagnosticToSemanticOrSyntacticError( | ||
diagnostic: ts.Diagnostic | ||
): SemanticOrSyntacticError { | ||
return { | ||
...diagnostic, | ||
message: ts.flattenDiagnosticMessageText( | ||
diagnostic.messageText, | ||
ts.sys.newLine | ||
) | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
const |
Oops, something went wrong.