Search in sources :

Example 11 with NameValuePair

use of org.eclipse.n4js.json.JSON.NameValuePair in project n4js by eclipse.

the class PackageJsonHelper method convertN4jsPairs.

private void convertN4jsPairs(ProjectDescriptionBuilder target, List<NameValuePair> n4jsPairs) {
    for (NameValuePair pair : n4jsPairs) {
        PackageJsonProperties property = PackageJsonProperties.valueOfNameValuePairOrNull(pair);
        if (property == null) {
            continue;
        }
        JSONValue value = pair.getValue();
        switch(property) {
            case PROJECT_TYPE:
                ProjectType projectType = parseProjectType(asNonEmptyStringOrNull(value));
                if (projectType != null) {
                    target.setType(projectType);
                }
                break;
            case VENDOR_ID:
                target.setVendorId(asNonEmptyStringOrNull(value));
                break;
            case VENDOR_NAME:
                target.setVendorName(asNonEmptyStringOrNull(value));
                break;
            case OUTPUT:
                target.setOutputPath(asNonEmptyStringOrNull(value));
                break;
            case SOURCES:
                target.getSourceContainers().addAll(asSourceContainerDescriptionsOrEmpty(value));
                break;
            case MODULE_FILTERS:
                target.getModuleFilters().addAll(asModuleFiltersInObjectOrEmpty(value));
                break;
            case MAIN_MODULE:
                target.setMainModule(asNonEmptyStringOrNull(value));
                break;
            case TESTED_PROJECTS:
                target.getTestedProjects().addAll(asProjectReferencesInArrayOrEmpty(value));
                break;
            case IMPLEMENTATION_ID:
                target.setImplementationId(asNonEmptyStringOrNull(value));
                break;
            case IMPLEMENTED_PROJECTS:
                target.getImplementedProjects().addAll(asProjectReferencesInArrayOrEmpty(value));
                break;
            case EXTENDED_RUNTIME_ENVIRONMENT:
                target.setExtendedRuntimeEnvironment(asProjectReferenceOrNull(value));
                break;
            case PROVIDED_RUNTIME_LIBRARIES:
                target.getProvidedRuntimeLibraries().addAll(asProjectReferencesInArrayOrEmpty(value));
                break;
            case REQUIRED_RUNTIME_LIBRARIES:
                target.getRequiredRuntimeLibraries().addAll(asProjectReferencesInArrayOrEmpty(value));
                break;
            case DEFINES_PACKAGE:
                target.setDefinesPackage(asStringOrNull(value));
                break;
            case GENERATOR:
                convertN4jsPairs(target, asNameValuePairsOrEmpty(value));
                break;
            case GENERATOR_SOURCE_MAPS:
                target.setGeneratorEnabledSourceMaps(asBooleanOrDefault(value, (Boolean) PackageJsonProperties.GENERATOR_SOURCE_MAPS.defaultValue));
                break;
            case GENERATOR_DTS:
                target.setGeneratorEnabledDts(asBooleanOrDefault(value, (Boolean) PackageJsonProperties.GENERATOR_DTS.defaultValue));
                break;
            case GENERATOR_REWRITE_MODULE_SPECIFIERS:
                for (NameValuePair nvp : asNameValuePairsOrEmpty(value)) {
                    String n = nvp.getName();
                    String v = asStringOrNull(nvp.getValue());
                    if (n != null && v != null) {
                        // note: we allow empty strings
                        target.getGeneratorRewriteModuleSpecifiers().put(n, v);
                    }
                }
                break;
            case GENERATOR_REWRITE_CJS_IMPORTS:
                target.setGeneratorEnabledRewriteCjsImports(asBooleanOrDefault(value, (Boolean) PackageJsonProperties.GENERATOR_REWRITE_CJS_IMPORTS.defaultValue));
                break;
            default:
                break;
        }
    }
}
Also used : JSONValue(org.eclipse.n4js.json.JSON.JSONValue) NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) ProjectType(org.eclipse.n4js.packagejson.projectDescription.ProjectType) PackageJsonUtils.parseProjectType(org.eclipse.n4js.packagejson.PackageJsonUtils.parseProjectType)

Example 12 with NameValuePair

use of org.eclipse.n4js.json.JSON.NameValuePair in project n4js by eclipse.

the class ProjectDescriptionLoader method adjustMainPath.

/**
 * Adjust the path value of the "main" property of the given package.json document as follows (in-place change of
 * the given JSON document):
 * <ol>
 * <li>if the path points to a folder, then "/index.js" will be appended,
 * <li>if neither a folder nor a file exist at the location the path points to and the path does not end in ".js",
 * then ".js" will be appended.
 * </ol>
 */
