public LinkedList<JSHintError> lint(Document d) { Context cx = Context.enter(); LinkedList<JSHintError> result = new LinkedList<>(); FileObject fo = NbEditorUtilities.getFileObject(d); try { Scriptable config = getConfig(cx, fo); String code = d.getText(0, d.getLength()); result = lint(cx, code, config); } catch (IOException | ParseException | BadLocationException ex) { Exceptions.printStackTrace(ex); } finally { Context.exit(); } return result; }
public LinkedList<JSHintError> lint(FileObject fo) { Context cx = Context.enter(); LinkedList<JSHintError> result = new LinkedList<>(); try { Scriptable config = getConfig(cx, fo); String code = fo.asText(); result = lint(cx, code, config); } catch (IOException | ParseException ex) { Exceptions.printStackTrace(ex); } finally { Context.exit(); } return result; }
@NbBundle.Messages({ "LBL_InvalidRC=Invalid .jshintrc", "# {0} - Linted JavaScript file", "# {1} - RC file", "DESC_InvalidRC=File {0} was linted with default configuration because its related {1} is not valid json", "ICON_InvalidRC=" }) private Scriptable getConfig(Context cx, FileObject fo) throws ParseException, IOException { JsonParser parser = new JsonParser(cx, scope); FileObject config = findConfig(fo); if (config == null) { return cx.newObject(scope); } String json = config.asText(); try { return (Scriptable) parser.parseValue(json); } catch (ParseException ex) { JTextArea text = new JTextArea(Bundle.DESC_InvalidRC(fo.getPath(), config.getPath())); NotificationDisplayer.getDefault().notify(Bundle.LBL_InvalidRC(), new ImageIcon(Bundle.ICON_InvalidRC()), text, text, NotificationDisplayer.Priority.NORMAL); return cx.newObject(scope); } }
@Override public void putJson(final String varName, final String json) { runWithContext(new RhinoCallable<Object, RuntimeException>() { @Override protected Object doCall(Context cx, Scriptable scope) throws RuntimeException { try { Object obj = new JsonParser(cx, scope).parseValue(json); scope.put(varName, scope, obj); return null; } catch (ParseException e) { throw Throwables.propagate(e); } } }); }
@Override public Object coerce(Object obj, Type type) { Context cx = Context.enter(); try { if (obj instanceof String) { return new JsonParser(cx, cx.initStandardObjects()).parseValue((String) obj); } return obj; } catch (ParseException e) { // just assume it's actually a string return obj; } }
@Test(expected = ParseException.class) public void shouldFailToParseIllegalWhitespaceChars() throws Exception { parser.parseValue(" \u000b 1"); }
@Test(expected = ParseException.class) public void shouldFailToParseJavaNull() throws Exception { parser.parseValue(null); }
@Test(expected = ParseException.class) public void shouldFailToParseDoubleNegativeNumbers() throws Exception { parser.parseValue("--5"); }
@Test(expected = ParseException.class) public void shouldFailToParseNumbersWithDecimalExponent() throws Exception { parser.parseValue("5e5.5"); }
@Test(expected = ParseException.class) public void shouldFailToParseNumbersBeginningWithZero() throws Exception { parser.parseValue("05"); }
@Test(expected = ParseException.class) public void shouldFailToParseEmptyJavaString() throws Exception { parser.parseValue(""); }
@Test(expected = ParseException.class) public void shouldFailToParseSingleDoubleQuote() throws Exception { parser.parseValue(str('"')); }
@Test(expected = ParseException.class) public void shouldFailToParseStringContainingSingleBackslash() throws Exception { parser.parseValue(str('"', '\\', '"')); }
@Test(expected = ParseException.class) public void shouldFailToParseStringIllegalStringChars() throws Exception { parser.parseValue(str('"', '\n', '"')); }
@Test(expected = ParseException.class) public void shouldFailToParseArrayWithInvalidElements() throws Exception { parser.parseValue("[wtf]"); }
@Test(expected = ParseException.class) public void shouldFailToParseJsonObjectsWithInvalidFormat() throws Exception { parser.parseValue("{\"only\", \"keys\"}"); }
@Test(expected = ParseException.class) public void shouldFailToParseMoreThanOneToplevelValue() throws Exception { parser.parseValue("1 2"); }
@Test(expected = ParseException.class) public void shouldFailToParseStringTruncatedUnicode() throws Exception { parser.parseValue("\"\\u00f\""); }
@Test(expected = ParseException.class) public void shouldFailToParseStringControlChars1() throws Exception { parser.parseValue("\"\u0000\""); }
@Test(expected = ParseException.class) public void shouldFailToParseStringControlChars2() throws Exception { parser.parseValue("\"\u001f\""); }
@Test(expected = ParseException.class) public void shouldThrowParseExceptionWhenIncompleteObject() throws Exception { parser.parseValue("{\"a\" "); }
@Test(expected = ParseException.class) public void shouldThrowParseExceptionWhenIncompleteArray() throws Exception { parser.parseValue("[1 "); }
@Test(expected = ParseException.class) public void shouldFailToParseIllegalUnicodeEscapeSeq() throws Exception { parser.parseValue("\"\\u-123\""); }