com.intellij.lang.javascript.psi.JSExpression Java Examples

The following examples show how to use com.intellij.lang.javascript.psi.JSExpression. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BlazeTypescriptGotoDeclarationHandler.java    From intellij with Apache License 2.0 6 votes vote down vote up
private static ImmutableList<JSLiteralExpression> getModuleDeclarations(
    ImmutableList<JSFile> jsFiles) {
  return findChildrenOfType(jsFiles, JSCallExpression.class).stream()
      .filter(
          call -> {
            JSExpression method = call.getMethodExpression();
            return method != null
                && (Objects.equals(method.getText(), "goog.provide")
                    || Objects.equals(method.getText(), "goog.module"));
          })
      .map(JSCallExpression::getArguments)
      .filter(a -> a.length == 1)
      .map(a -> a[0])
      .filter(JSLiteralExpression.class::isInstance)
      .map(JSLiteralExpression.class::cast)
      .filter(JSLiteralExpression::isQuotedLiteral)
      .collect(toImmutableList());
}
 
Example #2
Source File: GraphQLLanguageInjectionUtil.java    From js-graphql-intellij-plugin with MIT License 6 votes vote down vote up
public static String getEnvironmentFromTemplateTag(String tagText, PsiElement host) {
    if (RELAY_QL_TEMPLATE_TAG.equals(tagText)) {
        return RELAY_ENVIRONMENT;
    }
    if (GRAPHQL_TEMPLATE_TAG.equals(tagText) || GRAPHQL_EXPERIMENTAL_TEMPLATE_TAG.equals(tagText)) {
        if(host instanceof JSStringTemplateExpression) {
            final JSExpression[] arguments = ((JSStringTemplateExpression) host).getArguments();
            if(arguments.length > 0) {
                // one or more placeholders inside the tagged template text, so consider it templated graphql
                return GRAPHQL_TEMPLATE_ENVIRONMENT;
            }
        }
        return GRAPHQL_ENVIRONMENT;
    }
    if (GQL_TEMPLATE_TAG.equals(tagText)) {
        return PropertiesComponent.getInstance(host.getProject()).getValue(PROJECT_GQL_ENV, DEFAULT_GQL_ENVIRONMENT);
    }
    
    if (APOLLO_GQL_TEMPLATE_TAG.equals(tagText)) {
        return APOLLO_ENVIRONMENT;
    }
    // fallback
    return GRAPHQL_ENVIRONMENT;
}
 
Example #3
Source File: TestDeclareResolver.java    From needsmoredojo with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #4
Source File: TestDeclareResolver.java    From needsmoredojo with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #5
Source File: TestDeclareResolver.java    From needsmoredojo with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example #6
Source File: TestDeclareResolver.java    From needsmoredojo with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #7
Source File: TestDeclareResolver.java    From needsmoredojo with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #8
Source File: TestUtilToClassConverter.java    From needsmoredojo with Apache License 2.0 5 votes vote down vote up
@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"));
}
 
Example #9
Source File: JavascriptTestContextProvider.java    From intellij with Apache License 2.0 5 votes vote down vote up
private static boolean isImportingTestSuite(PsiElement element) {
  JSCallExpression call = PsiTreeUtil.getChildOfType(element, JSCallExpression.class);
  if (call == null || !Objects.equals(call.getMethodExpression().getText(), "goog.require")) {
    return false;
  }
  JSExpression[] arguments = call.getArguments();
  if (arguments.length != 1) {
    return false;
  }
  if (!(arguments[0] instanceof JSLiteralExpression)) {
    return false;
  }
  JSLiteralExpression literal = (JSLiteralExpression) arguments[0];
  return Objects.equals(literal.getStringValue(), "goog.testing.testSuite");
}
 
Example #10
Source File: TestUtilToClassConverter.java    From needsmoredojo with Apache License 2.0 5 votes vote down vote up
@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'"));
}
 
Example #11
Source File: MockJSArrayLiteralExpression.java    From needsmoredojo with Apache License 2.0 5 votes vote down vote up
public MockJSArrayLiteralExpression(String[] defines) {
    super(mock(ASTNode.class));

    expressions = new JSExpression[defines.length];
    for(int i=0;i<defines.length;i++)
    {
        expressions[i] = new MockJSLiteralExpression(defines[i]);
    }
}
 
Example #12
Source File: BasicPsiElements.java    From needsmoredojo with Apache License 2.0 5 votes vote down vote up
public static JSExpression expressionFromText(final String text)
{
    return new JSExpressionImpl(mock(ASTNode.class)) {
        @Override
        public String getText()
        {
            return text;
        }
    };
}
 
Example #13
Source File: RTRepeatExpression.java    From react-templates-plugin with MIT License 5 votes vote down vote up
public JSExpression getCollection() {
    final ASTNode myNode = getNode();
    final ASTNode secondExpression = myNode.findChildByType(
            JSElementTypes.EXPRESSIONS, myNode.findChildByType(JSTokenTypes.IN_KEYWORD)
    );
    return secondExpression != null ? (JSExpression) secondExpression.getPsi() : null;
}
 
Example #14
Source File: RTRepeatExpression.java    From react-templates-plugin with MIT License 5 votes vote down vote up
public JSExpression getCollection() {
    final ASTNode myNode = getNode();
    final ASTNode secondExpression = myNode.findChildByType(
            JSElementTypes.EXPRESSIONS, myNode.findChildByType(JSTokenTypes.IN_KEYWORD)
    );
    return secondExpression != null ? (JSExpression) secondExpression.getPsi() : null;
}
 
