Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor update to JavaScriptParser to generate correct AST elements. #73

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions js/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"display": "Node 14",

"compilerOptions": {
"lib": ["es2020"],
"lib": ["esnext"],
Copy link
Member

@zieka zieka Nov 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I imagine this is fine for now; is the plan:

graph LR
    A[.ts file] -->|ts AST via tsc| B((ts AST))
    B -->|map to LST| C((LST))
    C -->|.ts file via openrewrite printer| D[.ts file]
Loading

Copy link
Contributor Author

@traceyyoshima traceyyoshima Nov 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the supported extensions are set to:

  • .js, .jsx, .mjs, .cjs
  • .ts, .tsx, .mts, .cts

There are a few configurations I am unsure of:

  1. Should we set the ScriptTarget based on the project? ES2000, ES2020, ESNEXT, etc.
  2. Is it safe to set the ScriptKind to the TS equivalent of a JS file (JS -> TS, JSX -> TSX), or should we let the extension set the behavior in the compiler?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what the ScriptTarget settings affect in the compiler. I changed it so that the Scanner would be set to the same version of the lib and target in the compiler options.

"module": "commonjs",
"target": "es2020",
"target": "esnext",
"resolveJsonModule": true,
"strict": true,
"esModuleInterop": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public boolean accept(Path path) {

@Override
public Path sourcePathFromSourceText(Path prefix, String sourceCode) {
return prefix.resolve("file.js");
return prefix.resolve("file.ts");
}

public static Builder builder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
package org.openrewrite.javascript.internal;

import org.openrewrite.FileAttributes;
import org.openrewrite.ParseExceptionResult;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.internal.JavaTypeCache;
import org.openrewrite.java.marker.Semicolon;
import org.openrewrite.java.marker.TrailingComma;
import org.openrewrite.java.tree.*;
import org.openrewrite.javascript.JavaScriptParser;
import org.openrewrite.javascript.TypeScriptTypeMapping;
import org.openrewrite.javascript.internal.tsc.TSCNode;
import org.openrewrite.javascript.internal.tsc.TSCNodeList;
Expand Down Expand Up @@ -78,14 +80,16 @@ public JS.CompilationUnit visitSourceFile() {
Space childPrefix = whitespace();
String text = child.getText();
skip(text);
Markers childMarkers = Markers.build(singletonList(ParseExceptionResult.build(JavaScriptParser.builder().build(), t)
.withTreeType(child.syntaxKind().name())));
visited = new J.Unknown(
randomId(),
childPrefix,
Markers.EMPTY,
new J.Unknown.Source(
randomId(),
EMPTY,
Markers.EMPTY,
childMarkers,
text));
}

Expand Down Expand Up @@ -3164,7 +3168,8 @@ private <J2 extends J> List<JRightPadded<J2>> convertAll(List<TSCNode> elements,
new J.Unknown.Source(
randomId(),
EMPTY,
Markers.EMPTY,
Markers.build(singletonList(ParseExceptionResult.build(JavaScriptParser.builder().build(), e)
.withTreeType(element.syntaxKind().name()))),
text));
}
Space after = i == elements.size() - 1 ? suffix.apply(element) : innerSuffix.apply(element);
Expand Down Expand Up @@ -3247,7 +3252,8 @@ private <T extends J> JContainer<T> mapContainer(TSCSyntaxKind open, List<TSCNod
new J.Unknown.Source(
randomId(),
EMPTY,
Markers.EMPTY,
Markers.build(singletonList(ParseExceptionResult.build(JavaScriptParser.builder().build(), e)
.withTreeType(node.syntaxKind().name()))),
text));
} else {
throw e;
Expand Down Expand Up @@ -3503,7 +3509,8 @@ private J unknown(TSCNode node) {
new J.Unknown.Source(
randomId(),
EMPTY,
Markers.EMPTY,
Markers.build(singletonList(ParseExceptionResult.build(JavaScriptParser.builder().build(), new UnsupportedOperationException(node.syntaxKind().name() + " not implemented"))
.withTreeType(node.syntaxKind().name()))),
text));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.openrewrite.javascript.tree;

import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.ExpectedToFail;

@SuppressWarnings("JSUnusedLocalSymbols")
class MethodInvocationTest extends ParserTest {
Expand Down Expand Up @@ -45,6 +46,7 @@ export default function ( req ) {
);
}

@ExpectedToFail("Requires CallExpression#typeArguments")
@Test
void typeArguments() {
rewriteRun(
Expand Down
Loading