Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(390)

Unified Diff: third_party/WebKit/Source/devtools/scripts/jsdoc-validator/src/org/chromium/devtools/jsdoc/FileCheckerCallable.java

Issue 2464463002: Revert of DevTools: clean up scripts folder (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/devtools/scripts/jsdoc-validator/src/org/chromium/devtools/jsdoc/FileCheckerCallable.java
diff --git a/third_party/WebKit/Source/devtools/scripts/jsdoc-validator/src/org/chromium/devtools/jsdoc/FileCheckerCallable.java b/third_party/WebKit/Source/devtools/scripts/jsdoc-validator/src/org/chromium/devtools/jsdoc/FileCheckerCallable.java
new file mode 100644
index 0000000000000000000000000000000000000000..c2dae58a3e589c37b0a77475fe976ce817bb8430
--- /dev/null
+++ b/third_party/WebKit/Source/devtools/scripts/jsdoc-validator/src/org/chromium/devtools/jsdoc/FileCheckerCallable.java
@@ -0,0 +1,114 @@
+package org.chromium.devtools.jsdoc;
+
+import com.google.javascript.jscomp.Compiler;
+import com.google.javascript.jscomp.NodeTraversal;
+import com.google.javascript.jscomp.parsing.Config;
+import com.google.javascript.jscomp.parsing.Config.LanguageMode;
+import com.google.javascript.jscomp.parsing.ParserRunner;
+import com.google.javascript.rhino.ErrorReporter;
+import com.google.javascript.rhino.Node;
+
+import org.chromium.devtools.jsdoc.checks.ContextTrackingValidationCheck;
+
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.FileSystems;
+import java.nio.file.Files;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.Callable;
+
+public class FileCheckerCallable implements Callable<ValidatorContext> {
+
+ private static Set<String> EXTRA_ANNOTATIONS = new HashSet<>(Arrays.asList(
+ "suppressReceiverCheck",
+ "suppressGlobalPropertiesCheck"
+ ));
+ private final String fileName;
+
+ public FileCheckerCallable(String fileName) {
+ this.fileName = fileName;
+ }
+
+ @Override
+ public ValidatorContext call() {
+ try {
+ ValidatorContext context = new ValidatorContext(readScriptText(), fileName);
+ ValidationCheckDispatcher dispatcher = new ValidationCheckDispatcher(context);
+ dispatcher.registerCheck(new ContextTrackingValidationCheck());
+ NodeTraversal.traverse(new Compiler(), parseScript(context), dispatcher);
+ return context;
+ } catch (FileNotFoundException e) {
+ logError("File not found: " + fileName);
+ } catch (IOException e) {
+ logError("Failed to read file " + fileName);
+ }
+ return null;
+ }
+
+ private String readScriptText() throws IOException {
+ byte[] encoded = Files.readAllBytes(FileSystems.getDefault().getPath(fileName));
+ String text = StandardCharsets.UTF_8.decode(ByteBuffer.wrap(encoded)).toString();
+ return text;
+ }
+
+ private static Node parseScript(final ValidatorContext context) {
+ Config config = ParserRunner.createConfig(
+ true, true, true, LanguageMode.ECMASCRIPT5_STRICT, EXTRA_ANNOTATIONS);
+ ErrorReporter errorReporter = new ErrorReporter() {
+ @Override
+ public void warning(String message, String sourceName, int line, int lineOffset) {
+ // Ignore.
+ }
+
+ @Override
+ public void error(String message, String sourceName, int line, int lineOffset) {
+ logError("at " + sourceName + ":" + line + ":" + lineOffset);
+ }
+ };
+ try {
+ return ParserRunner.parse(
+ context.sourceFile, context.sourceFile.getCode(), config, errorReporter).ast;
+ } catch (IOException e) {
+ // Does not happen with preloaded files.
+ return null;
+ }
+ }
+
+ private static void logError(String message) {
+ System.err.println("ERROR: " + message);
+ }
+
+ private static class ValidationCheckDispatcher extends DoDidVisitorAdapter {
+ private final List<ValidationCheck> checks = new ArrayList<>(2);
+ private final ValidatorContext context;
+
+ public ValidationCheckDispatcher(ValidatorContext context) {
+ this.context = context;
+ }
+
+ public void registerCheck(ValidationCheck check) {
+ check.setContext(context);
+ checks.add(check);
+ }
+
+ @Override
+ public void doVisit(Node node) {
+ for (DoDidNodeVisitor visitor : checks) {
+ visitor.doVisit(node);
+ }
+ }
+
+ @Override
+ public void didVisit(Node node) {
+ for (ValidationCheck check : checks) {
+ check.didVisit(node);
+ }
+ }
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698