protected static void replaceExpression(JSExpression expression, String newExpression) throws IncorrectOperationException { final ASTNode fromText = JSChangeUtil.createStatementFromText(expression.getProject(), newExpression + ';'); final PsiElement element = fromText != null ? fromText.getPsi() : null; final JSExpressionStatement expressionStatement = element instanceof JSExpressionStatement ? (JSExpressionStatement) element:null; if (expressionStatement == null) { return; } final JSExpression newExp = expressionStatement.getExpression(); final ASTNode newExpressionNode = newExp.getNode(); final ASTNode expressionNode = expression.getNode(); final PsiElement parent = expression.getParent(); final ASTNode parentNode = parent.getNode(); parentNode.replaceChild(expressionNode, newExpressionNode); // final CodeStyleManager styleManager = manager.getCodeStyleManager(); // styleManager.reformat(newExpressionNode.getPsi()); }
public static void replaceSimplifiableImplicitAssignment(JSIfStatement statement, boolean negated) throws IncorrectOperationException { final JSElement prevStatement = (JSElement) JSElementFactory.getNonWhiteSpaceSibling(statement, false); final JSExpression condition = statement.getCondition(); final String conditionText = (negated ? BoolUtils.getNegatedExpressionText(condition) : condition.getText()); final JSExpressionStatement assignmentStatement = (JSExpressionStatement) ConditionalUtils.stripBraces(statement.getThen()); final JSAssignmentExpression assignmentExpression = (JSAssignmentExpression) assignmentStatement.getExpression(); final IElementType operator = assignmentExpression.getOperationSign(); final String operand = BinaryOperatorUtils.getOperatorText(operator); final JSExpression lhs = assignmentExpression.getLOperand(); final String lhsText = lhs.getText(); JSElementFactory.replaceStatement(statement, lhsText + operand + conditionText + ';'); assert (prevStatement != null); JSElementFactory.removeElement(prevStatement); }
@Override public boolean satisfiedBy(@NotNull PsiElement element) { if (!ExpressionUtil.isIncrementDecrementExpression(element)) { return false; } if (ErrorUtil.containsError(element)) { return false; } final PsiElement parent = element.getParent(); if (parent instanceof JSExpressionStatement) { return false; } final JSStatement containingStatement = TreeUtil.getParentOfType(element, JSStatement.class); if (element instanceof JSPostfixExpression && (containingStatement instanceof JSReturnStatement || containingStatement instanceof JSThrowStatement)) { return false; } return (containingStatement != null); }
public static boolean containsTestsInFiles(JSFile file) { JSSourceElement[] statements = file.getStatements(); for(JSSourceElement statement : statements) { if(statement instanceof JSExpressionStatement) { JSExpression expression = ((JSExpressionStatement) statement).getExpression(); if(expression instanceof JSCallExpression) { JSExpression methodExpression = ((JSCallExpression) expression).getMethodExpression(); if(methodExpression instanceof JSReferenceExpression) { JSExpression qualifier = ((JSReferenceExpression) methodExpression).getQualifier(); if(qualifier != null) { continue; } String referencedName = ((JSReferenceExpression) methodExpression).getReferencedName(); if("describe".equals(referencedName)) { JSArgumentList argumentList = ((JSCallExpression) expression).getArgumentList(); if(argumentList != null && argumentList.getArguments().length == 2) { return true; } } } } } } return false; }
@Test public void testPropertyIsGenerated() { List<JSExpressionStatement> methods = new ArrayList<JSExpressionStatement>(); methods.add(new MockJSExpressionStatement(new MockJSAssignmentExpression("util.", "property", "'value'"))); String result = converter.buildUtilPatternString(null, new JSExpression[0], methods, "util"); assertTrue(result.contains("property: 'value'")); }
@Test public void indexPropertyIsConverted() { List<JSExpressionStatement> methods = new ArrayList<JSExpressionStatement>(); methods.add(new MockJSExpressionStatement(new MockJSAssignmentExpression(new MockJSDefinitionExpressionWithIndexedProperty("util", "'-test-'"), "value"))); String result = converter.buildUtilPatternString(null, new JSExpression[0], methods, "util"); assertTrue(result.contains("-test-': value")); }
public JavaScriptKeywordCompletionContributor() { extend(CompletionType.BASIC, StandardPatterns.psiElement().withParent(JSReferenceExpression.class), new CompletionProvider() { @RequiredReadAction @Override public void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) { PsiElement position = parameters.getPosition(); JSReferenceExpression parent = (JSReferenceExpression) position.getParent(); if(parent.getQualifier() != null) { return; } boolean parentIsStatement = parent.getParent() instanceof JSExpressionStatement; LookupElementBuilder functionKeyword = LookupElementBuilder.create("function"); functionKeyword = functionKeyword.bold(); if(parentIsStatement) { functionKeyword = functionKeyword.withInsertHandler(SpaceInsertHandler.INSTANCE); } else { functionKeyword = functionKeyword.withInsertHandler(ParenthesesInsertHandler.getInstance(false)); functionKeyword = functionKeyword.withPresentableText("function()"); } result.addElement(functionKeyword); result.addElement(LookupElementBuilder.create("var").withInsertHandler(SpaceInsertHandler.INSTANCE).bold()); } }); }
@Override public int getTextEndOffset(@NotNull PsiElement element) { if(element instanceof JSDefinitionExpression) { element = PsiTreeUtil.getParentOfType(element, JSExpressionStatement.class); } return element.getTextRange().getEndOffset(); }
protected CreateClassDialog(final Project project, String className, String packageName, boolean isInterface) { super(project, false); setTitle(JavaScriptBundle.message(isInterface ? "create.interface.dialog.title" : "create.class.dialog.title")); setModal(true); myPackageName.getDocument().addDocumentListener(new DocumentAdapter() { @Override protected void textChanged(final DocumentEvent e) { String text = getPackageName(); boolean enabled; if(text.length() == 0) { enabled = true; } else { ASTNode node = JSChangeUtil.createJSTreeFromText(project, text); PsiElement elt; enabled = node != null && (elt = node.getPsi()) instanceof JSExpressionStatement && (elt = ((JSExpressionStatement) elt).getExpression()) instanceof JSReferenceExpression && ((JSReferenceExpression) elt).getReferencedName() != null && elt.textMatches(text); } getOKAction().setEnabled(enabled); } }); myClassName.setText(className); myPackageName.setText(packageName); init(); }
@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); }
@Override public void visitJSExpressionStatement(JSExpressionStatement jsExpressionStatement) { super.visitJSExpressionStatement(jsExpressionStatement); final JSExpression expression = jsExpressionStatement.getExpression(); if (!(expression instanceof JSReferenceExpression)) { return; } @NonNls final String text = expression.getText(); if (!"debugger".equals(text)) { return; } registerError(expression); }
public static boolean isAssignment(JSStatement statement, String value) { if (statement == null || !(statement instanceof JSExpressionStatement)) { return false; } final JSExpression expression = ((JSExpressionStatement) statement).getExpression(); if (!(expression instanceof JSAssignmentExpression)) { return false; } final JSExpression rhs = ((JSAssignmentExpression) expression).getROperand(); return (rhs != null && rhs.getText().equals(value)); }
public static boolean isAssignment(JSStatement statement) { if (!(statement instanceof JSExpressionStatement)) { return false; } final JSExpressionStatement expressionStatement = (JSExpressionStatement) statement; return (expressionStatement.getExpression() instanceof JSAssignmentExpression); }
public static boolean isSimplifiableAssignment(JSIfStatement ifStatement, boolean negated) { final JSStatement thenBranch = ConditionalUtils.stripBraces(ifStatement.getThen()); final JSStatement elseBranch = ConditionalUtils.stripBraces(ifStatement.getElse()); final boolean isAssignment; if (negated) { isAssignment = (ConditionalUtils.isAssignment(thenBranch, BoolUtils.FALSE) && ConditionalUtils.isAssignment(elseBranch, BoolUtils.TRUE)); } else { isAssignment = (ConditionalUtils.isAssignment(thenBranch, BoolUtils.TRUE) && ConditionalUtils.isAssignment(elseBranch, BoolUtils.FALSE)); } if (isAssignment) { final JSAssignmentExpression thenExpression = (JSAssignmentExpression) ((JSExpressionStatement) thenBranch).getExpression(); final JSAssignmentExpression elseExpression = (JSAssignmentExpression) ((JSExpressionStatement) elseBranch).getExpression(); final IElementType thenSign = thenExpression.getOperationSign(); final IElementType elseSign = elseExpression.getOperationSign(); if (!thenSign.equals(elseSign)) { return false; } final JSExpression thenLhs = thenExpression.getLOperand(); final JSExpression elseLhs = elseExpression.getLOperand(); return EquivalenceChecker.expressionsAreEquivalent(thenLhs, elseLhs); } else { return false; } }
public static void replaceSimplifiableAssignment(JSIfStatement statement, boolean negated) throws IncorrectOperationException { final JSExpression condition = statement.getCondition(); final String conditionText = (negated ? BoolUtils.getNegatedExpressionText(condition) : condition.getText()); final JSExpressionStatement assignmentStatement = (JSExpressionStatement) ConditionalUtils.stripBraces(statement.getThen()); final JSAssignmentExpression assignmentExpression = (JSAssignmentExpression) assignmentStatement.getExpression(); final IElementType operator = assignmentExpression.getOperationSign(); final String operand = BinaryOperatorUtils.getOperatorText(operator); final JSExpression lhs = assignmentExpression.getLOperand(); final String lhsText = lhs.getText(); JSElementFactory.replaceStatement(statement, lhsText + operand + conditionText + ';'); }
private static void getAssignmentReplacement(StringBuilder buffer, JSIfStatement ifStatement) { final JSExpression condition = ifStatement.getCondition(); final JSExpressionStatement thenBranch = (JSExpressionStatement) ConditionalUtils.stripBraces(ifStatement.getThen()); final JSAssignmentExpression thenAssign = (JSAssignmentExpression) thenBranch.getExpression(); final JSExpression thenRhs = thenAssign.getROperand(); final String operator = BinaryOperatorUtils.getOperatorText(thenAssign.getOperationSign()); final JSStatement elseBranch = ifStatement.getElse(); assert (thenRhs != null); buffer.append(thenAssign.getLOperand().getText()) .append(operator) .append(ParenthesesUtils.getParenthesized(condition, ParenthesesUtils.CONDITIONAL_PRECENDENCE)) .append(TERNARY_QUESTION) .append(ParenthesesUtils.getParenthesized(thenRhs, ParenthesesUtils.CONDITIONAL_PRECENDENCE)) .append(TERNARY_SEMICOLON); if (elseBranch instanceof JSIfStatement) { getAssignmentReplacement(buffer, (JSIfStatement) elseBranch); return; } final JSExpressionStatement strippedElseBranch = (JSExpressionStatement) ConditionalUtils.stripBraces(elseBranch); final JSAssignmentExpression elseAssign = (JSAssignmentExpression) strippedElseBranch.getExpression(); final JSExpression elseRhs = elseAssign.getROperand(); assert (elseRhs != null); buffer.append(ParenthesesUtils.getParenthesized(elseRhs, ParenthesesUtils.CONDITIONAL_PRECENDENCE)); }
private static JSFunctionExpression getFunctionExpr(final ASTNode statementNode) { return (JSFunctionExpression) ((JSAssignmentExpression) ((JSExpressionStatement) statementNode.getPsi()).getExpression()).getROperand(); }
public static boolean isSimplifiableImplicitAssignment(JSIfStatement ifStatement, boolean negated) { if (ifStatement.getElse() != null) { return false; } final JSStatement thenBranch = ConditionalUtils.stripBraces(ifStatement.getThen()); final PsiElement nextStatement = JSElementFactory.getNonWhiteSpaceSibling(ifStatement, false); if (!(nextStatement instanceof JSStatement)) { return false; } final JSStatement elseBranch = ConditionalUtils.stripBraces((JSStatement) nextStatement); final boolean isAssignment; if (negated) { isAssignment = (ConditionalUtils.isAssignment(thenBranch, BoolUtils.FALSE) && ConditionalUtils.isAssignment(elseBranch, BoolUtils.TRUE)); } else { isAssignment = (ConditionalUtils.isAssignment(thenBranch, BoolUtils.TRUE) && ConditionalUtils.isAssignment(elseBranch, BoolUtils.FALSE)); } if (isAssignment) { final JSAssignmentExpression thenExpression = (JSAssignmentExpression) ((JSExpressionStatement) thenBranch).getExpression(); final JSAssignmentExpression elseExpression = (JSAssignmentExpression) ((JSExpressionStatement) elseBranch).getExpression(); final IElementType thenSign = thenExpression.getOperationSign(); final IElementType elseSign = elseExpression.getOperationSign(); if (!thenSign.equals(elseSign)) { return false; } final JSExpression thenLhs = thenExpression.getLOperand(); final JSExpression elseLhs = elseExpression.getLOperand(); return EquivalenceChecker.expressionsAreEquivalent(thenLhs, elseLhs); } else { return false; } }
public static boolean statementMayReturnBeforeRecursing( JSStatement statement, JSFunction method) { if (statement == null) { return true; } if (statement instanceof JSBreakStatement || statement instanceof JSContinueStatement || statement instanceof JSThrowStatement || statement instanceof JSExpressionStatement || statement instanceof JSEmptyStatement || statement instanceof JSVarStatement) { return false; } else if (statement instanceof JSReturnStatement) { final JSReturnStatement returnStatement = (JSReturnStatement) statement; final JSExpression returnValue = returnStatement.getExpression(); return (returnValue == null || !RecursionUtil.expressionDefinitelyRecurses(returnValue, method)); } else if (statement instanceof JSForStatement) { return RecursionUtil.forStatementMayReturnBeforeRecursing( (JSForStatement) statement, method); } else if (statement instanceof JSForInStatement) { return RecursionUtil.forInStatementMayReturnBeforeRecursing( (JSForInStatement) statement, method); } else if (statement instanceof JSWhileStatement) { return RecursionUtil.whileStatementMayReturnBeforeRecursing( (JSWhileStatement) statement, method); } else if (statement instanceof JSDoWhileStatement) { return RecursionUtil.doWhileStatementMayReturnBeforeRecursing( (JSDoWhileStatement) statement, method); } else if (statement instanceof JSBlockStatement) { final JSBlockStatement blockStatement = (JSBlockStatement)statement; return RecursionUtil.blockStatementMayReturnBeforeRecursing(blockStatement, method, false); } else if (statement instanceof JSLabeledStatement) { return RecursionUtil.labeledStatementMayReturnBeforeRecursing( (JSLabeledStatement) statement, method); } else if (statement instanceof JSIfStatement) { return RecursionUtil.ifStatementMayReturnBeforeRecursing( (JSIfStatement) statement, method); } else if (statement instanceof JSTryStatement) { return RecursionUtil.tryStatementMayReturnBeforeRecursing( (JSTryStatement) statement, method); } else if (statement instanceof JSSwitchStatement) { return RecursionUtil.switchStatementMayReturnBeforeRecursing( (JSSwitchStatement) statement, method); } else { // unknown statement type return true; } }
private static boolean statementDefinitelyRecurses(JSStatement statement, JSFunction method) { if (statement == null) { return false; } if (statement instanceof JSBreakStatement || statement instanceof JSContinueStatement || statement instanceof JSThrowStatement || statement instanceof JSEmptyStatement) { return false; } else if (statement instanceof JSExpressionStatement) { final JSExpressionStatement expressionStatement = (JSExpressionStatement)statement; final JSExpression expression = expressionStatement.getExpression(); return RecursionUtil.expressionDefinitelyRecurses(expression, method); } else if (statement instanceof JSVarStatement) { final JSVarStatement varStatement = (JSVarStatement) statement; for(final JSVariable variable : varStatement.getVariables()) { final JSExpression initializer = variable.getInitializer(); if (RecursionUtil.expressionDefinitelyRecurses(initializer, method)) { return true; } } return false; } else if (statement instanceof JSReturnStatement) { final JSReturnStatement returnStatement = (JSReturnStatement) statement; final JSExpression returnValue = returnStatement.getExpression(); if (returnValue != null) { if (RecursionUtil.expressionDefinitelyRecurses(returnValue, method)) { return true; } } return false; } else if (statement instanceof JSForStatement) { return RecursionUtil.forStatementDefinitelyRecurses((JSForStatement) statement, method); } else if (statement instanceof JSForInStatement) { return RecursionUtil.forInStatementDefinitelyRecurses( (JSForInStatement) statement, method); } else if (statement instanceof JSWhileStatement) { return RecursionUtil.whileStatementDefinitelyRecurses( (JSWhileStatement) statement, method); } else if (statement instanceof JSDoWhileStatement) { return RecursionUtil.doWhileStatementDefinitelyRecurses( (JSDoWhileStatement) statement, method); } else if (statement instanceof JSBlockStatement) { return RecursionUtil.blockStatementDefinitelyRecurses( (JSBlockStatement) statement, method); } else if (statement instanceof JSLabeledStatement) { return RecursionUtil.labeledStatementDefinitelyRecurses( (JSLabeledStatement) statement, method); } else if (statement instanceof JSIfStatement) { return RecursionUtil.ifStatementDefinitelyRecurses( (JSIfStatement) statement, method); } else if (statement instanceof JSTryStatement) { return RecursionUtil.tryStatementDefinitelyRecurses( (JSTryStatement) statement, method); } else if (statement instanceof JSSwitchStatement) { return RecursionUtil.switchStatementDefinitelyRecurses( (JSSwitchStatement) statement, method); } else { // unknown statement type return false; } }