@Override public void visitJSNewExpression(JSNewExpression jsNewExpression) { super.visitJSNewExpression( jsNewExpression); final JSExpression methodExpression; try { methodExpression = jsNewExpression.getMethodExpression(); } catch (Exception e) { return; //catching an intelliJ CCE } if (!(methodExpression instanceof JSReferenceExpression)) { return; } final JSReferenceExpression referenceExpression = (JSReferenceExpression) methodExpression; final JSExpression qualifier = referenceExpression.getQualifier(); if (qualifier != null) { return; } @NonNls final String methodName = referenceExpression.getReferencedName(); if (!"Function".equals(methodName)) { return; } registerError(methodExpression); }
@Override public void invoke(@NotNull Project project, @NotNull PsiFile psiFile, @Nullable("is null when called from inspection") Editor editor, @NotNull PsiElement element, @NotNull PsiElement end) throws IncorrectOperationException { PsiElement parent = element.getParent(); if (!(parent instanceof JSNewExpression)) return; final JSExpressionCodeFragment useStrict = JSElementFactory.createExpressionCodeFragment(project, getNewExp(), parent); PsiElement child = useStrict.getFirstChild(); parent.replace(child); }
public static boolean isLHSExpression(JSExpression expr) { if(expr instanceof JSDefinitionExpression) { expr = ((JSDefinitionExpression) expr).getExpression(); } if(expr instanceof JSReferenceExpression) { return true; } if(expr instanceof JSParenthesizedExpression) { return isLHSExpression(((JSParenthesizedExpression) expr).getInnerExpression()); } if(expr instanceof JSIndexedPropertyAccessExpression) { return true; } if(expr instanceof JSCallExpression) { return true; } if(expr instanceof JSNewExpression) { return true; } return false; }
@Override public void visitJSExpressionStatement( @NotNull JSExpressionStatement statement) { super.visitJSExpressionStatement(statement); if (!(statement.getExpression()instanceof JSNewExpression)) { return; } final JSNewExpression newExpression = (JSNewExpression) statement.getExpression(); final JSExpression methodExpression = newExpression.getMethodExpression(); if (methodExpression == null) { return; } registerError(methodExpression); }
private static boolean newExpressionDefinitelyRecurses(JSNewExpression exp, JSFunction method) { final JSArgumentList argumentList = exp.getArgumentList(); if (argumentList != null) { final JSExpression[] args = argumentList.getArguments(); for (final JSExpression arg : args) { if (RecursionUtil.expressionDefinitelyRecurses(arg, method)) { return true; } } } return false; }
private static boolean expressionDefinitelyRecurses(JSExpression exp, JSFunction method) { if (exp == null) { return false; } if (exp instanceof JSNewExpression) { return RecursionUtil.newExpressionDefinitelyRecurses( (JSNewExpression) exp, method); } if (exp instanceof JSCallExpression) { return RecursionUtil.callExpressionDefinitelyRecurses( (JSCallExpression) exp, method); } if (exp instanceof JSAssignmentExpression) { return RecursionUtil.assignmentExpressionDefinitelyRecurses( (JSAssignmentExpression) exp, method); } if (exp instanceof JSArrayLiteralExpression) { return RecursionUtil.arrayLiteralExpressionDefinitelyRecurses( (JSArrayLiteralExpression) exp, method); } if (exp instanceof JSIndexedPropertyAccessExpression) { return RecursionUtil.indexedPropertyAccessExpressionDefinitelyRecurses( (JSIndexedPropertyAccessExpression) exp, method); } if (exp instanceof JSPrefixExpression) { return RecursionUtil.prefixExpressionDefinitelyRecurses( (JSPrefixExpression) exp, method); } if (exp instanceof JSPostfixExpression) { return RecursionUtil.postfixExpressionDefinitelyRecurses( (JSPostfixExpression) exp, method); } if (exp instanceof JSBinaryExpression) { return RecursionUtil.binaryExpressionDefinitelyRecurses( (JSBinaryExpression) exp, method); } if (exp instanceof JSConditionalExpression) { return RecursionUtil.conditionalExpressionDefinitelyRecurses( (JSConditionalExpression) exp, method); } if (exp instanceof JSParenthesizedExpression) { return RecursionUtil.parenthesizedExpressionDefinitelyRecurses( (JSParenthesizedExpression) exp, method); } if (exp instanceof JSReferenceExpression) { return RecursionUtil.referenceExpressionDefinitelyRecurses( (JSReferenceExpression) exp, method); } if (exp instanceof JSLiteralExpression || exp instanceof JSThisExpression) { return false; } return false; }