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);
}
}
}
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);
}
}
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();
}
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);
}
}
}
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);
}
}
Aggregations