Search in sources :

Example 11 with N4JSWorkspaceConfigSnapshot

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;
}
Also used : ASTMetaInfoCache(org.eclipse.n4js.postprocessing.ASTMetaInfoCache) Issue(org.eclipse.xtext.validation.Issue) N4JSWorkspaceConfigSnapshot(org.eclipse.n4js.workspace.N4JSWorkspaceConfigSnapshot) N4JSProjectConfigSnapshot(org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot) N4JSResource(org.eclipse.n4js.resource.N4JSResource)

Aggregations

N4JSWorkspaceConfigSnapshot (org.eclipse.n4js.workspace.N4JSWorkspaceConfigSnapshot)11 N4JSProjectConfigSnapshot (org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot)7 URI (org.eclipse.emf.common.util.URI)4 ArrayList (java.util.ArrayList)3 IResolutionAcceptor (org.eclipse.n4js.ide.imports.ReferenceResolutionFinder.IResolutionAcceptor)3 EObject (org.eclipse.emf.ecore.EObject)2 Resource (org.eclipse.emf.ecore.resource.Resource)2 N4JSResource (org.eclipse.n4js.resource.N4JSResource)2 N4JSPackageName (org.eclipse.n4js.workspace.utils.N4JSPackageName)2 IResourceDescription (org.eclipse.xtext.resource.IResourceDescription)2 IResourceDescriptions (org.eclipse.xtext.resource.IResourceDescriptions)2 Predicate (com.google.common.base.Predicate)1 Inject (com.google.inject.Inject)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 EClass (org.eclipse.emf.ecore.EClass)1