use of org.eclipse.n4js.workspace.N4JSWorkspaceConfigSnapshot in project n4js by eclipse.
the class N4JSResourceValidator method doValidateWithMeasurement.
private List<Issue> doValidateWithMeasurement(Resource resource, CheckMode mode, CancelIndicator cancelIndicator) {
// QUICK EXIT #1: in case of invalid file type (e.g. js file in a project with project type definition)
final N4JSWorkspaceConfigSnapshot ws = workspaceAccess.getWorkspaceConfig(resource);
final N4JSProjectConfigSnapshot project = ws.findProjectContaining(resource.getURI());
if (project != null && !isValidFileTypeForProjectType(resource, project)) {
final Issue issue = createInvalidFileTypeError(resource, project);
return Collections.singletonList(issue);
}
// QUICK EXIT #2: for files that match a "noValidate" module filter from package.json
if (ws.isNoValidate(resource.getURI())) {
return Collections.emptyList();
}
if (resource instanceof N4JSResource) {
final N4JSResource resourceCasted = (N4JSResource) resource;
// (pure performance tweak, because those resources have an empty AST anyway; see N4JSResource#doLoad())
if (resourceCasted.isOpaque()) {
return Collections.emptyList();
}
// QUICK EXIT #4: resources with disabled validation (e.g. d.ts files)
if (resourceCasted.isValidationDisabled()) {
return Collections.emptyList();
}
// trigger post-processing of N4JS resource (won't harm if post-processing has already taken place)
try {
resourceCasted.performPostProcessing(cancelIndicator);
} catch (Throwable th) {
// ignore this exception/error (we will create an issue for it below)
}
// QUICK EXIT #4: if post-processing failed
if (resourceCasted.isFullyProcessed() && resourceCasted.getPostProcessingThrowable() != null) {
// When getting here, we have an attempt to validate a resource that was post-processed but the
// post-processing failed (i.e. postProcessingThrowable!=null). Validating such a resource will usually
// cause a multitude of follow-up exceptions in many @Check methods (could easily be hundreds). Since,
// EMF/Xtext has the behavior of not aborting the overall validation due to an exception in a single
// @Check method, this will lead to many exceptions, which are all just follow-up issues of the problem
// that caused the post-processing to fail.
// 1) Since this is annoying and misleading, we just ignore all ordinary validation.
// 2) To not overlook the problem, we create a single validation issue (error) pointing out the problem.
// (for a test, see class AvoidFollowUpExceptionsInValidationTest)
final Throwable th = resourceCasted.getPostProcessingThrowable();
if (operationCanceledManager.isOperationCanceledException(th)) {
// do not show errors in case of cancellation
return Collections.emptyList();
}
final Issue issue = createPostProcessingFailedError(resourceCasted, th);
return Collections.singletonList(issue);
}
}
List<Issue> issues = super.validate(resource, mode, cancelIndicator);
if (resource instanceof N4JSResource) {
final N4JSResource resourceCasted = (N4JSResource) resource;
ASTMetaInfoCache cache = resourceCasted.getASTMetaInfoCache();
if (cache.hasUnknownTypeRef()) {
boolean hasErrors = issues.stream().anyMatch(issue -> issue.getSeverity() == Severity.ERROR);
if (!hasErrors) {
final String msg = IssueCodes.getMessageForTYS_UNKNOWN_TYPE_REF();
createFileIssue(resource, msg, IssueCodes.TYS_UNKNOWN_TYPE_REF);
}
}
}
return issues;
}
Aggregations