private void adjustMainPath(URI location, JSONDocument packageJSON) {
    JSONValue content = packageJSON.getContent();
    if (!(content instanceof JSONObject))
        return;
    JSONObject contentCasted = (JSONObject) content;
    NameValuePair mainProperty = JSONModelUtils.getNameValuePair(contentCasted, MAIN.name).orElse(null);
    if (mainProperty == null) {
        return;
    }
    String main = asNonEmptyStringOrNull(mainProperty.getValue());
    if (main == null) {
        return;
    }
    String[] mainSegments;
    if (File.separatorChar == '/') {
        List<String> splitted = Strings.split(main, '/');
        mainSegments = splitted.toArray(String[]::new);
    } else {
        mainSegments = windowsPattern.split(main);
    }
    URI locationWithMain = location.appendSegments(mainSegments);
    if (!main.endsWith(".js") && isFile(URIConverter.INSTANCE, locationWithMain.appendFileExtension("js"))) {
        main += ".js";
        mainProperty.setValue(JSONModelUtils.createStringLiteral(main));
    } else if (isDirectory(URIConverter.INSTANCE, locationWithMain)) {
        if (!(main.endsWith("/") || main.endsWith(File.separator))) {
            main += "/";
        }
        main += "index.js";
        mainProperty.setValue(JSONModelUtils.createStringLiteral(main));
    }
}
Also used : JSONValue(org.eclipse.n4js.json.JSON.JSONValue) NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) JSONObject(org.eclipse.n4js.json.JSON.JSONObject) URI(org.eclipse.emf.common.util.URI) SafeURI(org.eclipse.n4js.workspace.locations.SafeURI)

Example 13 with NameValuePair

use of org.eclipse.n4js.json.JSON.NameValuePair in project n4js by eclipse.

the class AbstractPackageJSONValidatorExtension method collectDocumentValues.

/**
 * Collects all key-path-value pairs that can be extracted from the given {@code object} and stores them in
 * {@code documentValues}.
 *
 * Key-paths are represented in terms of concatenated strings, separated by a {@code .} character (e.g.
 * "nested.name.value" => JSONValue).
 *
 * @param object
 *            The {@link JSONObject} to collect the values form.
 * @param documentValues
 *            The map to store the values to.
 * @param prefix
 *            The prefix to use for key-paths.
 * @param depth
 *            The depth up to which the given {@link JSONObject} should be traversed. If {@code -1} no depth limit
 *            is assumed.
 */
private Multimap<String, JSONValue> collectDocumentValues(JSONObject object, Multimap<String, JSONValue> documentValues, String prefix, int depth) {
    // For negative depths this will always evaluate to false -> no depth limit
    if (depth == 0) {
        return documentValues;
    }
    for (NameValuePair pair : object.getNameValuePairs()) {
        final String pairName = pair.getName();
        final String name = prefix.isEmpty() ? pairName : prefix + "." + pairName;
        final JSONValue value = pair.getValue();
        documentValues.put(name, value);
        if (value instanceof JSONObject) {
            // recursively collect all values from pair value
            collectDocumentValues((JSONObject) value, documentValues, name, depth - 1);
        }
    }
    return documentValues;
}
Also used : JSONValue(org.eclipse.n4js.json.JSON.JSONValue) NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) JSONObject(org.eclipse.n4js.json.JSON.JSONObject)

Example 14 with NameValuePair

use of org.eclipse.n4js.json.JSON.NameValuePair in project n4js by eclipse.

the class PackageJsonValidatorExtension method checkN4JSSection.

/**
 * Checks basic structural properties of the 'n4js' section (e.g. mandatory properties).
 */