Example #15
Source File: MockJSAssignmentExpression.java    From needsmoredojo with Apache License 2.0 5 votes vote down vote up
@Override
public JSExpression[] getChildren()
{
    if(definition == null)
    {
        return new JSExpression[] { new MockJSDefinitionExpression(definitionNamespace, definitionName), BasicPsiElements.expressionFromText(content) };
    }
    else
    {
        return new JSExpression[] { this.definition, BasicPsiElements.expressionFromText(content)};
    }
}
 
Example #16
Source File: MockJSArrayLiteralExpression.java    From needsmoredojo with Apache License 2.0 4 votes vote down vote up
@Override
public JSExpression[] getExpressions()
{
    return expressions;
}
 
Example #17
Source File: DojoModuleFileResolver.java    From needsmoredojo with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example #18
Source File: DeclareStatementItems.java    From needsmoredojo with Apache License 2.0 4 votes vote down vote up
public DeclareStatementItems(JSExpression[] expressionsToMixin, JSProperty[] methodsToConvert, JSElement returnStatement) {
    this.expressionsToMixin = expressionsToMixin;
    this.methodsToConvert = methodsToConvert;
    this.declareContainingStatement = returnStatement;
}
 
Example #19
Source File: DeclareStatementItems.java    From needsmoredojo with Apache License 2.0 4 votes vote down vote up
public DeclareStatementItems(JSLiteralExpression className, JSExpression[] expressionsToMixin, JSProperty[] methodsToConvert, JSElement declareContainingStatement) {
    this(expressionsToMixin, methodsToConvert, declareContainingStatement);

    this.className = className;
}
 
Example #20
Source File: DeclareStatementItems.java    From needsmoredojo with Apache License 2.0 4 votes vote down vote up
public JSExpression[] getExpressionsToMixin() {
    return expressionsToMixin;
}
 
Example #21
Source File: ModuleImporter.java    From needsmoredojo with Apache License 2.0 4 votes vote down vote up
/**
 * Determines if one module references another module, and returns a result if it does
 *
 * @param newModuleName the referenced module's name
 * @param statement the module's parsed define statement
 * @param targetFile the module file
 * @return
 */
protected @Nullable MatchResult getMatch(@NotNull String newModuleName, @NotNull DefineStatement statement, @NotNull PsiFile targetFile)
{
    // smoke test
    if(!statement.getArguments().getText().contains(moduleName))
    {
        return null;
    }

    // get a list of possible modules and their syntax
    LinkedHashMap<String, PsiFile> results = new ImportResolver().getChoicesFromFiles(possibleFiles, libraries, moduleName, targetFile, false, true);

    // go through the defines and determine if there is a match
    int matchIndex = -1;
    String matchedString = "";
    String matchedPostfix = "";
    char quote = '\'';

    for(int i=0;i<statement.getArguments().getExpressions().length;i++)
    {
        JSExpression argument = statement.getArguments().getExpressions()[i];

        if(argument.getText().contains("\""))
        {
            quote = '"';
        }

        String argumentText = argument.getText().replaceAll("'", "").replaceAll("\"", "");
        if(argumentText.contains(moduleName))
        {
            StringBuilder b = new StringBuilder(argumentText);
            b.replace(argumentText.lastIndexOf(moduleName), argumentText.lastIndexOf(moduleName) + moduleName.length(), newModuleName );
            argumentText = b.toString();
        }

        // in case it's a plugin
        // TODO unit test please !
        String modulePath = NameResolver.getModulePath(argumentText);
        String finalArgumentText = modulePath + NameResolver.getModuleName(argumentText);
        String pluginPostFix = NameResolver.getAMDPluginResourceIfPossible(argumentText, true);

        if(results.containsKey(finalArgumentText))
        {
            matchIndex = i;
            matchedString = finalArgumentText;
            matchedPostfix = pluginPostFix;
            break;
        }
    }

    if(matchIndex == -1)
    {
        return null;
    }

    return new MatchResult(targetFile, matchIndex, matchedString, quote, matchedPostfix, null, statement.getCallExpression());
}
 
Example #22
Source File: MockJSDefinitionExpressionWithIndexedProperty.java    From needsmoredojo with Apache License 2.0 4 votes vote down vote up
@Override
public JSExpression getExpression()
{
    return new MockJSIndexedPropertyAccessExpression(definitionName);
}
 
Example #23
Source File: MockJSExpressionStatement.java    From needsmoredojo with Apache License 2.0 4 votes vote down vote up
@Override
public JSExpression getExpression()
{
    return expression;
}
 
Example #24
Source File: MockJSArrayLiteralExpression.java    From needsmoredojo with Apache License 2.0 4 votes vote down vote up
public void setExpressions(JSExpression[] expressions) {
    this.expressions = expressions;
}
 
Example #25
Source File: MockJSDefinitionExpression.java    From needsmoredojo with Apache License 2.0 4 votes vote down vote up
@Override
public JSExpression getExpression()
{
    return new MockJSReferenceExpression(definitionName);
}
 
Example #26
Source File: MockJSIndexedPropertyAccessExpression.java    From needsmoredojo with Apache License 2.0 4 votes vote down vote up
@Override
public JSExpression getIndexExpression()
{
    return BasicPsiElements.expressionFromText(this.value);
}
 
Example #27
Source File: MockJSCallExpression.java    From needsmoredojo with Apache License 2.0 4 votes vote down vote up
@Override
public JSExpression[] getArguments()
{
    return arguments;
}
 
Example #28
Source File: MockJSCallExpression.java    From needsmoredojo with Apache License 2.0 4 votes vote down vote up
public MockJSCallExpression(JSExpression[] arguments) {
    super(mock(ASTNode.class));

    this.arguments = arguments;
}
 
Example #29
Source File: MockJSExpressionStatement.java    From needsmoredojo with Apache License 2.0 4 votes vote down vote up
public MockJSExpressionStatement(JSExpression expression) {
    super(mock(ASTNode.class));

    this.expression = expression;
}