use of org.eclipse.n4js.workspace.utils.N4JSPackageName in project n4js by eclipse.
the class ImportHelper method createImportDescriptorFromAST.
/**
* Creates an {@link ImportDescriptor} for a given existing import in the AST.
*
* @param importDecl
* the import declaration. May not be <code>null</code>.
* @param importSpec
* the import specification in case of a named or namespace import; <code>null</code> denotes a bare
* import.
* @param originalIndex
* the original index to set in the newly created {@link ImportDescriptor}. This should usually reflect
* the original order of imports in the AST.
* @return a newly created import descriptor.
*/
public ImportDescriptor createImportDescriptorFromAST(ImportDeclaration importDecl, ImportSpecifier importSpec, int originalIndex) {
Objects.requireNonNull(importDecl);
if (importSpec != null && !importSpec.isFlaggedUsedInCode()) {
throw new IllegalArgumentException("must not create an ImportDescriptor for unused imports");
}
if ((importSpec == null && ImportSpecifiersUtil.isBrokenImport(importDecl)) || (importSpec != null && ImportSpecifiersUtil.isBrokenImport(importSpec))) {
throw new IllegalArgumentException("must not create an ImportDescriptor for broken imports");
}
AbstractModule module = importDecl.getModule();
Optional<N4JSPackageName> targetProjectName = module instanceof TModule ? Optional.of(new N4JSPackageName(((TModule) module).getPackageName())) : // module declaration), so they do not have a targetProjectName:
Optional.absent();
QualifiedName targetModule = qualifiedNameConverter.toQualifiedName(module.getQualifiedName());
String moduleSpecifier = importDecl.getModuleSpecifierAsText();
if (importDecl.isBare()) {
return ImportDescriptor.createBareImport(moduleSpecifier, targetProjectName, targetModule, originalIndex);
} else {
if (importSpec instanceof NamedImportSpecifier) {
NamedImportSpecifier importSpecCasted = (NamedImportSpecifier) importSpec;
if (importSpecCasted.isDefaultImport()) {
String localName = importSpecCasted.getAlias();
return ImportDescriptor.createDefaultImport(localName, moduleSpecifier, targetProjectName, targetModule, originalIndex);
} else {
String elementName = importSpecCasted.getImportedElementAsText();
String alias = importSpecCasted.getAlias();
return ImportDescriptor.createNamedImport(elementName, alias, moduleSpecifier, targetProjectName, targetModule, originalIndex);
}
} else if (importSpec instanceof NamespaceImportSpecifier) {
String localNamespaceName = ((NamespaceImportSpecifier) importSpec).getAlias();
boolean isDynamic = ((NamespaceImportSpecifier) importSpec).isDeclaredDynamic();
return ImportDescriptor.createNamespaceImport(localNamespaceName, isDynamic, moduleSpecifier, targetProjectName, targetModule, originalIndex);
} else if (importSpec != null) {
throw new IllegalArgumentException("unknown subclass of ImportSpecifier: " + importSpec.getClass().getSimpleName());
} else {
throw new IllegalArgumentException("importSpec may be null only if importDecl is a bare import");
}
}
}
use of org.eclipse.n4js.workspace.utils.N4JSPackageName in project n4js by eclipse.
the class ReferenceResolutionFinder method getImportToBeAdded.
private ImportDescriptor getImportToBeAdded(ReferenceResolutionCandidate rrc) {
NameAndAlias requestedImport = rrc.addedImportNameAndAlias;
if (requestedImport == null) {
return null;
}
QualifiedName qualifiedName = requestedImport.name;
String optionalAlias = requestedImport.alias;
N4JSPackageName projectName = rrc.candidateProjectOrNull != null ? rrc.candidateProjectOrNull.getN4JSPackageName() : null;
if (projectName == null) {
return null;
}
QualifiedName moduleName = qualifiedName.skipLast(1);
String moduleSpecifier = qualifiedNameConverter.toString(moduleName);
ImportDescriptor importDesc;
if (N4JSLanguageUtils.isDefaultExport(qualifiedName)) {
String localName = optionalAlias != null ? optionalAlias : N4JSLanguageUtils.lastSegmentOrDefaultHost(qualifiedName);
importDesc = ImportDescriptor.createDefaultImport(localName, moduleSpecifier, Optional.of(projectName), moduleName, Integer.MAX_VALUE);
} else {
importDesc = ImportDescriptor.createNamedImport(qualifiedName.getLastSegment(), optionalAlias, moduleSpecifier, Optional.of(projectName), moduleName, Integer.MAX_VALUE);
}
return importDesc;
}
use of org.eclipse.n4js.workspace.utils.N4JSPackageName in project n4js by eclipse.
the class PackageJsonModificationUtils method setVersionOfDependenciesInPackageJsonString.
/**
* Changes the version of all dependencies and devDependencies to packages with a name contained in
* {@code projectNames} to the given version. Names given in the set that are not among the dependencies in the
* package.json file will be ignored (i.e. this method won't add any dependencies); dependencies in the package.json
* file that are not denoted by the names in the set will remain unchanged.
*
* @return the <code>package.json</code> string after all replacements were done, or {@link Optional#absent()
* absent} in case the JSON string was successfully parsed, but this operation did not lead to any changes.
* @throws IllegalArgumentException
* in case of parse errors.
*/
public static Optional<String> setVersionOfDependenciesInPackageJsonString(String packageJson, Set<N4JSPackageName> projectNames, String versionConstraintToSet) {
List<ElementWithRegion> deps = findDependenciesWithRegion(packageJson);
if (deps.isEmpty()) {
return Optional.absent();
}
StringBuilder result = null;
Collections.sort(deps, (dep1, dep2) -> Integer.compare(dep2.offset, dep1.offset));
for (ElementWithRegion dep : deps) {
if (projectNames.contains(new N4JSPackageName(dep.elementName))) {
if (result == null) {
result = new StringBuilder(packageJson);
}
result.replace(dep.offset + 1, dep.offset + dep.length - 1, versionConstraintToSet);
}
}
return result != null ? Optional.of(result.toString()) : Optional.absent();
}
use of org.eclipse.n4js.workspace.utils.N4JSPackageName in project n4js by eclipse.
the class ProjectImportEnablingScope method getElements.
@Override
public Iterable<IEObjectDescription> getElements(QualifiedName name) {
ModuleSpecifierForm moduleSpecifierForm = computeImportType(name, this.contextProject);
storeModuleSpecifierFormInAST(moduleSpecifierForm);
switch(moduleSpecifierForm) {
case PROJECT:
{
final N4JSPackageName firstSegment = new N4JSPackageName(name.getFirstSegment());
return findModulesInProject(Optional.absent(), firstSegment);
}
case COMPLETE:
{
final N4JSPackageName firstSegment = new N4JSPackageName(name.getFirstSegment());
return findModulesInProject(Optional.of(name.skipFirst(1)), firstSegment);
}
case PLAIN:
{
return parent.getElements(name);
}
default:
return Collections.emptyList();
}
}
use of org.eclipse.n4js.workspace.utils.N4JSPackageName in project n4js by eclipse.
the class N4jsLibsAccess method installN4jsLibs.
/**
* Same as {@link #installN4jsLibs(Path, boolean, boolean, boolean, N4JSPackageName...)} but will install the
* n4js-libs specified as (dev-)dependencies in the given package.json file. Will throw an exception if the given
* package.json file has dependencies that are not n4js-libs packages.
*
* @param targetPath
* see {@link #installN4jsLibs(Path, boolean, boolean, boolean, N4JSPackageName...) here}.
* @param includeDependencies
* see {@link #installN4jsLibs(Path, boolean, boolean, boolean, N4JSPackageName...) here}.
* @param useSymbolicLinks
* see {@link #installN4jsLibs(Path, boolean, boolean, boolean, N4JSPackageName...) here}.
* @param deleteOnExit
* see {@link #installN4jsLibs(Path, boolean, boolean, boolean, N4JSPackageName...) here}.
* @param packageJsonFile
* the package.json file to scan for [dev]Dependencies.
* @param includeDevDependencies
* whether also the devDependencies of the given package.json file should be installed.
*/
public static Map<N4JSPackageName, Path> installN4jsLibs(Path targetPath, boolean includeDependencies, boolean useSymbolicLinks, boolean deleteOnExit, Path packageJsonFile, boolean includeDevDependencies) throws IOException {
if (!packageJsonFile.getFileName().toString().equals(N4JSGlobals.PACKAGE_JSON)) {
throw new IllegalArgumentException("path does not denote a package.json file: " + packageJsonFile);
}
Path n4jsLibsLocation = findN4jsLibsLocation();
Map<N4JSPackageName, Path> toBeInstalled = new HashMap<>();
List<N4JSPackageName> deps = loadDepenencies(packageJsonFile.getParent(), includeDevDependencies);
for (N4JSPackageName projectName : deps) {
Path projectPath = findN4jsLib(n4jsLibsLocation, projectName, false);
toBeInstalled.put(projectName, projectPath);
}
return installN4jsLibs(targetPath, includeDependencies, useSymbolicLinks, deleteOnExit, toBeInstalled);
}
Aggregations