eclipseeclipse-plugineclipse-rcpjsdt

Suppress Errors in JavaScript validation


I'm currently developing an eclipse plugin. This plugin contains a project nature which depends on the javaScript nature of jsdt.

Now at a few details the JavaScripts that the projects of my nature can contain are somewhat special.

But at this two points the standard validation of jsdt come in and marks them as errors (which is normally right). I already managed to get this errors filtered out in the properties of the JavaScript validator (manually).

My question is, how can i exclude these errors from the validation of jsdt automatically for the projects with my nature?


Solution

  • After a lot of research, hours of deleting markers and debugging i finally managed to delete the errors i wanted. In a bad bad way of course but i've come to a point where i just wanted this to work no matter how it's done.

    If you ever want to delete existing problems that had been created during the validation process of jsdt you need to do the following (and you must not ommit anything):

    So there are two things you basicly have to care about.

    1. The actual problem markers that will be created or had already been created at the end of the validation process.

    2. The Problems created by the validation process. They are of the type CategorizedProblem and can be obtained by the ReconcileContext object that is passed to the reconcile() method.

    It seems to me that the CategorizedProblems will be translated to problem markers after the validation process.

    So what you need to do is:

    An example implementation of such a validationParticipant could look like this: (this one will filter out problems complaining about return statements outside of methods:

    [...ommited imports ...]
    
    public class MyValidationParticipant extends  org.eclipse.wst.jsdt.core.compiler.ValidationParticipant{
    
    @Override
    public boolean isActive(IJavaScriptProject project) {
        return true;
    }
    
    
    
    @Override
    public void buildStarting(BuildContext[] files, boolean isBatch) {      
        super.buildStarting(files, isBatch);    
        for(BuildContext context : files){
            IFile file = context.getFile();
            deleteUnwantedMarkers(file);
        }
    }
    
    
    @Override
    public void reconcile(ReconcileContext context) {
    
        IResource resource = context.getWorkingCopy().getResource();
        CategorizedProblem[] newProblems = new CategorizedProblem[0];
        ArrayList<CategorizedProblem> newProblemList = new ArrayList<CategorizedProblem>();
    
        CategorizedProblem[] probs = context.getProblems("org.eclipse.wst.jsdt.core.problem");
        if(probs != null){
            for(CategorizedProblem p : probs){
                if(!(p.getMessage().equals("Cannot return from outside a function or method."))){                   
                        newProblemList.add(p);                      
                    }                   
                }
            }
        }           
        context.putProblems("org.eclipse.wst.jsdt.core.problem", newProblemList.toArray(newProblems));      
    
        deleteUnwantedMarkers(resource);
    }
    
    public static void deleteUnwantedMarkers(IResource resource){
        if(resource.isSynchronized(IResource.DEPTH_INFINITE)){
            try {                   
                IMarker[] markers = resource.findMarkers(IMarker.PROBLEM, true, IResource.DEPTH_INFINITE);
                if(markers != null && markers.length > 0){
                    for(IMarker m : markers){
                        Object message = m.getAttribute(IMarker.MESSAGE);
                        if(message.equals("Cannot return from outside a function or method.")){                         
                            m.delete(); 
                        }                                   
                    }
                }
    
            }catch (CoreException e) {              
                e.printStackTrace();
            }
        }       
    }
    }
    

    As i said, this is kind of a bad solution since the code relies on the String of the error message. There should be better ways to identify the problems you don't want to have.

    Don't forget to add a proper extension in your plugin.xml for the ValidationParticipant.