Skip to content

Commit

Permalink
Fix type validation error in tag-utils.js
Browse files Browse the repository at this point in the history
  • Loading branch information
marklundin committed Sep 16, 2024
1 parent c74bda5 commit 876416d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
44 changes: 27 additions & 17 deletions src/utils/tag-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*

Check failure on line 38 in src/utils/tag-utils.js

View workflow job for this annotation

GitHub Actions / Unit Test

Trailing spaces not allowed
* @param value - The value to be validated.

Check failure on line 39 in src/utils/tag-utils.js

View workflow job for this annotation

GitHub Actions / Unit Test

Missing JSDoc @param "value" type
* @param typeAnnotation - The TypeScript type annotation as a string.

Check failure on line 40 in src/utils/tag-utils.js

View workflow job for this annotation

GitHub Actions / Unit Test

Missing JSDoc @param "typeAnnotation" type
* @param env - The environment containing the language service and file creation utilities.

Check failure on line 41 in src/utils/tag-utils.js

View workflow job for this annotation

GitHub Actions / Unit Test

Missing JSDoc @param "env" type
* @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

View workflow job for this annotation

GitHub Actions / Unit Test

Missing JSDoc @returns type
*/
export function validateTag(value, typeAnnotation, env) {

const virtualFileName = "/___virtual__.ts";

Check failure on line 46 in src/utils/tag-utils.js

View workflow job for this annotation

GitHub Actions / Unit Test

Strings must use singlequote
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

Check failure on line 60 in src/utils/tag-utils.js

View workflow job for this annotation

GitHub Actions / Unit Test

'ts' is not defined
);

// 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

Check failure on line 66 in src/utils/tag-utils.js

View workflow job for this annotation

GitHub Actions / Unit Test

'ts' is not defined
? flattenDiagnosticMessageText(typeErrors[0].messageText, "\n")

Check failure on line 67 in src/utils/tag-utils.js

View workflow job for this annotation

GitHub Actions / Unit Test

'?' should be placed at the end of the line

Check failure on line 67 in src/utils/tag-utils.js

View workflow job for this annotation

GitHub Actions / Unit Test

'flattenDiagnosticMessageText' is not defined
: 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;
}
8 changes: 6 additions & 2 deletions test/fixtures/program.valid.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ export const MyEnum = { value: 0 };
class Example extends Script {
/**
* @attribute
* @type {boolean}
* @precision 1
* @type {number}
*/
a = false;
a;

initialize() {
confetti();
new TWEEN.Tween({ x: 0 }).to({ x: 100 }, 1000).start();

// This is an intentional type error, but the parser should ignore these
this.a = 'string';
}
}

Expand Down
1 change: 1 addition & 0 deletions test/tests/valid/program.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ describe('VALID: Program ', function () {
expect(data).to.exist;
expect(data[0]).to.not.be.empty;
expect(data[1]).to.be.empty;
expect(data[0].example.errors).to.be.empty;
});
});

0 comments on commit 876416d

Please sign in to comment.