Use jshint instead instead of jslint in js.tests.

Adapted from https://github.com/develar/kotlin/commit/a9e0a42fb1347fa8e21c86b5a073ef8a7c873da0.
This commit is contained in:
Pavel V. Talanov
2012-08-13 18:28:16 +04:00
parent 53994f15d3
commit 554b43df68
5 changed files with 4623 additions and 237 deletions
@@ -51,8 +51,6 @@ public abstract class BasicTest extends KotlinTestWithEnvironment {
private static final String OUT = "out/";
private static final String EXPECTED = "expected/";
public static final String JSLINT_LIB = pathToTestFilesRoot() + "jslint.js";
@NotNull
private String mainDirectory = "";
@@ -1,44 +0,0 @@
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.k2js.test.rhino;
import org.jetbrains.annotations.NotNull;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.Scriptable;
/**
* @author Sergey Simonchik
*/
class FunctionWithScope {
private final Function fun;
private final Scriptable scope;
FunctionWithScope(@NotNull Function function, @NotNull Scriptable scope) {
this.fun = function;
this.scope = scope;
}
@NotNull
public Function getFunction() {
return fun;
}
@NotNull
public Scriptable getScope() {
return scope;
}
}
@@ -1,106 +0,0 @@
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.k2js.test.rhino;
import com.google.common.base.Supplier;
import com.intellij.openapi.diagnostic.Logger;
import org.jetbrains.annotations.NotNull;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.Script;
import org.mozilla.javascript.Scriptable;
/**
* @author Sergey Simonchik
*/
class RhinoFunctionManager {
private static final Logger LOG = Logger.getInstance(RhinoFunctionManager.class);
private final ThreadLocal<FunctionWithScope> threadLocalFunction = new ThreadLocal<FunctionWithScope>() {
@Override
protected FunctionWithScope initialValue() {
if (script == null) {
synchronized (threadLocalFunction) {
if (script == null) {
script = compileScript(9);
}
}
}
return extractFunctionWithScope(script);
}
};
private volatile Script script;
private final Supplier<String> scriptSourceProvider;
private final String functionName;
public RhinoFunctionManager(@NotNull Supplier<String> scriptSourceProvider,
@NotNull String functionName) {
this.scriptSourceProvider = scriptSourceProvider;
this.functionName = functionName;
}
private Script compileScript(int optimizationLevel) {
long startNano = System.nanoTime();
Context context = Context.enter();
try {
context.setOptimizationLevel(optimizationLevel);
String scriptSource = scriptSourceProvider.get();
return context.compileString(scriptSource, "<" + functionName + " script>", 1, null);
}
finally {
Context.exit();
LOG.info(formatMessage(startNano, functionName + " script rhino compilation"));
}
}
@NotNull
private FunctionWithScope extractFunctionWithScope(@NotNull Script script) {
long startNano = System.nanoTime();
Context context = Context.enter();
try {
Scriptable scope = context.initStandardObjects();
script.exec(context, scope);
Object jsLintObj = scope.get(functionName, scope);
if (jsLintObj instanceof Function) {
Function jsLint = (Function) jsLintObj;
return new FunctionWithScope(jsLint, scope);
}
else {
throw new RuntimeException(functionName + " is undefined or not a function.");
}
}
finally {
Context.exit();
LOG.info(formatMessage(startNano, functionName + " function extraction"));
}
}
private static String formatMessage(long startTimeNano, @NotNull String actionName) {
long nanoDuration = System.nanoTime() - startTimeNano;
return String.format("[%s] %s took %.2f ms",
Thread.currentThread().getName(),
actionName,
nanoDuration / 1000000.0);
}
@NotNull
public FunctionWithScope getFunctionWithScope() {
return threadLocalFunction.get();
}
}
@@ -17,18 +17,15 @@
package org.jetbrains.k2js.test.rhino;
import closurecompiler.internal.com.google.common.collect.Maps;
import com.google.common.base.Supplier;
import com.google.common.collect.Sets;
import com.intellij.openapi.util.io.FileUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.k2js.config.EcmaVersion;
import org.jetbrains.k2js.facade.K2JSTranslator;
import org.jetbrains.k2js.test.BasicTest;
import org.mozilla.javascript.*;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
@@ -42,71 +39,90 @@ import static org.jetbrains.k2js.test.BasicTest.pathToTestFilesRoot;
* @author Pavel Talanov
*/
public final class RhinoUtils {
@NotNull
private static final Set<String> IGNORED_JSLINT_WARNINGS = Sets.newHashSet();
static {
// todo dart ast bug
IGNORED_JSLINT_WARNINGS.add("Unexpected space between '}' and '('.");
// don't read JS, use kotlin and idea debugger ;)
IGNORED_JSLINT_WARNINGS
.add("Wrap an immediate function invocation in parentheses to assist the reader in understanding that the expression is the result of a function, and not the function itself.");
}
@NotNull
private static final RhinoFunctionManager functionManager = new RhinoFunctionManager(
new Supplier<String>() {
@Override
public String get() {
return fileToString(BasicTest.JSLINT_LIB);
}
},
"JSLINT"
);
public static final String KOTLIN_JS_LIB_COMMON = pathToTestFilesRoot() + "kotlin_lib.js";
private static final String KOTLIN_JS_LIB_ECMA_3 = pathToTestFilesRoot() + "kotlin_lib_ecma3.js";
private static final String KOTLIN_JS_LIB_ECMA_5 = pathToTestFilesRoot() + "kotlin_lib_ecma5.js";
private static final Set<String> IGNORED_JSHINT_WARNINGS = Sets.newHashSet();
private static final NativeObject JSHINT_OPTIONS = new NativeObject();
@NotNull
private static final Map<EcmaVersion, Scriptable> versionToScope = Maps.newHashMap();
private static final Map<EcmaVersion, ScriptableObject> versionToScope = Maps.newHashMap();
private RhinoUtils() {
static {
// don't read JS, use kotlin and idea debugger ;)
//IGNORED_JSHINT_WARNINGS.add(
// "Wrap an immediate function invocation in parentheses to assist the reader in understanding that the expression is the result of a function, and not the function itself.");
//IGNORED_JSHINT_WARNINGS.add("Expected exactly one space between ';' and 'else'.");
//// stupid jslint, see $initializer fun
//IGNORED_JSHINT_WARNINGS.add("Do not wrap function literals in parens unless they are to be immediately invoked.");
//// stupid jslint
//IGNORED_JSHINT_WARNINGS.add("'_' was used before it was defined.");
//IGNORED_JSHINT_WARNINGS.add("Empty block.");
IGNORED_JSHINT_WARNINGS.add("Expected to see a statement and instead saw a block.");
//IGNORED_JSHINT_WARNINGS.add("Unexpected '.'.");
//// todo
//IGNORED_JSHINT_WARNINGS.add("Strange loop.");
//IGNORED_JSHINT_WARNINGS.add("Weird relation.");
//IGNORED_JSHINT_WARNINGS.add("Weird condition.");
//IGNORED_JSHINT_WARNINGS.add("Expected ';' and instead saw ','.");
//IGNORED_JSHINT_WARNINGS.add("Expected an identifier and instead saw ','.");
//// it is normal,
//IGNORED_JSHINT_WARNINGS.add("Unexpected 'else' after 'return'.");
IGNORED_JSHINT_WARNINGS.add("Expected ')' and instead saw 'return'.");
//IGNORED_JSHINT_WARNINGS.add()
// todo fix dart ast?
//JSHINT_OPTIONS.defineProperty("white", true, ScriptableObject.READONLY);
// vars, http://uxebu.com/blog/2010/04/02/one-var-statement-for-one-varia
// ble/
//JSHINT_OPTIONS.defineProperty("vars", true, ScriptableObject.READONLY);
NativeArray globals = new NativeArray(new Object[] {"Kotlin"});
JSHINT_OPTIONS.defineProperty("predef", globals, ScriptableObject.READONLY);
// todo
JSHINT_OPTIONS.defineProperty("expr", true, ScriptableObject.READONLY);
JSHINT_OPTIONS.defineProperty("asi", true, ScriptableObject.READONLY);
JSHINT_OPTIONS.defineProperty("laxcomma", true, ScriptableObject.READONLY);
//JSHINT_OPTIONS.defineProperty("nomen", true, ScriptableObject.READONLY);
//JSHINT_OPTIONS.defineProperty("continue", true, ScriptableObject.READONLY);
//JSHINT_OPTIONS.defineProperty("plusplus", true, ScriptableObject.READONLY);
//JSHINT_OPTIONS.defineProperty("evil", true, ScriptableObject.READONLY);
//JSHINT_OPTIONS.defineProperty("indent", 2, ScriptableObject.READONLY);
}
private static String fileToString(String file) {
try {
return FileUtil.loadFile(new File(file));
}
catch (IOException e) {
throw new RuntimeException(e);
}
private RhinoUtils() {
}
private static void runFileWithRhino(@NotNull String inputFile,
@NotNull Context context,
@NotNull Scriptable scope) throws Exception {
FileReader reader = new FileReader(inputFile);
String result;
try {
context.evaluateReader(scope, reader, inputFile, 1, null);
result = FileUtil.loadFile(new File(inputFile));
}
finally {
reader.close();
catch (IOException e) {
throw new RuntimeException(e);
}
context.evaluateString(scope, result, inputFile, 1, null);
}
public static void runRhinoTest(@NotNull List<String> fileNames,
@NotNull RhinoResultChecker checker) throws Exception {
runRhinoTest(fileNames, checker, null, EcmaVersion.defaultVersion());
public static void runRhinoTest(@NotNull List<String> fileNames, @NotNull RhinoResultChecker checker) throws Exception {
runRhinoTest(fileNames, checker, EcmaVersion.defaultVersion());
}
public static void runRhinoTest(@NotNull List<String> fileNames, @NotNull RhinoResultChecker checker, @NotNull EcmaVersion ecmaVersion)
throws Exception {
runRhinoTest(fileNames, checker, null, ecmaVersion);
}
public static void runRhinoTest(@NotNull List<String> fileNames,
@NotNull RhinoResultChecker checker,
@Nullable Map<String, Object> variables,
@NotNull EcmaVersion ecmaVersion) throws Exception {
runRhinoTest(fileNames, checker, variables, ecmaVersion, Collections.<String>emptyList());
@NotNull EcmaVersion ecmaVersion)
throws Exception {
runRhinoTest(fileNames, checker, variables, ecmaVersion, Collections.<String>emptyList());
}
public static void runRhinoTest(@NotNull List<String> fileNames,
@@ -116,14 +132,18 @@ public final class RhinoUtils {
@NotNull List<String> jsLibraries) throws Exception {
Context context = createContext(ecmaVersion);
try {
Scriptable scope = getScope(ecmaVersion, context, jsLibraries);
ScriptableObject scope = getScope(ecmaVersion, context, jsLibraries);
putGlobalVariablesIntoScope(scope, variables);
for (String filename : fileNames) {
runFileWithRhino(filename, context, scope);
String problems = lintIt(context, filename, scope);
if (problems != null) {
//fail(problems);
//noinspection UseOfSystemOutOrSystemErr
System.out.print(problems);
}
}
checker.runChecks(context, scope);
lintIt(context, fileNames.get(fileNames.size() - 1));
}
finally {
Context.exit();
@@ -131,31 +151,34 @@ public final class RhinoUtils {
}
@NotNull
private static Scriptable getScope(@NotNull EcmaVersion version, @NotNull Context context,
@NotNull List<String> jsLibraries) {
private static ScriptableObject getScope(@NotNull EcmaVersion version, @NotNull Context context, @NotNull List<String> jsLibraries) {
ScriptableObject scope = context.initStandardObjects(null, false);
scope.setParentScope(getParentScope(version, context, jsLibraries));
return scope;
}
@NotNull
private static Scriptable getParentScope(@NotNull EcmaVersion version, @NotNull Context context,
@NotNull List<String> jsLibraries) {
Scriptable parentScope = versionToScope.get(version);
private static Scriptable getParentScope(@NotNull EcmaVersion version, @NotNull Context context, @NotNull List<String> jsLibraries) {
ScriptableObject parentScope = versionToScope.get(version);
if (parentScope == null) {
parentScope = initScope(version, context, jsLibraries);
versionToScope.put(version, parentScope);
}
else {
NativeObject kotlin = (NativeObject) parentScope.get("Kotlin");
kotlin.put("modules", kotlin, new NativeObject());
}
return parentScope;
}
@NotNull
private static Scriptable initScope(@NotNull EcmaVersion version, @NotNull Context context,
@NotNull List<String> jsLibraries) {
ScriptableObject scope = context.initStandardObjects(null, false);
private static ScriptableObject initScope(@NotNull EcmaVersion version, @NotNull Context context, @NotNull List<String> jsLibraries) {
ScriptableObject scope = context.initStandardObjects();
try {
runFileWithRhino(getKotlinLibFile(version), context, scope);
runFileWithRhino(KOTLIN_JS_LIB_COMMON, context, scope);
runFileWithRhino(pathToTestFilesRoot() + "kotlin_lib.js", context, scope);
runFileWithRhino(pathToTestFilesRoot() + "maps.js", context, scope);
runFileWithRhino(pathToTestFilesRoot() + "jshint.js", context, scope);
for (String jsLibrary : jsLibraries) {
runFileWithRhino(jsLibrary, context, scope);
}
@@ -163,11 +186,10 @@ public final class RhinoUtils {
catch (Exception e) {
throw rethrow(e);
}
scope.sealObject();
//scope.sealObject();
return scope;
}
//TODO:
@NotNull
private static Context createContext(@NotNull EcmaVersion ecmaVersion) {
@@ -200,34 +222,20 @@ public final class RhinoUtils {
context.evaluateString(scope, K2JSTranslator.FLUSH_SYSTEM_OUT, "test", 0, null);
}
private static void lintIt(Context context, String fileName) throws IOException {
@Nullable
private static String lintIt(Context context, String fileName, ScriptableObject scope) throws IOException {
if (Boolean.valueOf(System.getProperty("test.lint.skip"))) {
return;
return null;
}
NativeObject options = new NativeObject();
// todo fix dart ast?
options.defineProperty("white", true, ScriptableObject.READONLY);
// vars, http://uxebu.com/blog/2010/04/02/one-var-statement-for-one-variable/
options.defineProperty("vars", true, ScriptableObject.READONLY);
NativeArray globals = new NativeArray(new Object[] {"Kotlin"});
options.defineProperty("predef", globals, ScriptableObject.READONLY);
Object[] args = {FileUtil.loadFile(new File(fileName)), options};
FunctionWithScope functionWithScope = functionManager.getFunctionWithScope();
Function function = functionWithScope.getFunction();
Scriptable scope = functionWithScope.getScope();
Object status = function.call(context, scope, scope, args);
Boolean noErrors = (Boolean) Context.jsToJava(status, Boolean.class);
if (!noErrors) {
Object[] args = {FileUtil.loadFile(new File(fileName)), JSHINT_OPTIONS};
Function function = (Function) ScriptableObject.getProperty(scope.getParentScope(), "JSHINT");
Object status = function.call(context, scope.getParentScope(), scope.getParentScope(), args);
if (!(Boolean) Context.jsToJava(status, Boolean.class)) {
Object errors = function.get("errors", scope);
if (errors == null) {
return;
}
System.out.println(fileName);
StringBuilder sb = new StringBuilder(fileName);
for (Object errorObj : ((NativeArray) errors)) {
if (!(errorObj instanceof NativeObject)) {
if (errorObj == null) {
continue;
}
@@ -240,14 +248,21 @@ public final class RhinoUtils {
Object reasonObj = e.get("reason");
if (reasonObj instanceof String) {
String reason = (String) reasonObj;
if (IGNORED_JSLINT_WARNINGS.contains(reason)) {
if (IGNORED_JSHINT_WARNINGS.contains(reason) ||
reason.startsWith("Expected exactly one space between ')' and ") ||
reason.startsWith("Expected '}' to match '{' from line ") ||
reason.startsWith("Expected '{' and instead saw ")) {
continue;
}
System.out.println(line + ":" + character + " " + reason);
sb.append('\n').append(line).append(':').append(character).append(' ').append(reason);
}
}
return sb.length() == fileName.length() ? null : sb.toString();
}
return null;
}
private static int toInt(Object obj) {
@@ -256,4 +271,4 @@ public final class RhinoUtils {
}
return -1;
}
}
}
File diff suppressed because it is too large Load Diff