Search in sources :

Example 1 with JSONStringLiteral

use of org.eclipse.n4js.json.JSON.JSONStringLiteral 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)

Example 2 with JSONStringLiteral

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

the class PackageJsonValidatorExtension method internalCheckNoNestedSourceContainers.

/**
 * Validates that the given list of declared source container paths do not declare source containers that are nested
 * within each other.
 *
 * This validation runs in O(n^2) wrt. the number of source containers.
 */
private void internalCheckNoNestedSourceContainers(final List<JSONStringLiteral> sourceContainers) {
    // collect conflicting prefixes (containers) per offending source container path (string literal)
    final Multimap<ASTTraceable<Path>, ASTTraceable<Path>> conflictingPrefixes = HashMultimap.create();
    final List<ASTTraceable<Path>> containerPaths = sourceContainers.stream().map(ASTTraceable.map(l -> new File(l.getValue()).toPath().normalize())).collect(Collectors.toList());
    // check paths that prefix each other
    for (ASTTraceable<Path> path : containerPaths) {
        for (ASTTraceable<Path> otherPaths : containerPaths) {
            // do not check with itself
            if (path == otherPaths) {
                continue;
            }
            // skip exact matches, this is detected in duplicate validation
            if (path.element.equals(otherPaths.element)) {
                continue;
            }
            if (path.element.startsWith(otherPaths.element) || otherPaths.element.toString().isEmpty()) {
                conflictingPrefixes.put(path, otherPaths);
            }
        }
    }
    // eventually, add issues for all offending source container literals
    for (ASTTraceable<Path> path : conflictingPrefixes.keySet()) {
        final Collection<ASTTraceable<Path>> containers = conflictingPrefixes.get(path);
        final String containersDescription = containers.stream().map(c -> "\"" + ((JSONStringLiteral) c.astElement).getValue() + "\"").sorted(// sort by ascending length
        (s1, s2) -> s1.length() - s2.length()).collect(Collectors.joining(", "));
        addIssue(IssueCodes.getMessageForPKGJ_NESTED_SOURCE_CONTAINER(containersDescription), path.astElement, IssueCodes.PKGJ_NESTED_SOURCE_CONTAINER);
    }
}
Also used : Path(java.nio.file.Path) REQUIRED_RUNTIME_LIBRARIES(org.eclipse.n4js.packagejson.PackageJsonProperties.REQUIRED_RUNTIME_LIBRARIES) IssueCodes(org.eclipse.n4js.validation.IssueCodes) JSONStringLiteral(org.eclipse.n4js.json.JSON.JSONStringLiteral) Inject(com.google.inject.Inject) NV_SOURCE_CONTAINER(org.eclipse.n4js.packagejson.PackageJsonProperties.NV_SOURCE_CONTAINER) NAME(org.eclipse.n4js.packagejson.PackageJsonProperties.NAME) FileUtils(org.eclipse.n4js.utils.io.FileUtils) ProjectNameInfo(org.eclipse.n4js.utils.ProjectDescriptionUtils.ProjectNameInfo) URLVersionRequirement(org.eclipse.n4js.semver.Semver.URLVersionRequirement) VENDOR_NAME(org.eclipse.n4js.packagejson.PackageJsonProperties.VENDOR_NAME) HashMultimap(com.google.common.collect.HashMultimap) PackageJsonUtils(org.eclipse.n4js.packagejson.PackageJsonUtils) DEPENDENCIES(org.eclipse.n4js.packagejson.PackageJsonProperties.DEPENDENCIES) ModuleFilterType(org.eclipse.n4js.packagejson.projectDescription.ModuleFilterType) Optional(com.google.common.base.Optional) Map(java.util.Map) SourceContainerType(org.eclipse.n4js.packagejson.projectDescription.SourceContainerType) VersionRangeSetRequirement(org.eclipse.n4js.semver.Semver.VersionRangeSetRequirement) INode(org.eclipse.xtext.nodemodel.INode) Check(org.eclipse.xtext.validation.Check) Path(java.nio.file.Path) PackageJsonProperties(org.eclipse.n4js.packagejson.PackageJsonProperties) JSONPackage(org.eclipse.n4js.json.JSON.JSONPackage) ProjectType(org.eclipse.n4js.packagejson.projectDescription.ProjectType) IParseResult(org.eclipse.xtext.parser.IParseResult) FileURI(org.eclipse.n4js.workspace.locations.FileURI) DEFINES_PACKAGE(org.eclipse.n4js.packagejson.PackageJsonProperties.DEFINES_PACKAGE) N4JS(org.eclipse.n4js.packagejson.PackageJsonProperties.N4JS) Collection(java.util.Collection) IMPLEMENTATION_ID(org.eclipse.n4js.packagejson.PackageJsonProperties.IMPLEMENTATION_ID) Set(java.util.Set) EObject(org.eclipse.emf.ecore.EObject) TagVersionRequirement(org.eclipse.n4js.semver.Semver.TagVersionRequirement) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) Collectors(java.util.stream.Collectors) GitHubVersionRequirement(org.eclipse.n4js.semver.Semver.GitHubVersionRequirement) JSONArray(org.eclipse.n4js.json.JSON.JSONArray) List(java.util.List) Stream(java.util.stream.Stream) NPMVersionRequirement(org.eclipse.n4js.semver.Semver.NPMVersionRequirement) SimpleVersion(org.eclipse.n4js.semver.Semver.SimpleVersion) GENERATOR_REWRITE_MODULE_SPECIFIERS(org.eclipse.n4js.packagejson.PackageJsonProperties.GENERATOR_REWRITE_MODULE_SPECIFIERS) IMPLEMENTED_PROJECTS(org.eclipse.n4js.packagejson.PackageJsonProperties.IMPLEMENTED_PROJECTS) Entry(java.util.Map.Entry) Resource(org.eclipse.emf.ecore.resource.Resource) ProjectDescription(org.eclipse.n4js.packagejson.projectDescription.ProjectDescription) Singleton(com.google.inject.Singleton) DEV_DEPENDENCIES(org.eclipse.n4js.packagejson.PackageJsonProperties.DEV_DEPENDENCIES) URI(org.eclipse.emf.common.util.URI) VersionRangeConstraint(org.eclipse.n4js.semver.Semver.VersionRangeConstraint) JSONValue(org.eclipse.n4js.json.JSON.JSONValue) N4JSProjectConfigSnapshot(org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot) Multimap(com.google.common.collect.Multimap) VERSION(org.eclipse.n4js.packagejson.PackageJsonProperties.VERSION) NodeModelUtils(org.eclipse.xtext.nodemodel.util.NodeModelUtils) PROVIDED_RUNTIME_LIBRARIES(org.eclipse.n4js.packagejson.PackageJsonProperties.PROVIDED_RUNTIME_LIBRARIES) TESTED_PROJECTS(org.eclipse.n4js.packagejson.PackageJsonProperties.TESTED_PROJECTS) VENDOR_ID(org.eclipse.n4js.packagejson.PackageJsonProperties.VENDOR_ID) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) InvalidPathException(java.nio.file.InvalidPathException) EClass(org.eclipse.emf.ecore.EClass) N4JSGlobals(org.eclipse.n4js.N4JSGlobals) WorkspaceAccess(org.eclipse.n4js.workspace.WorkspaceAccess) PROJECT_TYPE(org.eclipse.n4js.packagejson.PackageJsonProperties.PROJECT_TYPE) JSONDocument(org.eclipse.n4js.json.JSON.JSONDocument) MAIN_MODULE(org.eclipse.n4js.packagejson.PackageJsonProperties.MAIN_MODULE) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) JSONModelUtils.asNonEmptyStringOrNull(org.eclipse.n4js.json.model.utils.JSONModelUtils.asNonEmptyStringOrNull) ProjectDescriptionUtils(org.eclipse.n4js.utils.ProjectDescriptionUtils) MODULE_FILTERS(org.eclipse.n4js.packagejson.PackageJsonProperties.MODULE_FILTERS) NV_MODULE(org.eclipse.n4js.packagejson.PackageJsonProperties.NV_MODULE) OUTPUT(org.eclipse.n4js.packagejson.PackageJsonProperties.OUTPUT) Iterator(java.util.Iterator) EXTENDED_RUNTIME_ENVIRONMENT(org.eclipse.n4js.packagejson.PackageJsonProperties.EXTENDED_RUNTIME_ENVIRONMENT) SOURCES(org.eclipse.n4js.packagejson.PackageJsonProperties.SOURCES) File(java.io.File) LocalPathVersionRequirement(org.eclipse.n4js.semver.Semver.LocalPathVersionRequirement) SemverHelper(org.eclipse.n4js.semver.SemverHelper) JSONObject(org.eclipse.n4js.json.JSON.JSONObject) Issue(org.eclipse.xtext.validation.Issue) Paths(java.nio.file.Paths) SemverSerializer(org.eclipse.n4js.semver.model.SemverSerializer) NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) File(java.io.File)

