@Nullable @Override public PsiElement getElementByReference(@NotNull PsiReference ref, int flags) { if (ref instanceof JSTextReference) { final PsiElement element = ref.getElement(); final JSCallExpression call = PsiTreeUtil.getParentOfType(element, JSCallExpression.class); final JSExpression expression = call != null ? call.getMethodExpression() : null; if (expression instanceof JSReferenceExpression) { JSReferenceExpression callee = (JSReferenceExpression)expression; JSExpression qualifier = callee.getQualifier(); if (qualifier != null && "component".equals(callee.getReferencedName()) && EmberIndexUtil.hasEmberJS(element.getProject())) { return element; } } } return null; }
@Override public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull final InspectionManager manager, boolean isOnTheFly) { if(!isEnabled(file.getProject())) { return new ProblemDescriptor[0]; } DefineResolver resolver = new DefineResolver(); final List<ProblemDescriptor> descriptors = new ArrayList<ProblemDescriptor>(); Set<JSCallExpression> expressions = resolver.getAllImportBlocks(file); for(JSCallExpression expression : expressions) { addProblemsForBlock(expression, descriptors, file, manager); } return descriptors.toArray(new ProblemDescriptor[0]); }
public static boolean elementIsAttachPoint(PsiElement element) { /* It's hard to detect when an element is an attach point, because of the use of this inside other functions this.attachpoint that.attachpoint ideally we would parse the template file in the beginning and cache all of the attach points, maybe that's a todo item... */ if(element == null || element.getParent() == null || !(element.getParent() instanceof JSReferenceExpression)) { return false; } // we can exclude JSCallExpressions at least because you will never reference an attach point like // this.attachpoint(...) if(element.getParent().getParent() instanceof JSCallExpression) { return false; } return true; }
/** * takes an existing AMD import and updates it. Used when a module changes locations * * @param index the index of the import * @param currentModule the module that is being updated * @param quote the quote symbol to use for imports * @param path the new module's path * @param newModule the new module */ public void reimportModule(int index, PsiFile currentModule, char quote, String path, PsiFile newModule, String pluginPostfix, boolean updateReferences, PsiFile pluginResourceFile, JSCallExpression callExpression) { DefineStatement defineStatement = new DefineResolver().getDefineStatementItemsFromArguments(callExpression.getArguments(), callExpression); String newModuleName = newModule.getName().substring(0, newModule.getName().indexOf('.')); LinkedHashMap<String, PsiFile> results = new ImportResolver().getChoicesFromFiles(new PsiFile[] { newModule }, libraries, newModuleName, currentModule, false, true); // check if the original used a relative syntax or absolute syntax, and prefer that String[] possibleImports = results.keySet().toArray(new String[0]); String chosenImport = chooseImportToReplaceAnImport(path, possibleImports); pluginPostfix = getUpdatedPluginResource(pluginResourceFile, pluginPostfix, currentModule); PsiElement defineLiteral = defineStatement.getArguments().getExpressions()[index]; PsiElement newImport = JSUtil.createExpression(defineLiteral.getParent(), quote + chosenImport + pluginPostfix + quote); MatchResult match = new MatchResult(currentModule, index, path, quote, pluginPostfix, pluginResourceFile, callExpression); importUpdater.updateModuleReference(currentModule, match, defineStatement, newImport, updateReferences); }
@Test public void testTheBasicHappyPath() { Map<String, String> propertyMap = new HashMap<String, String>(); propertyMap.put("property 1", "value"); JSExpression[] arguments = new JSExpression[] { new MockJSArrayLiteralExpression(new String[] { "mixin1", "mixin2"}), new MockJSObjectLiteralExpression(propertyMap) }; JSCallExpression callExpression = new MockJSCallExpression(arguments); Object[] statements = new Object[] {callExpression, null}; DeclareStatementItems result = resolver.getDeclareStatementFromParsedStatement(statements); assertEquals(2, result.getExpressionsToMixin().length); assertEquals(1, result.getMethodsToConvert().length); }
@Test public void testWhenFirstArgumentIsNull() { Map<String, String> propertyMap = new HashMap<String, String>(); propertyMap.put("property 1", "value"); JSExpression[] arguments = new JSExpression[] { BasicPsiElements.Null(), new MockJSObjectLiteralExpression(propertyMap) }; JSCallExpression callExpression = new MockJSCallExpression(arguments); Object[] statements = new Object[] {callExpression, null}; DeclareStatementItems result = resolver.getDeclareStatementFromParsedStatement(statements); assertEquals(0, result.getExpressionsToMixin().length); assertEquals(1, result.getMethodsToConvert().length); }
@Test public void classNameIsRetrieved() { Map<String, String> propertyMap = new HashMap<String, String>(); propertyMap.put("property 1", "value"); JSExpression[] arguments = new JSExpression[] { new MockJSLiteralExpression("test class"), new MockJSArrayLiteralExpression(new String[] { "define 1", "define 2"}), new MockJSObjectLiteralExpression(propertyMap) }; JSCallExpression callExpression = new MockJSCallExpression(arguments); Object[] statements = new Object[] {callExpression, null}; DeclareStatementItems result = resolver.getDeclareStatementFromParsedStatement(statements); assertEquals("test class", result.getClassName().getText()); }
@Test public void testWhenFirstArgumentIsAClassName() { Map<String, String> propertyMap = new HashMap<String, String>(); propertyMap.put("property 1", "value"); JSExpression[] arguments = new JSExpression[] { new MockJSLiteralExpression("test class"), new MockJSArrayLiteralExpression(new String[] { "define 1", "define 2"}), new MockJSObjectLiteralExpression(propertyMap) }; JSCallExpression callExpression = new MockJSCallExpression(arguments); Object[] statements = new Object[] {callExpression, null}; DeclareStatementItems result = resolver.getDeclareStatementFromParsedStatement(statements); assertEquals(2, result.getExpressionsToMixin().length); assertEquals(1, result.getMethodsToConvert().length); }
@Test public void testWhenMixinArrayIsNull() { Map<String, String> propertyMap = new HashMap<String, String>(); propertyMap.put("property 1", "value"); JSExpression[] arguments = new JSExpression[] { new MockJSLiteralExpression("test class"), new MockJSLiteralExpression("null"), new MockJSObjectLiteralExpression(propertyMap) }; JSCallExpression callExpression = new MockJSCallExpression(arguments); Object[] statements = new Object[] {callExpression, null}; DeclareStatementItems result = resolver.getDeclareStatementFromParsedStatement(statements); assertNotNull(result); }
public boolean isRequireCall(PsiElement element) { PsiElement prevEl = element.getParent(); if (prevEl != null) { prevEl = prevEl.getParent(); } if (prevEl instanceof JSCallExpression) { if (prevEl.getChildren().length > 1) { String requireFunctionName = Settings.REQUIREJS_REQUIRE_FUNCTION_NAME; if (prevEl.getChildren()[0].getText().toLowerCase().equals(requireFunctionName)) { return true; } } } return false; }
private static JSExpression findExpressionInRange(PsiFile file, int startOffset, int endOffset) { PsiElement element1 = file.findElementAt(startOffset); PsiElement element2 = file.findElementAt(endOffset - 1); if(element1 instanceof PsiWhiteSpace) { startOffset = element1.getTextRange().getEndOffset(); } if(element2 instanceof PsiWhiteSpace) { endOffset = element2.getTextRange().getStartOffset(); } JSExpression expression = PsiTreeUtil.findElementOfClassAtRange(file, startOffset, endOffset, JSExpression.class); if(expression == null || expression.getTextRange().getEndOffset() != endOffset) { return null; } if(expression instanceof JSReferenceExpression && expression.getParent() instanceof JSCallExpression) { return null; } return expression; }
@Override public void visitJSCallExpression(JSCallExpression jsCallExpression) { super.visitJSCallExpression(jsCallExpression); final JSExpression methodExpression; try { methodExpression = jsCallExpression.getMethodExpression(); } catch (Exception e) { return; //catching an intelliJ CCE } if (!(methodExpression instanceof JSReferenceExpression)) { return; } final JSReferenceExpression referenceExpression = (JSReferenceExpression) methodExpression; final JSExpression qualifier = referenceExpression.getQualifier(); @NonNls final String methodName = referenceExpression.getReferencedName(); if (!"eval".equals(methodName) && !"setTimeout".equals(methodName) && !"setInterval".equals(methodName)) { return; } registerError(methodExpression); }
@Override public void visitJSCallExpression( @NotNull JSCallExpression expression) { super.visitJSCallExpression(expression); final JSExpression reference = expression.getMethodExpression(); if(!(reference instanceof JSReferenceExpression)) { return; } final JSExpression qualifier = ((JSReferenceExpression)reference).getQualifier(); if (qualifier == null) { return; } if (!isCallExpression(qualifier)) { return; } registerFunctionCallError(expression); }
private static boolean callExpressionDefinitelyRecurses( JSCallExpression exp, JSFunction method) { final JSFunction calledMethod = ControlFlowUtils.resolveMethod(exp); if (calledMethod != null && calledMethod.equals(method)) { return true; } final JSExpression methodExpression = exp.getMethodExpression(); if (methodExpression == null) { return false; } else if (RecursionUtil.expressionDefinitelyRecurses(methodExpression, method)) { return true; } for (final JSExpression arg : exp.getArgumentList().getArguments()) { if (RecursionUtil.expressionDefinitelyRecurses(arg, method)) { return true; } } return false; }
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; }
public JSCallExpression getCallExpression() { if(callExpression == null) { throw new RuntimeException("callExpression was not set but is being accessed"); } return callExpression; }
public MatchResult(PsiFile module, int index, String path, char quote, String pluginResourceId, PsiFile pluginResourceFile, JSCallExpression callExpression) { this.index = index; this.path = path; this.quote = quote; this.module = module; this.pluginResourceId = pluginResourceId; this.pluginResourceFile = pluginResourceFile; this.callExpression = callExpression; }
@Override protected void addParameters(Template template, JSReferenceExpression referenceExpression, PsiFile file, Set<JavaScriptFeature> features) { JSCallExpression methodInvokation = (JSCallExpression) referenceExpression.getParent(); final JSArgumentList list = methodInvokation.getArgumentList(); final JSExpression[] expressions = list.getArguments(); int paramCount = expressions.length; for(int i = 0; i < paramCount; ++i) { if(i != 0) { template.addTextSegment(", "); } String var = null; final JSExpression passedParameterValue = expressions[i]; if(passedParameterValue instanceof JSReferenceExpression) { var = ((JSReferenceExpression) passedParameterValue).getReferencedName(); } if(var == null || var.length() == 0) { var = "param" + (i != 0 ? Integer.toString(i + 1) : ""); } final String var1 = var; Expression expression = new MyExpression(var1); template.addVariable(var, expression, expression, true); if(features.contains(JavaScriptFeature.CLASS)) { template.addTextSegment(":"); BaseCreateFix.guessExprTypeAndAddSuchVariable(passedParameterValue, template, var1, file, features); } } }
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; }
@Nullable public static JSArgumentList findArgumentList(final PsiFile file, final int offset) { JSArgumentList argList = ParameterInfoUtils.findParentOfType(file, offset, JSArgumentList.class); if(argList == null) { final JSCallExpression callExpression = ParameterInfoUtils.findParentOfType(file, offset, JSCallExpression.class); if(callExpression != null) { argList = callExpression.getArgumentList(); } } return argList; }
protected void registerFunctionCallError(JSCallExpression expression){ final JSExpression methodExpression = expression.getMethodExpression(); PsiElement errorLocation = null; if (methodExpression instanceof JSReferenceExpression) { errorLocation = ((JSReferenceExpression)methodExpression).getReferenceNameElement(); } else if (methodExpression instanceof JSFunction) { final PsiElement node = ((JSFunction)methodExpression).getNameIdentifier(); if (node != null) errorLocation = node; else errorLocation = methodExpression; } registerError(errorLocation); }
private static boolean isCallExpression(JSExpression expression) { if (expression instanceof JSCallExpression ) { return true; } if (expression instanceof JSParenthesizedExpression) { final JSParenthesizedExpression parenthesizedExpression = (JSParenthesizedExpression) expression; final JSExpression containedExpression = parenthesizedExpression.getInnerExpression(); return isCallExpression(containedExpression); } return false; }
@Override public void visitJSCallExpression(JSCallExpression jsCallExpression) { super.visitJSCallExpression(jsCallExpression); final JSExpression methodExpression; try { methodExpression = jsCallExpression.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 qualifierText = qualifier.getText(); if(!"document".equals(qualifierText)) { return; } @NonNls final String methodName = referenceExpression.getReferencedName(); if(!"write".equals(methodName) && !"writeln".equals(methodName)) { return; } registerError(methodExpression); }
@Override public boolean satisfiedBy(@NotNull PsiElement element) { if (!(element instanceof JSExpression)) { return false; } if (ErrorUtil.containsError(element)) { return false; } final JSExpression expression = (JSExpression) element; if (element instanceof JSLiteralExpression || ( element instanceof JSReferenceExpression && ((JSReferenceExpression)element).getQualifier() != null ) || expression instanceof JSCallExpression ) { return false; } if (!ExpressionUtil.isConstantExpression(expression)) { return false; } if (ExpressionUtil.computeConstantExpression(expression) == null) { return false; } final PsiElement parent = element.getParent(); if (!(parent instanceof JSExpression)) { return true; } return (!ExpressionUtil.isConstantExpression((JSExpression) parent)); }
@Override public void actionPerformed(AnActionEvent e) { final PsiFile psiFile = PsiFileUtil.getPsiFileInCurrentEditor(e.getProject()); final AMDImportOrganizer organizer = new AMDImportOrganizer(); CommandProcessor.getInstance().executeCommand(psiFile.getProject(), new Runnable() { @Override public void run() { ApplicationManager.getApplication().runWriteAction(new Runnable() { @Override public void run() { int totalSize = 0; DefineResolver resolver = new DefineResolver(); Set<JSCallExpression> expressions = resolver.getAllImportBlocks(psiFile); for(JSCallExpression expression : expressions) { List<PsiElement> blockDefines = new ArrayList<PsiElement>(); List<PsiElement> blockParameters = new ArrayList<PsiElement>(); try { resolver.addDefinesAndParametersOfImportBlock(expression, blockDefines, blockParameters); } catch (InvalidDefineException e1) {} if(blockDefines.size() == 0 || blockParameters.size() == 0) { continue; } SortingResult result = organizer.sortDefinesAndParameters(blockDefines, blockParameters); totalSize += blockDefines.size(); organizer.reorder(blockDefines.toArray(new PsiElement[]{}), result.getDefines(), true, result); organizer.reorder(blockParameters.toArray(new PsiElement[]{}), result.getParameters(), false, result); } if(totalSize == 0) { Notifications.Bus.notify(new Notification("needsmoredojo", "Organize AMD Imports", "There were no AMD imports", NotificationType.WARNING)); return; } else { Notifications.Bus.notify(new Notification("needsmoredojo", "Organize AMD Imports", "Completed", NotificationType.INFORMATION)); } } }); } }, "Organize AMD Imports", "Organize AMD Imports"); }
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 UnusedImportBlockEntry(boolean isDefine, JSCallExpression block, List<PsiElement> defines, List<PsiElement> parameters) { this.block = block; this.defines = defines; this.parameters = parameters; this.isDefine = isDefine; }
public JSCallExpression getBlock() { return block; }
public JSCallExpression getCallExpression() { return callExpression; }
/** * given a module, returns a list of modules that it references in the define block * * @param module * @return */ public List<MatchResult> findFilesThatModuleReferences(PsiFile module) { DefineResolver resolver = new DefineResolver(); Set<JSCallExpression> callExpressions = resolver.getAllImportBlocks(module); List<MatchResult> matches = new ArrayList<MatchResult>(); for(JSCallExpression callExpression : callExpressions) { if(callExpression == null) { continue; } DefineStatement statement = resolver.getDefineStatementItemsFromArguments(callExpression.getArguments(), callExpression); if(statement == null) { // define statement wasn't valid return matches; } for(int i=0;i<statement.getArguments().getExpressions().length;i++) { JSExpression expression = statement.getArguments().getExpressions()[i]; char quote = '"'; if(expression.getText().contains("'")) { quote = '\''; } // figure out which module it is String importModule = expression.getText().replaceAll("'", "").replaceAll("\"", ""); // get the module name String moduleName = NameResolver.getModuleName(importModule); String pluginResourceId = NameResolver.getAMDPluginResourceIfPossible(importModule, true); String modulePath = NameResolver.getModulePath(importModule); PsiFile pluginResourceFile = null; if(pluginResourceId.startsWith("!") && pluginResourceId.length() > 2 && pluginResourceId.charAt(1) == '.') { PsiReference[] fileReferences = expression.getReferences(); if(fileReferences.length > 0) { PsiReference potentialReference = fileReferences[fileReferences.length-1]; pluginResourceFile = (PsiFile) potentialReference.resolve(); } } else if (pluginResourceId.startsWith("!") && pluginResourceId.length() > 2) { // this is an absolute reference } // get the list of possible strings/PsiFiles that would match it PsiFile[] files = new ImportResolver().getPossibleDojoImportFiles(module.getProject(), moduleName, true, false); // get the files that are being imported // TODO performance optimization LinkedHashMap<String, PsiFile> results = new ImportResolver().getChoicesFromFiles(files, libraries, moduleName, module.getContainingFile(), false, true); if(results.containsKey(modulePath + moduleName)) { MatchResult match = new MatchResult(results.get(modulePath + moduleName), i, modulePath + moduleName, quote, pluginResourceId, pluginResourceFile, callExpression); matches.add(match); } } } return matches; }
@Override public void delete() throws IncorrectOperationException { PsiElement element = getParent(); element.replace(((JSCallExpression) element).getMethodExpression()); }
@Nullable private static JSArgumentList fillSignaturesForArgumentList(final CreateParameterInfoContext context, final @NotNull JSArgumentList argList) { final PsiElement psiElement = argList.getParent(); if(!(psiElement instanceof JSCallExpression)) { return null; } final JSCallExpression parent = (JSCallExpression) psiElement; final JSExpression methodExpression = parent.getMethodExpression(); if(methodExpression instanceof JSReferenceExpression) { final ResolveResult[] resolveResults = ((JSReferenceExpression) methodExpression).multiResolve(true); if(resolveResults.length > 0) { List<JSFunction> items = new ArrayList<JSFunction>(resolveResults.length); Set<String> availableSignatures = new HashSet<String>(); for(ResolveResult r : resolveResults) { PsiElement element = r.getElement(); if(element instanceof JSProperty) { element = ((JSProperty) element).getValue(); } doAddSignature(items, availableSignatures, element); } context.setItemsToShow(ArrayUtil.toObjectArray(items)); return argList; } } else if(methodExpression instanceof JSSuperExpression) { final PsiElement clazz = methodExpression.getReference().resolve(); if(clazz instanceof JSFunction) { context.setItemsToShow(new Object[]{clazz}); return argList; } } return null; }
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; }