Skip to content

Commit

Permalink
fix cjs import fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mmews committed Aug 30, 2024
1 parent 36e2622 commit bd832e5
Showing 1 changed file with 37 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package org.eclipse.n4js.utils;

import java.util.Collection;
import java.util.Objects;
import java.util.stream.Collectors;

import org.eclipse.emf.common.util.EList;
Expand Down Expand Up @@ -127,9 +128,11 @@ public N4JSProjectConfigSnapshot replaceDefinitionProjectByDefinedProject(Import
N4JSPackageName definitionPackageName = importedProject.getDefinesPackage();
N4JSProjectConfigSnapshot importingPrj = workspaceAccess.findProjectContaining(importingDeclOrigAST);
if (definitionPackageName != null && importingPrj != null) {
String definitionProjectId = importingPrj.getProjectIdForPackageName(definitionPackageName.getRawName());
String definitionProjectId = importingPrj
.getProjectIdForPackageName(definitionPackageName.getRawName());
if (definitionProjectId != null) {
N4JSProjectConfigSnapshot definitionProject = workspaceAccess.findProjectByName(importingDeclOrigAST,
N4JSProjectConfigSnapshot definitionProject = workspaceAccess.findProjectByName(
importingDeclOrigAST,
definitionProjectId);
if (definitionProject != null) {
return definitionProject;
Expand All @@ -154,7 +157,7 @@ public boolean isES6Module(IResourceDescriptions index, ImportDeclaration import
// 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;
String ext = uri != null ? URIUtils.fileExtension(uri) : null;
if (!module.isN4jsdModule() && N4JSGlobals.ALL_N4JS_FILE_EXTENSIONS.contains(ext)) {
return true; // the N4JS transpiler always emits ES6 module code
}
Expand Down Expand Up @@ -208,7 +211,7 @@ public String getOutputFileExtension(IResourceDescriptions index, ImportDeclarat
}
Resource targetResource = targetModule.eResource();
URI uri = targetResource != null ? targetResource.getURI() : null;
String ext = uri != null ? uri.fileExtension() : null;
String ext = uri != null ? URIUtils.fileExtension(uri) : null;
if (N4JSGlobals.ALL_JS_FILE_EXTENSIONS.contains(ext)) {
if (N4JSGlobals.JSX_FILE_EXTENSION.equals(ext)) {
// we assume .jsx files are transpiled to .js by other tools
Expand All @@ -226,31 +229,50 @@ public String getOutputFileExtension(IResourceDescriptions index, ImportDeclarat
* In case of .n4jsd files, we have to find out the extension of the plain-JS file being described by the .n4jsd
* file *and* provide special handling for directory imports.
*/
private String getActualFileExtensionForN4jsdFile(IResourceDescriptions index, ImportDeclaration importingDeclOrigAST,
TModule targetModule) {
private String getActualFileExtensionForN4jsdFile(IResourceDescriptions index,
ImportDeclaration importingDeclOrigAST, TModule targetModule) {

URI targetUri = targetModule.eResource().getURI();
String targetExt = URIUtils.fileExtension(targetUri);
N4JSProjectConfigSnapshot targetPrj = workspaceAccess.findProjectContaining(targetModule);
ProjectDescription targetPD = targetPrj.getProjectDescription();
String definesPackageName = targetPD.getDefinesPackage();
if (definesPackageName == null && N4JSGlobals.DTS_FILE_EXTENSION.equals(targetExt)) {
if (targetPrj.getPackageName().startsWith(N4JSGlobals.TYPES_SCOPE)) {
definesPackageName = targetPrj.getPackageName().replace(N4JSGlobals.TYPES_SCOPE + "/", "");
} else {
// assume this ts project also contains the js files
definesPackageName = targetPrj.getPackageName();
}
}

QualifiedName targetQN = qualifiedNameConverter.toQualifiedName(targetModule.getQualifiedName());
Iterable<IEObjectDescription> matchingTModules = index.getExportedObjects(TypesPackage.Literals.TMODULE,
targetQN, false);
boolean gotJS = false;
boolean gotCJS = false;
boolean gotMJS = false;
for (IEObjectDescription desc : matchingTModules) {
String ext = desc.getEObjectURI().fileExtension();
if (N4JSGlobals.JS_FILE_EXTENSION.equals(ext)) {
URI descUri = desc.getEObjectURI().trimFragment();
N4JSProjectConfigSnapshot definedPrj = workspaceAccess.findProjectByNestedLocation(targetModule, descUri);
if (definesPackageName != null && !Objects.equals(definesPackageName, definedPrj.getPackageName())) {
continue;
}
if (targetUri == descUri) {
continue;
}
String ext = URIUtils.fileExtension(descUri);
if (N4JSGlobals.MJS_FILE_EXTENSION.equals(ext)) {
return N4JSGlobals.MJS_FILE_EXTENSION;
} else if (N4JSGlobals.JS_FILE_EXTENSION.equals(ext)) {
gotJS = true;
} else if (N4JSGlobals.CJS_FILE_EXTENSION.equals(ext)) {
gotCJS = true;
} else if (N4JSGlobals.MJS_FILE_EXTENSION.equals(ext)) {
gotMJS = true;
}
if (gotJS && gotCJS && gotMJS) {
if (gotJS && gotCJS) {
break;
}
}
if (gotMJS) {
return N4JSGlobals.MJS_FILE_EXTENSION;
} else if (gotCJS) {
if (gotCJS) {
return N4JSGlobals.CJS_FILE_EXTENSION;
} else if (gotJS) {
return N4JSGlobals.JS_FILE_EXTENSION;
Expand Down

0 comments on commit bd832e5

Please sign in to comment.