Example 3 with JSONStringLiteral

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

the class PackageJsonValidatorExtension method isExistingModule.

/**
 * Tells if for the given moduleSpecifier of the form "a/b/c/M" (without project ID) a module exists in the N4JS
 * project with the given module specifier.
 *
 * Checks if a corresponding .js, .jsx, .n4js, .n4jsx, or .n4jsd file exists in any of the project's source
 * containers.
 */
private boolean isExistingModule(JSONStringLiteral moduleSpecifierLiteral) {
    final URI uri = moduleSpecifierLiteral.eResource().getURI();
    final String moduleSpecifier = moduleSpecifierLiteral.getValue();
    final String relativeModulePath = moduleSpecifier.replace('/', File.separator.charAt(0));
    final Path absoluteProjectPath = getAbsoluteProjectPath(moduleSpecifierLiteral, uri);
    // obtain a stream of File representations of all declared source containers
    final Stream<File> sourceFolders = getAllSourceContainerPaths().stream().map(sourcePath -> new File(absoluteProjectPath.toFile(), sourcePath));
    // using any of the aforementioned file extensions
    return sourceFolders.filter(sourceFolder -> // check each file extension
    N4JSGlobals.ALL_N4_FILE_EXTENSIONS.stream().filter(ext -> new File(sourceFolder, relativeModulePath + "." + ext).exists()).findAny().isPresent()).findAny().isPresent();
}
Also used : Path(java.nio.file.Path) REQUIRED_RUNTIME_LIBRARIES(org.eclipse.n4js.packagejson.PackageJsonProperties.REQUIRED_RUNTIME_LIBRARIES) IssueCodes(org.eclipse.n4js.validation.IssueCodes) JSONStringLiteral(org.eclipse.n4js.json.JSON.JSONStringLiteral) Inject(com.google.inject.Inject) NV_SOURCE_CONTAINER(org.eclipse.n4js.packagejson.PackageJsonProperties.NV_SOURCE_CONTAINER) NAME(org.eclipse.n4js.packagejson.PackageJsonProperties.NAME) FileUtils(org.eclipse.n4js.utils.io.FileUtils) ProjectNameInfo(org.eclipse.n4js.utils.ProjectDescriptionUtils.ProjectNameInfo) URLVersionRequirement(org.eclipse.n4js.semver.Semver.URLVersionRequirement) VENDOR_NAME(org.eclipse.n4js.packagejson.PackageJsonProperties.VENDOR_NAME) HashMultimap(com.google.common.collect.HashMultimap) PackageJsonUtils(org.eclipse.n4js.packagejson.PackageJsonUtils) DEPENDENCIES(org.eclipse.n4js.packagejson.PackageJsonProperties.DEPENDENCIES) ModuleFilterType(org.eclipse.n4js.packagejson.projectDescription.ModuleFilterType) Optional(com.google.common.base.Optional) Map(java.util.Map) SourceContainerType(org.eclipse.n4js.packagejson.projectDescription.SourceContainerType) VersionRangeSetRequirement(org.eclipse.n4js.semver.Semver.VersionRangeSetRequirement) INode(org.eclipse.xtext.nodemodel.INode) Check(org.eclipse.xtext.validation.Check) Path(java.nio.file.Path) PackageJsonProperties(org.eclipse.n4js.packagejson.PackageJsonProperties) JSONPackage(org.eclipse.n4js.json.JSON.JSONPackage) ProjectType(org.eclipse.n4js.packagejson.projectDescription.ProjectType) IParseResult(org.eclipse.xtext.parser.IParseResult) FileURI(org.eclipse.n4js.workspace.locations.FileURI) DEFINES_PACKAGE(org.eclipse.n4js.packagejson.PackageJsonProperties.DEFINES_PACKAGE) N4JS(org.eclipse.n4js.packagejson.PackageJsonProperties.N4JS) Collection(java.util.Collection) IMPLEMENTATION_ID(org.eclipse.n4js.packagejson.PackageJsonProperties.IMPLEMENTATION_ID) Set(java.util.Set) EObject(org.eclipse.emf.ecore.EObject) TagVersionRequirement(org.eclipse.n4js.semver.Semver.TagVersionRequirement) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) Collectors(java.util.stream.Collectors) GitHubVersionRequirement(org.eclipse.n4js.semver.Semver.GitHubVersionRequirement) JSONArray(org.eclipse.n4js.json.JSON.JSONArray) List(java.util.List) Stream(java.util.stream.Stream) NPMVersionRequirement(org.eclipse.n4js.semver.Semver.NPMVersionRequirement) SimpleVersion(org.eclipse.n4js.semver.Semver.SimpleVersion) GENERATOR_REWRITE_MODULE_SPECIFIERS(org.eclipse.n4js.packagejson.PackageJsonProperties.GENERATOR_REWRITE_MODULE_SPECIFIERS) IMPLEMENTED_PROJECTS(org.eclipse.n4js.packagejson.PackageJsonProperties.IMPLEMENTED_PROJECTS) Entry(java.util.Map.Entry) Resource(org.eclipse.emf.ecore.resource.Resource) ProjectDescription(org.eclipse.n4js.packagejson.projectDescription.ProjectDescription) Singleton(com.google.inject.Singleton) DEV_DEPENDENCIES(org.eclipse.n4js.packagejson.PackageJsonProperties.DEV_DEPENDENCIES) URI(org.eclipse.emf.common.util.URI) VersionRangeConstraint(org.eclipse.n4js.semver.Semver.VersionRangeConstraint) JSONValue(org.eclipse.n4js.json.JSON.JSONValue) N4JSProjectConfigSnapshot(org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot) Multimap(com.google.common.collect.Multimap) VERSION(org.eclipse.n4js.packagejson.PackageJsonProperties.VERSION) NodeModelUtils(org.eclipse.xtext.nodemodel.util.NodeModelUtils) PROVIDED_RUNTIME_LIBRARIES(org.eclipse.n4js.packagejson.PackageJsonProperties.PROVIDED_RUNTIME_LIBRARIES) TESTED_PROJECTS(org.eclipse.n4js.packagejson.PackageJsonProperties.TESTED_PROJECTS) VENDOR_ID(org.eclipse.n4js.packagejson.PackageJsonProperties.VENDOR_ID) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) InvalidPathException(java.nio.file.InvalidPathException) EClass(org.eclipse.emf.ecore.EClass) N4JSGlobals(org.eclipse.n4js.N4JSGlobals) WorkspaceAccess(org.eclipse.n4js.workspace.WorkspaceAccess) PROJECT_TYPE(org.eclipse.n4js.packagejson.PackageJsonProperties.PROJECT_TYPE) JSONDocument(org.eclipse.n4js.json.JSON.JSONDocument) MAIN_MODULE(org.eclipse.n4js.packagejson.PackageJsonProperties.MAIN_MODULE) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) JSONModelUtils.asNonEmptyStringOrNull(org.eclipse.n4js.json.model.utils.JSONModelUtils.asNonEmptyStringOrNull) ProjectDescriptionUtils(org.eclipse.n4js.utils.ProjectDescriptionUtils) MODULE_FILTERS(org.eclipse.n4js.packagejson.PackageJsonProperties.MODULE_FILTERS) NV_MODULE(org.eclipse.n4js.packagejson.PackageJsonProperties.NV_MODULE) OUTPUT(org.eclipse.n4js.packagejson.PackageJsonProperties.OUTPUT) Iterator(java.util.Iterator) EXTENDED_RUNTIME_ENVIRONMENT(org.eclipse.n4js.packagejson.PackageJsonProperties.EXTENDED_RUNTIME_ENVIRONMENT) SOURCES(org.eclipse.n4js.packagejson.PackageJsonProperties.SOURCES) File(java.io.File) LocalPathVersionRequirement(org.eclipse.n4js.semver.Semver.LocalPathVersionRequirement) SemverHelper(org.eclipse.n4js.semver.SemverHelper) JSONObject(org.eclipse.n4js.json.JSON.JSONObject) Issue(org.eclipse.xtext.validation.Issue) Paths(java.nio.file.Paths) SemverSerializer(org.eclipse.n4js.semver.model.SemverSerializer) NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) FileURI(org.eclipse.n4js.workspace.locations.FileURI) URI(org.eclipse.emf.common.util.URI) File(java.io.File)

