Search in sources :

Example 41 with N4JSProjectConfigSnapshot

use of org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot in project n4js by eclipse.

the class ApiImplMapping method getErrorMessages.

/**
 * In {@link #hasErrors() case of error}, returns human-readable error messages. Otherwise, an empty list will be
 * returned.
 */
public List<String> getErrorMessages() {
    final List<String> msgs = new ArrayList<>();
    for (N4JSProjectConfigSnapshot p : projectsWithUndefImplIds) {
        msgs.add("project '" + p.getName() + "' does not define an ImplementationId in its manifest");
    }
    for (Map.Entry<Pair<N4JSPackageName, N4JSPackageName>, Set<N4JSProjectConfigSnapshot>> currConflict : conflicts.entrySet()) {
        final N4JSPackageName apiId = currConflict.getKey().getKey();
        final N4JSPackageName implId = currConflict.getKey().getValue();
        final Set<N4JSProjectConfigSnapshot> culprits = currConflict.getValue();
        final String culpritsStr = " - " + culprits.stream().map(c -> c.getName()).collect(Collectors.joining("\n - "));
        msgs.add("several projects define an implementation for API project '" + apiId + "' with implementation ID '" + implId + "':\n" + culpritsStr);
    }
    return msgs;
}
Also used : Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) N4JSPackageName(org.eclipse.n4js.workspace.utils.N4JSPackageName) N4JSProjectConfigSnapshot(org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Pair(org.eclipse.xtext.xbase.lib.Pair)

Example 42 with N4JSProjectConfigSnapshot

use of org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot in project n4js by eclipse.

the class TestDiscoveryHelper method isTestable.

/**
 * Checks if the resource at the given location can be executed as a test. The location may point to an N4JS project
 * or a folder or file within an N4JS project.
 * <p>
 * This method is intended as a very first check to decide if the user should be given the option to launch a test
 * or not; for example, to enable or disable a "Run as test" context menu item). This method does not check all
 * details because many checks are better handled at later stages when meaningful error messages can be provided to
 * the user.
 */
public boolean isTestable(N4JSWorkspaceConfigSnapshot ws, final URI location) {
    if (null == location) {
        return false;
    }
    if (isProject(ws, location)) {
        // location points an N4JS project
        // --> must contain at least 1 source container of type "test"
        // (do *not* check if the folder contains test classes (for performance reasons and
        // to show better error messages; same behavior as in JUnit support in Eclipse))
        final N4JSProjectConfigSnapshot p = ws.findProjectByPath(location);
        return p.getSourceFolders().stream().anyMatch(N4JSSourceFolderSnapshot::isTest);
    } else {
        // then extract the fragment and check the location for the module.
        if (location.hasFragment()) {
            return isTestable(ws, location.trimFragment());
        }
        // (2) location must lie in a source container of type "test"
        final N4JSSourceFolderSnapshot c = ws.findSourceFolderContaining(location);
        if (c == null || !c.isTest())
            return false;
        // (3) if the location points to an n4js-file, it must contain at least one test class
        final ResourceSet resourceSet = workspaceAccess.createResourceSet();
        final IResourceDescriptions index = workspaceAccess.getXtextIndex(resourceSet).get();
        final IResourceDescription rdesc = index.getResourceDescription(location);
        if (rdesc != null) {
            return stream(rdesc.getExportedObjectsByType(T_CLASS)).anyMatch(desc -> hasTestMethods(resourceSet, desc));
        } else {
            // to show better error messages; same behavior as in JUnit support in Eclipse))
            return true;
        }
    }
}
Also used : IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) N4JSProjectConfigSnapshot(org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot) IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions) N4JSSourceFolderSnapshot(org.eclipse.n4js.workspace.N4JSSourceFolderSnapshot) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet)

Example 43 with N4JSProjectConfigSnapshot

use of org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot in project n4js by eclipse.

the class TestDiscoveryHelper method collectAllTestsFromProjects.

/**
 * Collects all test cases from the given projects and returns with a new test tree instance representing those test
 * cases.
 *
 * @param resourceSetAccess
 *            the accessor for the resource set
 *
 * @param projects
 *            the projects that are searched for tests
 *
 * @return a test tree representing all test cases in the workspace.
 */
public TestTree collectAllTestsFromProjects(N4JSWorkspaceConfigSnapshot ws, Function<? super URI, ? extends ResourceSet> resourceSetAccess, Iterable<? extends N4JSProjectConfigSnapshot> projects) {
    List<URI> testableProjectURIs = new ArrayList<>();
    for (N4JSProjectConfigSnapshot project : projects) {
        URI location = project.getPathAsFileURI().toURI();
        if (isTestable(ws, location)) {
            testableProjectURIs.add(location);
        }
    }
    TestTree collectedTests = collectTests(ws, resourceSetAccess, testableProjectURIs);
    return collectedTests;
}
Also used : N4JSProjectConfigSnapshot(org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) URI(org.eclipse.emf.common.util.URI) TestTree(org.eclipse.n4js.tooling.tester.model.TestTree)

Example 44 with N4JSProjectConfigSnapshot

use of org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot in project n4js by eclipse.

the class EObjectDescriptionHelper method isDescriptionOfModuleWith.