@CheckProperty(property = N4JS)
public void checkN4JSSection(JSONValue n4jsSection) {
    // make sure n4js section is an object
    if (!checkIsType(n4jsSection, JSONPackage.Literals.JSON_OBJECT, "as package.json n4js section.")) {
        return;
    }
    JSONObject n4jsSectionJO = (JSONObject) n4jsSection;
    final Multimap<String, JSONValue> n4jsValues = collectObjectValues(n4jsSectionJO);
    // Check for correct types (null-values (non-existent) will not lead to issues)
    // Properties that are not checked here, have their own check-method which also validates their types.
    checkIsType(n4jsValues.get(VENDOR_ID.name), JSONPackage.Literals.JSON_STRING_LITERAL, "as vendor ID");
    checkIsType(n4jsValues.get(VENDOR_NAME.name), JSONPackage.Literals.JSON_STRING_LITERAL, "as vendor name");
    checkIsType(n4jsValues.get(OUTPUT.name), JSONPackage.Literals.JSON_STRING_LITERAL, "as output folder path");
    checkIsType(n4jsValues.get(EXTENDED_RUNTIME_ENVIRONMENT.name), JSONPackage.Literals.JSON_STRING_LITERAL, "as reference to extended runtime environment");
    checkIsArrayOfType(n4jsValues.get(PROVIDED_RUNTIME_LIBRARIES.name), JSONPackage.Literals.JSON_STRING_LITERAL, "as provided runtime libraries", "as library reference");
    checkIsArrayOfType(n4jsValues.get(REQUIRED_RUNTIME_LIBRARIES.name), JSONPackage.Literals.JSON_STRING_LITERAL, "as required runtime libraries", "as library reference");
    // Check for empty strings
    checkIsNonEmptyString(n4jsValues.get(VENDOR_ID.name), VENDOR_ID);
    checkIsNonEmptyString(n4jsValues.get(VENDOR_NAME.name), VENDOR_NAME);
    Set<String> allN4JSPropertyNames = PackageJsonProperties.getAllN4JSPropertyNames();
    for (NameValuePair nameValuePair : n4jsSectionJO.getNameValuePairs()) {
        String nvpName = nameValuePair.getName();
        if (!allN4JSPropertyNames.contains(nvpName)) {
            String msg = IssueCodes.getMessageForPKGJ_PROPERTY_UNKNOWN(nvpName);
            addIssue(msg, nameValuePair, JSONPackage.Literals.NAME_VALUE_PAIR__NAME, IssueCodes.PKGJ_PROPERTY_UNKNOWN);
        }
    }
}
Also used : JSONValue(org.eclipse.n4js.json.JSON.JSONValue) NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) JSONObject(org.eclipse.n4js.json.JSON.JSONObject)

Example 15 with NameValuePair

use of org.eclipse.n4js.json.JSON.NameValuePair in project n4js by eclipse.

the class PackageJsonValidatorExtension method checkRewriteModuleSpecifiers.

/**
 * Checks 'rewriteModuleSpecifiers'.
 */
@CheckProperty(property = GENERATOR_REWRITE_MODULE_SPECIFIERS)
public void checkRewriteModuleSpecifiers(JSONValue value) {
    if (!checkIsType(value, JSONPackage.Literals.JSON_OBJECT, "(map from module specifier in N4JS source code to specifier used in output code)")) {
        return;
    }
    for (NameValuePair nvp : ((JSONObject) value).getNameValuePairs()) {
        String n = nvp.getName();
        JSONValue v = nvp.getValue();
        if (n == null || v == null) {
            // syntax error
            continue;
        }
        if (n.isEmpty()) {
            addIssue(IssueCodes.getMessageForPKGJ_REWRITE_MODULE_SPECIFIERS__EMPTY_SPECIFIER("Source"), nvp, JSONPackage.eINSTANCE.getNameValuePair_Name(), IssueCodes.PKGJ_REWRITE_MODULE_SPECIFIERS__EMPTY_SPECIFIER);
        } else if (!(v instanceof JSONStringLiteral)) {
            addIssue(IssueCodes.getMessageForPKGJ_REWRITE_MODULE_SPECIFIERS__INVALID_VALUE(), v, IssueCodes.PKGJ_REWRITE_MODULE_SPECIFIERS__INVALID_VALUE);
        } else if (((JSONStringLiteral) v).getValue().isEmpty()) {
            addIssue(IssueCodes.getMessageForPKGJ_REWRITE_MODULE_SPECIFIERS__EMPTY_SPECIFIER("Output code"), v, IssueCodes.PKGJ_REWRITE_MODULE_SPECIFIERS__EMPTY_SPECIFIER);
        }
    }
}
Also used : JSONValue(org.eclipse.n4js.json.JSON.JSONValue) NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) JSONObject(org.eclipse.n4js.json.JSON.JSONObject) JSONStringLiteral(org.eclipse.n4js.json.JSON.JSONStringLiteral)

Aggregations

NameValuePair (org.eclipse.n4js.json.JSON.NameValuePair)28 JSONObject (org.eclipse.n4js.json.JSON.JSONObject)17 JSONValue (org.eclipse.n4js.json.JSON.JSONValue)17 JSONStringLiteral (org.eclipse.n4js.json.JSON.JSONStringLiteral)7 EObject (org.eclipse.emf.ecore.EObject)6 JSONArray (org.eclipse.n4js.json.JSON.JSONArray)4 JSONDocument (org.eclipse.n4js.json.JSON.JSONDocument)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Map (java.util.Map)2 URI (org.eclipse.emf.common.util.URI)2 JSONBooleanLiteral (org.eclipse.n4js.json.JSON.JSONBooleanLiteral)2 JSONNumericLiteral (org.eclipse.n4js.json.JSON.JSONNumericLiteral)2 SourceContainerType (org.eclipse.n4js.packagejson.projectDescription.SourceContainerType)2 NPMVersionRequirement (org.eclipse.n4js.semver.Semver.NPMVersionRequirement)2 INode (org.eclipse.xtext.nodemodel.INode)2 Check (org.eclipse.xtext.validation.Check)2 Optional (com.google.common.base.Optional)1