Example 4 with JSONStringLiteral

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

the class PackageJsonValidatorExtension method checkIsDependenciesSection.

/**
 * Checks whether the given {@code sectionValue} is a structurally valid package.json dependency section (including
 * the version constraints).
 */
private void checkIsDependenciesSection(JSONValue sectionValue) {
    if (!checkIsType(sectionValue, JSONPackage.Literals.JSON_OBJECT, "as list of dependencies")) {
        return;
    }
    final JSONObject dependenciesObject = (JSONObject) sectionValue;
    for (NameValuePair entry : dependenciesObject.getNameValuePairs()) {
        final JSONValue versionRequirement = entry.getValue();
        if (checkIsType(versionRequirement, JSONPackage.Literals.JSON_STRING_LITERAL, "as version specifier")) {
            JSONStringLiteral jsonStringVersionRequirement = (JSONStringLiteral) versionRequirement;
            String constraintValue = jsonStringVersionRequirement.getValue();
            validateSemver(jsonStringVersionRequirement, constraintValue);
        }
    }
}
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)

Example 5 with JSONStringLiteral

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

the class PackageJsonValidatorExtension method checkImplementationId.

/**
 * Validates basic properties of the {@code n4js.implementationId}.
 */
@CheckProperty(property = IMPLEMENTATION_ID)
public void checkImplementationId(JSONValue value) {
    final JSONArray implementedProjectsValue = getSingleDocumentValue(IMPLEMENTED_PROJECTS, JSONArray.class);
    // check basic constraints
    if (!checkIsType(value, JSONPackage.Literals.JSON_STRING_LITERAL, "as implementation ID")) {
        return;
    }
    if (!checkIsNonEmptyString((JSONStringLiteral) value, IMPLEMENTATION_ID)) {
        return;
    }
    final JSONStringLiteral implementationId = (JSONStringLiteral) value;
    // at this point we may assume that the implementationId was set
    if ((implementedProjectsValue == null || implementedProjectsValue.getElements().isEmpty())) {
        // missing implemented projects
        addIssue(IssueCodes.getMessageForPKGJ_APIIMPL_MISSING_IMPL_PROJECTS(), implementationId.eContainer(), JSONPackage.Literals.NAME_VALUE_PAIR__NAME, IssueCodes.PKGJ_APIIMPL_MISSING_IMPL_PROJECTS);
    }
}
Also used : JSONArray(org.eclipse.n4js.json.JSON.JSONArray) JSONStringLiteral(org.eclipse.n4js.json.JSON.JSONStringLiteral)

Aggregations

JSONStringLiteral (org.eclipse.n4js.json.JSON.JSONStringLiteral)18 JSONObject (org.eclipse.n4js.json.JSON.JSONObject)11 NameValuePair (org.eclipse.n4js.json.JSON.NameValuePair)11 JSONArray (org.eclipse.n4js.json.JSON.JSONArray)9 JSONValue (org.eclipse.n4js.json.JSON.JSONValue)9 ArrayList (java.util.ArrayList)7 List (java.util.List)7 URI (org.eclipse.emf.common.util.URI)7 SourceContainerType (org.eclipse.n4js.packagejson.projectDescription.SourceContainerType)7 Map (java.util.Map)6 EObject (org.eclipse.emf.ecore.EObject)6 Resource (org.eclipse.emf.ecore.resource.Resource)6 JSONDocument (org.eclipse.n4js.json.JSON.JSONDocument)6 Optional (com.google.common.base.Optional)5 HashMultimap (com.google.common.collect.HashMultimap)5 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)5 Multimap (com.google.common.collect.Multimap)5 Inject (com.google.inject.Inject)5 Singleton (com.google.inject.Singleton)5 File (java.io.File)5