Search code examples
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.

  • They can contain "compiler hints" which are basicly statements beginning with #
  • They can contain return statements outside of functions

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:

    • Delete all unwanted problem markers of all files in buildStarting (this removes problem markers from all files in your project that are about to be validated)
    • Iterate the CategorizedProblem objects of the ReconcileContext (getProblems())
    • Create a new Array containing only the CategorizedProblems you want to keep
    • Set this new Array to the ReconcileContext with putProblems()
    • Delete the unwanted markers again for that file (i don't know why this is needed, please don't ask, i don't care anymore :-/)

    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.