참고소스 수정본
This commit is contained in:
2
참고/playwright-main/utils/doclint/linting-code-snippets/java/.gitignore
vendored
Normal file
2
참고/playwright-main/utils/doclint/linting-code-snippets/java/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
target/
|
||||
dependency-reduced-pom.xml
|
||||
@@ -0,0 +1,53 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.example</groupId>
|
||||
<artifactId>java-syntax-checker</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>15</maven.compiler.source>
|
||||
<maven.compiler.target>15</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.github.javaparser</groupId>
|
||||
<artifactId>javaparser-core</artifactId>
|
||||
<version>3.26.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.11.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>3.5.1</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<transformers>
|
||||
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||
<mainClass>JavaSyntaxChecker</mainClass>
|
||||
</transformer>
|
||||
</transformers>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,110 @@
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import com.github.javaparser.JavaParser;
|
||||
import com.github.javaparser.Problem;
|
||||
import com.github.javaparser.ParseResult;
|
||||
import com.github.javaparser.ast.CompilationUnit;
|
||||
import com.github.javaparser.ParserConfiguration;
|
||||
import com.github.javaparser.StaticJavaParser;
|
||||
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class JavaSyntaxChecker {
|
||||
public static void main(String[] args) {
|
||||
if (args.length == 0) {
|
||||
System.out.println("Error: Please provide the path to the JSON file");
|
||||
return;
|
||||
}
|
||||
|
||||
String codeSnippetsPath = args[args.length - 1];
|
||||
List<CodeSnippet> codeSnippets = readCodeSnippets(codeSnippetsPath);
|
||||
if (codeSnippets == null) {
|
||||
System.out.println("Error: codeSnippets is null");
|
||||
return;
|
||||
}
|
||||
|
||||
List<Map<String, Object>> output = new ArrayList<>();
|
||||
|
||||
ParserConfiguration config = new ParserConfiguration();
|
||||
config.setLanguageLevel(ParserConfiguration.LanguageLevel.JAVA_17);
|
||||
|
||||
for (CodeSnippet codeSnippet : codeSnippets) {
|
||||
String cleanedCode = cleanSnippet(codeSnippet.code);
|
||||
ParseResult<CompilationUnit> parseResult = new JavaParser(config).parse(cleanedCode);
|
||||
List<Problem> syntaxErrors = parseResult.getProblems();
|
||||
|
||||
if (!syntaxErrors.isEmpty()) {
|
||||
output.add(Map.of(
|
||||
"status", "error",
|
||||
"error", String.join("\n", syntaxErrors.stream()
|
||||
.map(Problem::getMessage)
|
||||
.collect(Collectors.toList()))
|
||||
));
|
||||
} else {
|
||||
output.add(Map.of("status", "ok"));
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println(new Gson().toJson(output));
|
||||
}
|
||||
|
||||
private static String removeImports(String code) {
|
||||
// Remove import statements
|
||||
return Pattern.compile("^import.*;$", Pattern.MULTILINE)
|
||||
.matcher(code)
|
||||
.replaceAll("");
|
||||
}
|
||||
|
||||
private static String cleanSnippet(String code) {
|
||||
// if it contains "public class" then it's a full class, return immediately
|
||||
if (code.contains("public class")) {
|
||||
return code;
|
||||
}
|
||||
code = removeImports(code);
|
||||
String wrappedCode = """
|
||||
import com.microsoft.playwright.*;
|
||||
import static com.microsoft.playwright.assertions.PlaywrightAssertions.*;
|
||||
|
||||
public class Example {
|
||||
public static void main(String[] args) {
|
||||
try (Playwright playwright = Playwright.create()) {
|
||||
Browser browser = playwright.chromium().launch();
|
||||
BrowserContext context = browser.newContext();
|
||||
Page page = context.newPage();
|
||||
%s
|
||||
}
|
||||
}
|
||||
}
|
||||
""".formatted(code);
|
||||
return wrappedCode;
|
||||
}
|
||||
|
||||
private static List<CodeSnippet> readCodeSnippets(String filePath) {
|
||||
try (FileReader reader = new FileReader(filePath)) {
|
||||
Type listType = new TypeToken<ArrayList<CodeSnippet>>(){}.getType();
|
||||
return new Gson().fromJson(reader, listType);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class CodeSnippet {
|
||||
String filePath;
|
||||
String codeLang;
|
||||
String code;
|
||||
|
||||
public CodeSnippet(String filePath, String codeLang, String code) {
|
||||
this.filePath = filePath;
|
||||
this.codeLang = codeLang;
|
||||
this.code = code;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user