2022-07-22 20:31:08 +00:00
|
|
|
import { DataTree, DataTreeEntity } from "entities/DataTree/dataTreeFactory";
|
|
|
|
|
import {
|
|
|
|
|
getEntityNameAndPropertyPath,
|
|
|
|
|
isAction,
|
|
|
|
|
isATriggerPath,
|
|
|
|
|
isJSAction,
|
|
|
|
|
isWidget,
|
|
|
|
|
} from "workers/evaluationUtils";
|
2021-10-05 13:52:27 +00:00
|
|
|
import { Position } from "codemirror";
|
|
|
|
|
import {
|
|
|
|
|
EvaluationError,
|
|
|
|
|
extraLibraries,
|
2022-07-22 20:31:08 +00:00
|
|
|
isDynamicValue,
|
|
|
|
|
isPathADynamicBinding,
|
2021-10-05 13:52:27 +00:00
|
|
|
PropertyEvaluationErrorType,
|
|
|
|
|
} from "utils/DynamicBindingUtils";
|
2022-07-21 13:01:23 +00:00
|
|
|
import { JSHINT as jshint, LintError, LintOptions } from "jshint";
|
2022-07-22 20:31:08 +00:00
|
|
|
import { get, isEmpty, isNumber, keys, last } from "lodash";
|
2021-10-07 12:33:15 +00:00
|
|
|
import {
|
|
|
|
|
EvaluationScripts,
|
|
|
|
|
EvaluationScriptType,
|
|
|
|
|
ScriptTemplate,
|
|
|
|
|
} from "workers/evaluate";
|
2022-07-21 13:01:23 +00:00
|
|
|
import {
|
|
|
|
|
getLintErrorMessage,
|
|
|
|
|
getLintSeverity,
|
|
|
|
|
} from "components/editorComponents/CodeEditor/lintHelpers";
|
2022-08-23 11:09:42 +00:00
|
|
|
import { ECMA_VERSION } from "@shared/ast";
|
2022-08-03 14:35:33 +00:00
|
|
|
import {
|
|
|
|
|
IGNORED_LINT_ERRORS,
|
|
|
|
|
SUPPORTED_WEB_APIS,
|
|
|
|
|
} from "components/editorComponents/CodeEditor/constants";
|
2021-10-07 12:33:15 +00:00
|
|
|
|
2022-07-22 20:31:08 +00:00
|
|
|
export const pathRequiresLinting = (
|
|
|
|
|
dataTree: DataTree,
|
|
|
|
|
entity: DataTreeEntity,
|
|
|
|
|
fullPropertyPath: string,
|
|
|
|
|
): boolean => {
|
|
|
|
|
const { propertyPath } = getEntityNameAndPropertyPath(fullPropertyPath);
|
|
|
|
|
const unEvalPropertyValue = (get(
|
|
|
|
|
dataTree,
|
|
|
|
|
fullPropertyPath,
|
|
|
|
|
) as unknown) as string;
|
|
|
|
|
|
|
|
|
|
if (isATriggerPath(entity, propertyPath)) {
|
|
|
|
|
return isDynamicValue(unEvalPropertyValue);
|
|
|
|
|
}
|
|
|
|
|
const isADynamicBindingPath =
|
|
|
|
|
(isAction(entity) || isWidget(entity) || isJSAction(entity)) &&
|
|
|
|
|
isPathADynamicBinding(entity, propertyPath);
|
|
|
|
|
const requiresLinting =
|
|
|
|
|
isADynamicBindingPath &&
|
|
|
|
|
(isDynamicValue(unEvalPropertyValue) ||
|
|
|
|
|
(isJSAction(entity) && propertyPath === "body"));
|
|
|
|
|
return requiresLinting;
|
|
|
|
|
};
|
|
|
|
|
|
2022-09-14 08:35:29 +00:00
|
|
|
// Removes "export default" statement from js Object
|
|
|
|
|
export const getJSToLint = (
|
2022-07-22 20:31:08 +00:00
|
|
|
entity: DataTreeEntity,
|
|
|
|
|
snippet: string,
|
|
|
|
|
propertyPath: string,
|
|
|
|
|
) => {
|
|
|
|
|
return entity && isJSAction(entity) && propertyPath === "body"
|
|
|
|
|
? snippet.replace(/export default/g, "")
|
|
|
|
|
: snippet;
|
|
|
|
|
};
|
|
|
|
|
|
2021-10-07 12:33:15 +00:00
|
|
|
export const getPositionInEvaluationScript = (
|
|
|
|
|
type: EvaluationScriptType,
|
|
|
|
|
): Position => {
|
|
|
|
|
const script = EvaluationScripts[type];
|
|
|
|
|
|
|
|
|
|
const index = script.indexOf(ScriptTemplate);
|
2022-04-13 07:00:38 +00:00
|
|
|
const substr = script.slice(0, index !== -1 ? index : 0);
|
2021-10-07 12:33:15 +00:00
|
|
|
const lines = substr.split("\n");
|
|
|
|
|
const lastLine = last(lines) || "";
|
|
|
|
|
|
|
|
|
|
return { line: lines.length, ch: lastLine.length };
|
|
|
|
|
};
|
|
|
|
|
|
2021-12-14 08:30:43 +00:00
|
|
|
const EvaluationScriptPositions: Record<string, Position> = {};
|
2021-10-07 12:33:15 +00:00
|
|
|
|
|
|
|
|
function getEvaluationScriptPosition(scriptType: EvaluationScriptType) {
|
2021-12-14 08:30:43 +00:00
|
|
|
if (isEmpty(EvaluationScriptPositions)) {
|
2021-10-07 12:33:15 +00:00
|
|
|
// We are computing position of <<script>> in our templates.
|
|
|
|
|
// This will be used to get the exact location of error in linting
|
|
|
|
|
keys(EvaluationScripts).forEach((type) => {
|
2021-12-14 08:30:43 +00:00
|
|
|
EvaluationScriptPositions[type] = getPositionInEvaluationScript(
|
2021-10-07 12:33:15 +00:00
|
|
|
type as EvaluationScriptType,
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-14 08:30:43 +00:00
|
|
|
return EvaluationScriptPositions[scriptType];
|
2021-10-07 12:33:15 +00:00
|
|
|
}
|
2021-10-05 13:52:27 +00:00
|
|
|
|
|
|
|
|
export const getLintingErrors = (
|
|
|
|
|
script: string,
|
|
|
|
|
data: Record<string, unknown>,
|
2022-09-14 08:35:29 +00:00
|
|
|
// {{user's code}}
|
2021-10-05 13:52:27 +00:00
|
|
|
originalBinding: string,
|
|
|
|
|
scriptType: EvaluationScriptType,
|
|
|
|
|
): EvaluationError[] => {
|
2021-10-07 12:33:15 +00:00
|
|
|
const scriptPos = getEvaluationScriptPosition(scriptType);
|
2021-10-05 13:52:27 +00:00
|
|
|
const globalData: Record<string, boolean> = {};
|
|
|
|
|
for (const dataKey in data) {
|
|
|
|
|
globalData[dataKey] = true;
|
|
|
|
|
}
|
|
|
|
|
// Jshint shouldn't throw errors for additional libraries
|
|
|
|
|
extraLibraries.forEach((lib) => (globalData[lib.accessor] = true));
|
2022-08-03 14:35:33 +00:00
|
|
|
// JSHint shouldn't throw errors for supported web apis
|
|
|
|
|
Object.keys(SUPPORTED_WEB_APIS).forEach(
|
|
|
|
|
(apiName) => (globalData[apiName] = true),
|
|
|
|
|
);
|
2021-10-05 13:52:27 +00:00
|
|
|
|
2022-07-21 13:01:23 +00:00
|
|
|
const options: LintOptions = {
|
2021-10-05 13:52:27 +00:00
|
|
|
indent: 2,
|
2021-12-02 10:03:43 +00:00
|
|
|
esversion: ECMA_VERSION,
|
2021-10-05 13:52:27 +00:00
|
|
|
eqeqeq: false, // Not necessary to use ===
|
|
|
|
|
curly: false, // Blocks can be added without {}, eg if (x) return true
|
|
|
|
|
freeze: true, // Overriding inbuilt classes like Array is not allowed
|
|
|
|
|
undef: true, // Undefined variables should be reported as error
|
|
|
|
|
forin: false, // Doesn't require filtering for..in loops with obj.hasOwnProperty()
|
|
|
|
|
noempty: false, // Empty blocks are allowed
|
|
|
|
|
strict: false, // We won't force strict mode
|
2022-01-28 08:18:19 +00:00
|
|
|
unused: "strict", // Unused variables are not allowed
|
2021-10-05 13:52:27 +00:00
|
|
|
asi: true, // Tolerate Automatic Semicolon Insertion (no semicolons)
|
|
|
|
|
boss: true, // Tolerate assignments where comparisons would be expected
|
|
|
|
|
evil: false, // Use of eval not allowed
|
|
|
|
|
funcscope: true, // Tolerate variable definition inside control statements
|
|
|
|
|
sub: true, // Don't force dot notation
|
2022-02-23 16:19:44 +00:00
|
|
|
expr: true, // suppresses warnings about the use of expressions where normally you would expect to see assignments or function calls
|
2021-10-05 13:52:27 +00:00
|
|
|
// environments
|
|
|
|
|
browser: true,
|
|
|
|
|
worker: true,
|
|
|
|
|
mocha: false,
|
|
|
|
|
// global values
|
|
|
|
|
globals: globalData,
|
2022-03-14 05:27:52 +00:00
|
|
|
loopfunc: true,
|
2021-10-05 13:52:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
jshint(script, options);
|
2022-07-22 20:31:08 +00:00
|
|
|
|
2022-07-21 13:01:23 +00:00
|
|
|
return getValidLintErrors(jshint.errors, scriptPos).map((lintError) => {
|
2021-10-05 13:52:27 +00:00
|
|
|
const ch = lintError.character;
|
|
|
|
|
return {
|
|
|
|
|
errorType: PropertyEvaluationErrorType.LINT,
|
|
|
|
|
raw: script,
|
2022-01-28 08:18:19 +00:00
|
|
|
severity: getLintSeverity(lintError.code),
|
2022-07-21 13:01:23 +00:00
|
|
|
errorMessage: getLintErrorMessage(lintError.reason),
|
2021-10-05 13:52:27 +00:00
|
|
|
errorSegment: lintError.evidence,
|
|
|
|
|
originalBinding,
|
|
|
|
|
// By keeping track of these variables we can highlight the exact text that caused the error.
|
|
|
|
|
variables: [lintError.a, lintError.b, lintError.c, lintError.d],
|
|
|
|
|
code: lintError.code,
|
|
|
|
|
line: lintError.line - scriptPos.line,
|
|
|
|
|
ch: lintError.line === scriptPos.line ? ch - scriptPos.ch : ch,
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
};
|
2022-03-02 06:37:20 +00:00
|
|
|
|
2022-07-21 13:01:23 +00:00
|
|
|
const getValidLintErrors = (lintErrors: LintError[], scriptPos: Position) => {
|
|
|
|
|
return lintErrors.reduce((result: LintError[], lintError) => {
|
|
|
|
|
// Ignored errors should not be reported
|
|
|
|
|
if (IGNORED_LINT_ERRORS.includes(lintError.code)) return result;
|
|
|
|
|
/** Some error messages reference line numbers,
|
|
|
|
|
* Eg. Expected '{a}' to match '{b}' from line {c} and instead saw '{d}'
|
|
|
|
|
* these line numbers need to be re-calculated based on the binding location.
|
|
|
|
|
* Errors referencing line numbers outside the user's script should also be ignored
|
|
|
|
|
* */
|
|
|
|
|
let message = lintError.reason;
|
|
|
|
|
const matchedLines = message.match(/line \d/gi);
|
|
|
|
|
const lineNumbersInErrorMessage = new Set<number>();
|
|
|
|
|
let isInvalidErrorMessage = false;
|
|
|
|
|
if (matchedLines) {
|
|
|
|
|
matchedLines.forEach((lineStatement) => {
|
|
|
|
|
const digitString = lineStatement.split(" ")[1];
|
|
|
|
|
const digit = Number(digitString);
|
|
|
|
|
if (isNumber(digit)) {
|
|
|
|
|
if (digit < scriptPos.line) {
|
|
|
|
|
// referenced line number is outside the scope of user's script
|
|
|
|
|
isInvalidErrorMessage = true;
|
|
|
|
|
} else {
|
|
|
|
|
lineNumbersInErrorMessage.add(digit);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
if (isInvalidErrorMessage) return result;
|
|
|
|
|
if (lineNumbersInErrorMessage.size) {
|
|
|
|
|
Array.from(lineNumbersInErrorMessage).forEach((lineNumber) => {
|
|
|
|
|
message = message.replaceAll(
|
|
|
|
|
`line ${lineNumber}`,
|
|
|
|
|
`line ${lineNumber - scriptPos.line + 1}`,
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
result.push({
|
|
|
|
|
...lintError,
|
|
|
|
|
reason: message,
|
|
|
|
|
});
|
|
|
|
|
return result;
|
|
|
|
|
}, []);
|
2022-03-02 06:37:20 +00:00
|
|
|
};
|