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;
}
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;
}
}
}
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;
}
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;
}
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;
}
Aggregations