-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix type validation error in tag-utils.js
- Loading branch information
1 parent
c74bda5
commit 876416d
Showing
3 changed files
with
34 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,31 +35,41 @@ export function parseTag(input = '') { | |
|
||
/** | ||
* Validates that a tag value matches the expected type | ||
* @throws {Error} - If the tag value is not the expected type | ||
* | ||
* @param {String} value - The string representation of the value to type check | ||
* @param {string} typeAnnotation - The expected type | ||
* @param {import('@typescript/vfs').VirtualTypeScriptEnvironment} env - The environment to validate in | ||
* @returns {boolean} - Whether the tag value is valid | ||
* | ||
* @param value - The value to be validated. | ||
Check failure on line 39 in src/utils/tag-utils.js GitHub Actions / Unit Test
|
||
* @param typeAnnotation - The TypeScript type annotation as a string. | ||
Check failure on line 40 in src/utils/tag-utils.js GitHub Actions / Unit Test
|
||
* @param env - The environment containing the language service and file creation utilities. | ||
Check failure on line 41 in src/utils/tag-utils.js GitHub Actions / Unit Test
|
||
* @throws Will throw an error if the value does not conform to the typeAnnotation. | ||
* @returns `true` if validation passes without type errors. | ||
Check failure on line 43 in src/utils/tag-utils.js GitHub Actions / Unit Test
|
||
*/ | ||
export function validateTag(value, typeAnnotation, env) { | ||
|
||
const virtualFileName = "/___virtual__.ts"; | ||
const sourceText = `let a: ${typeAnnotation} = ${value};`; | ||
env.createFile('/___virtual__.ts', sourceText); | ||
|
||
// Get the program and check for semantic errors | ||
const program = env.languageService.getProgram(); | ||
const errors = program.getSemanticDiagnostics(); | ||
// Create or overwrite the virtual file with the new source text | ||
env.createFile(virtualFileName, sourceText); | ||
|
||
// Retrieve the language service from the environment | ||
const languageService = env.languageService; | ||
|
||
// Fetch semantic diagnostics only for the virtual file | ||
const errors = languageService.getSemanticDiagnostics(virtualFileName); | ||
|
||
// Filter against the type errors we're concerned with | ||
const typeErrors = errors.filter(error => error.category === 1 && error.code === 2322); | ||
// Filter for type assignment errors (Error Code 2322: Type 'X' is not assignable to type 'Y') | ||
const typeErrors = errors.filter( | ||
error => error.code === 2322 && error.category === ts.DiagnosticCategory.Error | ||
); | ||
|
||
// return first error | ||
const typeError = typeErrors[0]; | ||
// If any type error is found, throw an error with the diagnostic message | ||
if (typeErrors.length > 0) { | ||
// TypeScript's messageText can be a string or a DiagnosticMessageChain | ||
const errorMessage = typeErrors[0].messageText instanceof ts.DiagnosticMessageChain | ||
? flattenDiagnosticMessageText(typeErrors[0].messageText, "\n") | ||
Check failure on line 67 in src/utils/tag-utils.js GitHub Actions / Unit Test
|
||
: typeErrors[0].messageText.toString(); | ||
|
||
if (typeError) { | ||
throw new Error(`${typeError.messageText}`); | ||
throw new Error(`Type Validation Error: ${errorMessage}`); | ||
} | ||
|
||
// If no type errors are found, return true indicating successful validation | ||
return true; | ||
} |
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