/**
 * Helper method that checks if given {@link IEObjectDescription description} describes {@link TModule} containing
 * given {@link EObject}.
 *
 * Returns <code>true</code> only if provided {@link IEObjectDescription description} has the same
 * {@link QualifiedName} as module of the {@link EObject}. Additionally if {@link IEObjectDescription description}
 * describes {@link TModule#isMainModule() main module} then it is checked if both are contained in the same
 * {@link N4JSProjectConfigSnapshot}.
 *
 * @returns true if {@link IEObjectDescription} describes module of {@link EObject}
 */
public boolean isDescriptionOfModuleWith(Notifier context, IEObjectDescription eoDescription, EObject eObject) {
    // check if module names are the same
    final Script containingScript = EcoreUtil2.getContainerOfType(eObject, Script.class);
    final TModule containingModule = containingScript != null ? containingScript.getModule() : null;
    final String eObjectModuleName = containingModule != null ? containingModule.getQualifiedName() : null;
    if (eObjectModuleName == null || !eObjectModuleName.equals(qualifiedNameConverter.toString(eoDescription.getQualifiedName()))) {
        return false;
    }
    // if not a main module we assume true
    if (!isMainModule(eoDescription)) {
        return true;
    }
    // for main modules we check containing project
    Resource eObjectResource = eObject != null ? eObject.eResource() : null;
    URI eObjectResourceURI = eObjectResource != null ? eObjectResource.getURI() : null;
    if (eObjectResourceURI == null) {
        return false;
    }
    final N4JSProjectConfigSnapshot targetProject = workspaceAccess.findProjectByNestedLocation(context, eoDescription.getEObjectURI());
    final N4JSProjectConfigSnapshot currentProject = workspaceAccess.findProjectByNestedLocation(context, eObjectResourceURI);
    return targetProject == currentProject;
}
Also used : Script(org.eclipse.n4js.n4JS.Script) N4JSProjectConfigSnapshot(org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot) Resource(org.eclipse.emf.ecore.resource.Resource) TModule(org.eclipse.n4js.ts.types.TModule) URI(org.eclipse.emf.common.util.URI)

Example 45 with N4JSProjectConfigSnapshot

use of org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot in project n4js by eclipse.

the class N4JSLanguageHelper method isES6Module.

/**
 * Tells whether the given module is an EcmaScript 2015 module, i.e. using {@code import} instead of
 * {@code require()}, etc.
 * <p>
 * <b>WARNING</b>: regarding {@code index} the same warning applies as given
 * {@link #getOutputFileExtension(IResourceDescriptions, TModule) here}.
 */
public boolean isES6Module(IResourceDescriptions index, TModule module) {
    // 1) decide based on the file extension of the target module
    Resource resource = module.eResource();
    URI uri = resource != null ? resource.getURI() : null;
    String ext = uri != null ? uri.fileExtension() : null;
    if (!module.isN4jsdModule() && N4JSGlobals.ALL_N4JS_FILE_EXTENSIONS.contains(ext)) {
        // the N4JS transpiler always emits ES6 module code
        return true;
    }
    String extActual = getOutputFileExtension(index, module);
    if (N4JSGlobals.CJS_FILE_EXTENSION.equals(extActual)) {
        return false;
    } else if (N4JSGlobals.MJS_FILE_EXTENSION.equals(extActual)) {
        return true;
    } else if (extActual.isEmpty()) {
        // use 'true' as fall back
        return true;
    }
    // (failed: file extension of target module does not tell whether it's commonjs or esm)
    // 2) decide based on the nature of the target project
    N4JSProjectConfigSnapshot targetProject = replaceDefinitionProjectByDefinedProject(resource, workspaceAccess.findProjectContaining(resource), true);
    if (targetProject == null) {
        // use 'true' as fall back
        return true;
    }
    if (targetProject.isESM()) {
        return true;
    }
    return false;
}
Also used : N4JSProjectConfigSnapshot(org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot) Resource(org.eclipse.emf.ecore.resource.Resource) URI(org.eclipse.emf.common.util.URI) FileURI(org.eclipse.n4js.workspace.locations.FileURI)

Aggregations

N4JSProjectConfigSnapshot (org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot)50 URI (org.eclipse.emf.common.util.URI)15 N4JSPackageName (org.eclipse.n4js.workspace.utils.N4JSPackageName)9 N4JSSourceFolderSnapshot (org.eclipse.n4js.workspace.N4JSSourceFolderSnapshot)8 Path (java.nio.file.Path)7 Resource (org.eclipse.emf.ecore.resource.Resource)7 N4JSWorkspaceConfigSnapshot (org.eclipse.n4js.workspace.N4JSWorkspaceConfigSnapshot)7 FileURI (org.eclipse.n4js.workspace.locations.FileURI)7 ArrayList (java.util.ArrayList)6 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)6 File (java.io.File)5 HashMap (java.util.HashMap)5 N4JSResource (org.eclipse.n4js.resource.N4JSResource)5 LinkedHashMap (java.util.LinkedHashMap)4 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)4 ProjectDescription (org.eclipse.n4js.packagejson.projectDescription.ProjectDescription)4 TModule (org.eclipse.n4js.ts.types.TModule)4 QualifiedName (org.eclipse.xtext.naming.QualifiedName)4 IResourceDescription (org.eclipse.xtext.resource.IResourceDescription)4 IResourceDescriptions (org.eclipse.xtext.resource.IResourceDescriptions)4