private PsiElement moveImportToEnd(JSArrayLiteralExpression imports, JSParameterList parameters, String module, String parameter, PsiElement lastDefine, PsiElement lastParameter) { // TODO move to AMDPsiUtil if we need to reuse this in the future PsiElement lastChild = imports.getChildren()[imports.getChildren().length-1]; if(lastDefine != null) { lastChild = lastDefine; } PsiElement element = imports.addAfter(JSChangeUtil.createExpressionFromText(imports.getProject(), String.format("%s", module)).getPsi(), lastChild); imports.getNode().addLeaf(JSTokenTypes.COMMA, ",", element.getNode()); imports.getNode().addLeaf(JSTokenTypes.WHITE_SPACE, "\n", element.getNode()); PsiElement lastParameterChild = parameters.getChildren()[parameters.getChildren().length-1]; if(lastParameter != null) { lastParameterChild = lastParameter; } PsiElement parameterElement = parameters.addAfter(JSChangeUtil.createExpressionFromText(imports.getProject(), String.format("%s", parameter)).getPsi(), lastParameterChild); parameters.getNode().addLeaf(JSTokenTypes.COMMA, ",", parameterElement.getNode()); return element; }
public boolean isDefineFirstCollection(PsiElement element) { PsiElement jsArrayLiteral = element.getParent(); if (null != jsArrayLiteral && jsArrayLiteral instanceof JSArrayLiteralExpression) { PsiElement jsArgumentList = jsArrayLiteral.getParent(); if (null != jsArgumentList && jsArgumentList instanceof JSArgumentList) { PsiElement jsReferenceExpression = jsArgumentList.getPrevSibling(); if (null != jsReferenceExpression && jsReferenceExpression instanceof JSReferenceExpression) { if (jsReferenceExpression.getText().equals(Settings.REQUIREJS_DEFINE_FUNCTION_NAME)) { return true; } } } } return false; }
@RequiredReadAction @Override @SuppressWarnings("unchecked") public T parseValue(@NotNull Class type, @NotNull Type genericType, @NotNull PsiElement value) throws JomBadValueExpressionException { if(value instanceof JSArrayLiteralExpression) { JSExpression[] expressions = ((JSArrayLiteralExpression) value).getExpressions(); T collection = ReflectionUtil.newInstance(myNewInstanceClass); Pair<Class, Type> valueType = findValueTypeInsideGeneric(genericType, 0); // E for(JSExpression expression : expressions) { try { Object o = JomValueConverter.convertToObject(valueType.getFirst(), valueType.getSecond(), expression); collection.add(o); } catch(JomBadValueExpressionException e) { // we dont interest in bad value } } return collection; } throw new JomBadValueExpressionException(); }
private static boolean arrayLiteralExpressionDefinitelyRecurses( JSArrayLiteralExpression expression, JSFunction method) { for (final JSExpression initializer : expression.getExpressions()) { if (RecursionUtil.expressionDefinitelyRecurses(initializer, method)) { return true; } } return false; }
public Set<String> getDojoModulesInHtmlFile(PsiFile file) { final Set<String> modules = new HashSet<String>(); file.acceptChildren(new JSRecursiveElementVisitor() { @Override public void visitJSCallExpression(JSCallExpression node) { if(!node.getText().startsWith("require")) { super.visitJSCallExpression(node); return; } if(node.getArguments().length > 0 && node.getArguments()[0] instanceof JSArrayLiteralExpression) { JSArrayLiteralExpression arguments = (JSArrayLiteralExpression) node.getArguments()[0]; for(JSExpression literal : arguments.getExpressions()) { String literalText = literal.getText().replaceAll("'", "").replaceAll("\"", ""); if(!isDojoModule(literalText)) { modules.add(literalText); } } } super.visitJSCallExpression(node); } }); file.acceptChildren(new XmlRecursiveElementVisitor() { @Override public void visitXmlTag(XmlTag tag) { super.visitXmlTag(tag); } @Override public void visitXmlAttribute(XmlAttribute attribute) { if(attribute.getName().equals("data-dojo-type")) { if(!isDojoModule(attribute.getValue())) { modules.add(attribute.getValue()); } } super.visitXmlAttribute(attribute); } }); return modules; }
public DefineStatement(JSArrayLiteralExpression arguments, JSFunctionExpression function, String className, JSCallExpression originalParent) { this.arguments = arguments; this.function = function; this.className = className; this.callExpression = originalParent; }
public JSArrayLiteralExpression getArguments() { return arguments; }
@RequiredReadAction @SuppressWarnings("unchecked") public static Object convertToObject(@NotNull Class type, @NotNull Type genericType, @Nullable JSExpression value) throws JomBadValueExpressionException { if(value == null) { throw new JomBadValueExpressionException("No value for property"); } JomValueConverter.Converter converter = JomValueConverter.ourDefaultConverters.get(type); if(converter != null) { return converter.parseValue(type, genericType, value); } if(type.isArray()) { if(value instanceof JSArrayLiteralExpression) { Class componentType = type.getComponentType(); JSExpression[] expressions = ((JSArrayLiteralExpression) value).getExpressions(); List list = new ArrayList(expressions.length); for(JSExpression expression : expressions) { try { Object o = JomValueConverter.convertToObject(componentType, componentType, expression); list.add(o); } catch(JomBadValueExpressionException e) { // we dont interest in bad value } } return list.toArray((Object[]) Array.newInstance(componentType, list.size())); } } if(JomElement.class.isAssignableFrom(type)) { return JomProxyInvocationHandler.createProxy(type, value instanceof JSObjectLiteralExpression ? (JSObjectLiteralExpression) value : null); } throw new UnsupportedOperationException("Unsupported type: " + genericType); }
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; }