Skip to content

Commit

Permalink
Custom Worker - Warning when use of identifiers. (#896)
Browse files Browse the repository at this point in the history
Fix the bug where the custom worker will throw an error for the use of variables.

ex.

let temp = 100
C.PREHEAT_OVEN(temp) #ERROR

now
C.PREHEAT_OVEN(temp) #warning
  • Loading branch information
goetzrrGit authored Sep 25, 2023
1 parent 8aa593d commit d79d586
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/workers/customCodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const CustomErrorCodes = {
MAX_RELATIVE: -14,
UNBALANCED: -9,
UNCAUGHT_ARG: -3,
UNCHECK_ARG: -15,
},
/**
* InvalidAbsoluteTime error code and message.
Expand Down Expand Up @@ -186,4 +187,14 @@ Suggestion: ${balancedTime}`,
UncaughtArgumentType: (): ErrorCode => {
return { id: CustomErrorCodes.Type.UNCAUGHT_ARG, message: `Error: Command Argument Type Mismatch` };
},
/**
* uncheckable warning code and message.
*/
UncheckableArgumentType: (): ErrorCode => {
return {
id: CustomErrorCodes.Type.UNCHECK_ARG,
message: `Warning: Not able to evaluate and validate identifier.
Suggestion: Use literals when you can`,
};
},
};
33 changes: 33 additions & 0 deletions src/workers/workerHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,29 @@ export function findNextChildOfKind(
.find(child => child !== undefined);
}

/**
* Checks if a TypeScript AST node is a literal (string, number, boolean).
*
* @param {ts.Node} node - The TypeScript AST node to check.
* @returns {boolean} `true` if the node is a literal (string, number, boolean), `false` otherwise.
*
* @example
* const node = ... // some TypeScript AST node
* const result = isLiteral(node);
* console.log(result); // Outputs: true or false
*/
function isLiteral(node: tsc.Node): boolean {
switch (node.kind) {
case tsc.SyntaxKind.StringLiteral:
case tsc.SyntaxKind.NumericLiteral:
case tsc.SyntaxKind.TrueKeyword:
case tsc.SyntaxKind.FalseKeyword:
return true;
default:
return false;
}
}

/**
* Removes quotation marks from the input string.
*
Expand Down Expand Up @@ -401,6 +424,16 @@ export function validateArguments(
return false;
}

// ignore identifier as we need literals to do the checks
if (!isLiteral(argumentNode)) {
return makeDiagnostic(
CustomErrorCodes.UncheckableArgumentType(),
sourceFile,
argumentNode,
tsc.DiagnosticCategory.Warning,
);
}

let validation_resp: ValidationReturn;

switch (expected_argument.arg_type) {
Expand Down

0 comments on commit d79d586

Please sign in to comment.