Search in sources :

Example 1 with ProjectNameInfo

use of org.eclipse.n4js.utils.ProjectDescriptionUtils.ProjectNameInfo in project n4js by eclipse.

the class PackageJsonValidatorExtension method checkName.

/**
 * Validates the project/package name.
 */
@CheckProperty(property = NAME)
public void checkName(JSONValue projectNameValue) {
    // first check for the type of the name value
    if (!checkIsType(projectNameValue, JSONPackage.Literals.JSON_STRING_LITERAL, "as package name")) {
        return;
    }
    final JSONStringLiteral projectNameLiteral = (JSONStringLiteral) projectNameValue;
    final String projectName = projectNameLiteral.getValue();
    final String projectNameWithoutScope = ProjectDescriptionUtils.getPlainPackageName(projectName);
    final String scopeName = ProjectDescriptionUtils.getScopeName(projectName);
    // make sure the name conforms to the IDENTIFIER_PATTERN
    if (!ProjectDescriptionUtils.isValidPlainPackageName(projectNameWithoutScope)) {
        addIssue(IssueCodes.getMessageForPKGJ_INVALID_PROJECT_NAME(projectNameWithoutScope), projectNameValue, IssueCodes.PKGJ_INVALID_PROJECT_NAME);
    }
    if (scopeName != null) {
        String scopeNameWithoutPrefix = scopeName.substring(1);
        if (!ProjectDescriptionUtils.isValidScopeName(scopeNameWithoutPrefix)) {
            String msg = IssueCodes.getMessageForPKGJ_INVALID_SCOPE_NAME(scopeNameWithoutPrefix);
            addIssue(msg, projectNameValue, IssueCodes.PKGJ_INVALID_SCOPE_NAME);
        }
    }
    // compute names of project folder, parent folder, Eclipse project folder
    final URI projectUri = projectNameValue.eResource().getURI().trimSegments(1);
    final ProjectNameInfo nameInfo = ProjectNameInfo.of(projectUri);
    // make sure the project name equals the name of the project folder
    if (!projectNameWithoutScope.equals(nameInfo.projectFolderName)) {
        String msg = IssueCodes.getMessageForPKGJ_PACKAGE_NAME_MISMATCH(projectNameWithoutScope, nameInfo.projectFolderName);
        addIssue(msg, projectNameLiteral, IssueCodes.PKGJ_PACKAGE_NAME_MISMATCH);
    }
    // (i.e. the folder containing the project folder)
    if (scopeName != null && !scopeName.equals(nameInfo.parentFolderName)) {
        String msg = IssueCodes.getMessageForPKGJ_SCOPE_NAME_MISMATCH(scopeName, nameInfo.parentFolderName);
        addIssue(msg, projectNameLiteral, IssueCodes.PKGJ_SCOPE_NAME_MISMATCH);
    }
}
Also used : ProjectNameInfo(org.eclipse.n4js.utils.ProjectDescriptionUtils.ProjectNameInfo) FileURI(org.eclipse.n4js.workspace.locations.FileURI) URI(org.eclipse.emf.common.util.URI) JSONStringLiteral(org.eclipse.n4js.json.JSON.JSONStringLiteral)

Aggregations

URI (org.eclipse.emf.common.util.URI)1 JSONStringLiteral (org.eclipse.n4js.json.JSON.JSONStringLiteral)1 ProjectNameInfo (org.eclipse.n4js.utils.ProjectDescriptionUtils.ProjectNameInfo)1 FileURI (org.eclipse.n4js.workspace.locations.FileURI)1