Skip to content

Commit

Permalink
Move printer code to typescript (#496)
Browse files Browse the repository at this point in the history
* chore: setup Typescript

* refactor: move arrays printer to TS

* chore: update parser signature

* refactor: move types values & variables to TS

* refactor: move expressions to ts

* refactor: move comments utils to TS

* refactor: move blocks and statements to TS

* refactor: move printer-utils to TS

* refactor: move classes to TS

* refactor: move interfaces to TS

* refactor: move lexical structures, names & packages & modules to TS

* refactor: move all tests to TS

* chore: configure package for release with TS

* chore: update github workflows
  • Loading branch information
clementdessoude authored Aug 28, 2021
1 parent f0c2e40 commit 53c6d2b
Show file tree
Hide file tree
Showing 141 changed files with 3,795 additions and 1,156 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ packages/java-parser/samples
packages/prettier-plugin-java/samples
packages/prettier-plugin-java/test-samples
node_modules
packages/prettier-plugin-java/dist
babel.config.js
6 changes: 4 additions & 2 deletions .github/workflows/github-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ jobs:
matrix:
node_version:
- 12.x
- 13.x
- 14.x
- 16.x
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
Expand All @@ -52,7 +52,7 @@ jobs:
test_repository:
- e2e-jhipster1
- e2e-jhipster2
node_version: [12.x]
node_version: [14.x]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
Expand All @@ -63,5 +63,7 @@ jobs:
java-version: '11.x'
- name: Install dependencies
run: yarn
- name: Build prettier-plugin-java
run: yarn run build:prettier-plugin-java
- name: Run e2e tests
run: yarn run test:prettier-plugin-java test:${{ matrix.test_repository }}
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ lerna.json
packages/prettier-plugin-java/samples
packages/prettier-plugin-java/test-samples
packages/prettier-plugin-java/README.md
packages/prettier-plugin-java/dist
CHANGELOG*
.nyc_output
coverage
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"lint": "eslint packages/**/*.js",
"format:fix": "prettier --write \"**/*.@(js|json|md)\"",
"format:validate": "prettier --list-different \"**/*.@(js|json|md)\"",
"build": "node packages/java-parser/scripts/unicode.js packages/java-parser/resources/Unicode/UnicodeData.txt && prettier --write packages/java-parser/src/unicodesets.js && node packages/java-parser/scripts/gen-diagrams.js",
"build": "yarn build:prettier-plugin-java && node packages/java-parser/scripts/unicode.js packages/java-parser/resources/Unicode/UnicodeData.txt && prettier --write packages/java-parser/src/unicodesets.js && node packages/java-parser/scripts/gen-diagrams.js",
"build:prettier-plugin-java": "cd packages/prettier-plugin-java && yarn build && cd ../..",
"prettier": "prettier --plugin=./packages/prettier-plugin-java --parser=java",
"update-test-outputs": "node packages/prettier-plugin-java/scripts/update-test-output.js"
},
Expand Down
134 changes: 127 additions & 7 deletions packages/java-parser/api.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,34 @@
import { CstNode, ICstVisitor, IToken } from "chevrotain";
import {
CstNode as ChevrotainCstNode,
CstNodeLocation,
ICstVisitor,
IToken as ChevrotainIToken
} from "chevrotain";

export interface CstNode extends ChevrotainCstNode {
children: CstChildrenDictionary;
leadingComments?: IToken[];
trailingComments?: IToken[];
ignore?: boolean;
location: CstNodeLocation;
}

export interface IToken extends ChevrotainIToken {
leadingComments?: IToken[];
trailingComments?: IToken[];
startOffset: number;
startLine: number;
startColumn: number;
endOffset: number;
endLine: number;
endColumn: number;
}

export type CstElement = IToken | CstNode;

export declare type CstChildrenDictionary = {
[identifier: string]: CstElement[];
};

export function parse(text: string, startProduction?: string): CstNode;

Expand Down Expand Up @@ -342,6 +372,8 @@ export abstract class JavaCstVisitor<IN, OUT> implements ICstVisitor<IN, OUT> {
classLiteralSuffix(ctx: ClassLiteralSuffixCtx, param?: IN): OUT;
arrayAccessSuffix(ctx: ArrayAccessSuffixCtx, param?: IN): OUT;
methodReferenceSuffix(ctx: MethodReferenceSuffixCtx, param?: IN): OUT;
pattern(ctx: PatternCtx, param?: IN): OUT;
typePattern(ctx: TypePatternCtx, param?: IN): OUT;
identifyNewExpressionType(ctx: IdentifyNewExpressionTypeCtx, param?: IN): OUT;
isLambdaExpression(ctx: IsLambdaExpressionCtx, param?: IN): OUT;
isCastExpression(ctx: IsCastExpressionCtx, param?: IN): OUT;
Expand All @@ -358,7 +390,8 @@ interface JavaCstVisitorConstructor<IN, OUT> {
}

export abstract class JavaCstVisitorWithDefaults<IN, OUT>
implements ICstVisitor<IN, OUT> {
implements ICstVisitor<IN, OUT>
{
// No need to implement these two methods
// Generic Visit method implemented by the Chevrotain Library
visit(cstNode: CstNode | CstNode[], param?: IN): OUT;
Expand Down Expand Up @@ -692,6 +725,8 @@ export abstract class JavaCstVisitorWithDefaults<IN, OUT>
classLiteralSuffix(ctx: ClassLiteralSuffixCtx, param?: IN): OUT;
arrayAccessSuffix(ctx: ArrayAccessSuffixCtx, param?: IN): OUT;
methodReferenceSuffix(ctx: MethodReferenceSuffixCtx, param?: IN): OUT;
pattern(ctx: PatternCtx, param?: IN): OUT;
typePattern(ctx: TypePatternCtx, param?: IN): OUT;
identifyNewExpressionType(ctx: IdentifyNewExpressionTypeCtx, param?: IN): OUT;
isLambdaExpression(ctx: IsLambdaExpressionCtx, param?: IN): OUT;
isCastExpression(ctx: IsCastExpressionCtx, param?: IN): OUT;
Expand Down Expand Up @@ -814,6 +849,7 @@ export interface ReferenceTypeCstNode extends CstNode {
export type ReferenceTypeCtx = {
annotation?: AnnotationCstNode[];
primitiveType?: PrimitiveTypeCstNode[];
dims?: DimsCstNode[];
classOrInterfaceType?: ClassOrInterfaceTypeCstNode[];
};

Expand Down Expand Up @@ -1220,6 +1256,7 @@ export interface VariableDeclaratorCstNode extends CstNode {
export type VariableDeclaratorCtx = {
variableDeclaratorId: VariableDeclaratorIdCstNode[];
Equals?: IToken[];
variableInitializer?: VariableInitializerCstNode[];
};

export interface VariableDeclaratorIdCstNode extends CstNode {
Expand Down Expand Up @@ -1358,6 +1395,7 @@ export interface MethodHeaderCstNode extends CstNode {

export type MethodHeaderCtx = {
typeParameters?: TypeParametersCstNode[];
annotation?: AnnotationCstNode[];
result: ResultCstNode[];
methodDeclarator: MethodDeclaratorCstNode[];
throws?: ThrowsCstNode[];
Expand Down Expand Up @@ -1395,6 +1433,7 @@ export type ReceiverParameterCtx = {
annotation?: AnnotationCstNode[];
unannType: UnannTypeCstNode[];
Identifier?: IToken[];
Dot?: IToken[];
This: IToken[];
};

Expand Down Expand Up @@ -1544,6 +1583,7 @@ export type ConstructorDeclaratorCtx = {
simpleTypeName: SimpleTypeNameCstNode[];
LBrace: IToken[];
receiverParameter?: ReceiverParameterCstNode[];
Comma?: IToken[];
formalParameterList?: FormalParameterListCstNode[];
RBrace: IToken[];
};
Expand Down Expand Up @@ -1656,6 +1696,8 @@ export type EnumConstantCtx = {
enumConstantModifier?: EnumConstantModifierCstNode[];
Identifier: IToken[];
LBrace?: IToken[];
argumentList?: ArgumentListCstNode[];
RBrace?: IToken[];
classBody?: ClassBodyCstNode[];
};

Expand Down Expand Up @@ -1817,6 +1859,9 @@ export type IsDimsCtx = {
At?: IToken[];
typeName?: TypeNameCstNode[];
LBrace?: IToken[];
elementValuePairList?: ElementValuePairListCstNode[];
elementValue?: ElementValueCstNode[];
RBrace?: IToken[];
};

export interface IsCompactConstructorDeclarationCstNode extends CstNode {
Expand All @@ -1838,12 +1883,20 @@ export interface CompilationUnitCstNode extends CstNode {
children: CompilationUnitCtx;
}

export type CompilationUnitCtx = {
ordinaryCompilationUnit?: OrdinaryCompilationUnitCstNode[];
modularCompilationUnit?: ModularCompilationUnitCstNode[];
export type AbstractOrdinaryCompilationUnitCtx = {
ordinaryCompilationUnit: OrdinaryCompilationUnitCstNode[];
EOF: IToken[];
};

export type AbstractModularCompilationUnitCtx = {
modularCompilationUnit: OrdinaryCompilationUnitCstNode[];
EOF: IToken[];
};

export type CompilationUnitCtx =
| AbstractOrdinaryCompilationUnitCtx
| AbstractModularCompilationUnitCtx;

export interface OrdinaryCompilationUnitCstNode extends CstNode {
name: "ordinaryCompilationUnit";
children: OrdinaryCompilationUnitCtx;
Expand Down Expand Up @@ -1894,6 +1947,11 @@ export interface ImportDeclarationCstNode extends CstNode {

export type ImportDeclarationCtx = {
Import?: IToken[];
Static?: IToken[];
packageOrTypeName?: PackageOrTypeNameCstNode[];
Dot?: IToken[];
Star?: IToken[];
Semicolon?: IToken[];
emptyStatement?: EmptyStatementCstNode[];
};

Expand Down Expand Up @@ -1958,6 +2016,8 @@ export type ExportsModuleDirectiveCtx = {
Exports: IToken[];
packageName: PackageNameCstNode[];
To?: IToken[];
moduleName?: ModuleNameCstNode[];
Comma?: IToken[];
Semicolon: IToken[];
};

Expand All @@ -1970,6 +2030,8 @@ export type OpensModuleDirectiveCtx = {
Opens: IToken[];
packageName: PackageNameCstNode[];
To?: IToken[];
moduleName?: ModuleNameCstNode[];
Comma?: IToken[];
Semicolon: IToken[];
};

Expand Down Expand Up @@ -2237,6 +2299,9 @@ export type AnnotationCtx = {
At: IToken[];
typeName: TypeNameCstNode[];
LBrace?: IToken[];
elementValuePairList?: ElementValuePairListCstNode[];
elementValue?: ElementValueCstNode[];
RBrace?: IToken[];
};

export interface ElementValuePairListCstNode extends CstNode {
Expand Down Expand Up @@ -2387,6 +2452,7 @@ export interface BlockStatementCstNode extends CstNode {
export type BlockStatementCtx = {
localVariableDeclarationStatement?: LocalVariableDeclarationStatementCstNode[];
classDeclaration?: ClassDeclarationCstNode[];
interfaceDeclaration?: InterfaceDeclarationCstNode[];
statement?: StatementCstNode[];
};

Expand Down Expand Up @@ -2563,6 +2629,8 @@ export interface SwitchLabelCstNode extends CstNode {

export type SwitchLabelCtx = {
Case?: IToken[];
caseConstant?: CaseConstantCstNode[];
Comma?: IToken[];
Default?: IToken[];
};

Expand All @@ -2577,6 +2645,7 @@ export type SwitchRuleCtx = {
throwStatement?: ThrowStatementCstNode[];
block?: BlockCstNode[];
expression?: ExpressionCstNode[];
Semicolon?: IToken[];
};

export interface CaseConstantCstNode extends CstNode {
Expand Down Expand Up @@ -2752,6 +2821,9 @@ export interface TryStatementCstNode extends CstNode {

export type TryStatementCtx = {
Try?: IToken[];
block?: BlockCstNode[];
catches?: CatchesCstNode[];
finally?: FinallyCstNode[];
tryWithResourcesStatement?: TryWithResourcesStatementCstNode[];
};

Expand Down Expand Up @@ -3041,6 +3113,8 @@ export interface TernaryExpressionCstNode extends CstNode {
export type TernaryExpressionCtx = {
binaryExpression: BinaryExpressionCstNode[];
QuestionMark?: IToken[];
expression?: ExpressionCstNode[];
Colon?: IToken[];
};

export interface BinaryExpressionCstNode extends CstNode {
Expand All @@ -3051,7 +3125,10 @@ export interface BinaryExpressionCstNode extends CstNode {
export type BinaryExpressionCtx = {
unaryExpression: UnaryExpressionCstNode[];
Instanceof?: IToken[];
pattern?: PatternCstNode[];
referenceType?: ReferenceTypeCstNode[];
AssignmentOperator?: IToken[];
expression?: ExpressionCstNode[];
Less?: IToken[];
Greater?: IToken[];
BinaryOperator?: IToken[];
Expand Down Expand Up @@ -3113,6 +3190,10 @@ export interface PrimarySuffixCstNode extends CstNode {

export type PrimarySuffixCtx = {
Dot?: IToken[];
This?: IToken[];
unqualifiedClassInstanceCreationExpression?: UnqualifiedClassInstanceCreationExpressionCstNode[];
typeArguments?: TypeArgumentsCstNode[];
Identifier?: IToken[];
methodInvocationSuffix?: MethodInvocationSuffixCstNode[];
classLiteralSuffix?: ClassLiteralSuffixCstNode[];
arrayAccessSuffix?: ArrayAccessSuffixCstNode[];
Expand All @@ -3138,10 +3219,20 @@ export interface FqnOrRefTypePartRestCstNode extends CstNode {

export type FqnOrRefTypePartRestCtx = {
annotation?: AnnotationCstNode[];
typeArguments?: TypeArgumentsCstNode[];
$methodTypeArguments?: FqnOrRefTypePartRest$MethodTypeArgumentsCstNode[];
fqnOrRefTypePartCommon: FqnOrRefTypePartCommonCstNode[];
};

export interface FqnOrRefTypePartRest$MethodTypeArgumentsCstNode
extends CstNode {
name: "fqnOrRefTypePartRest$methodTypeArguments";
children: FqnOrRefTypePartRest$MethodTypeArgumentsCtx;
}

export type FqnOrRefTypePartRest$MethodTypeArgumentsCtx = {
typeArguments: TypeArgumentsCstNode[];
};

export interface FqnOrRefTypePartCommonCstNode extends CstNode {
name: "fqnOrRefTypePartCommon";
children: FqnOrRefTypePartCommonCtx;
Expand All @@ -3150,7 +3241,17 @@ export interface FqnOrRefTypePartCommonCstNode extends CstNode {
export type FqnOrRefTypePartCommonCtx = {
Identifier?: IToken[];
Super?: IToken[];
typeArguments?: TypeArgumentsCstNode[];
$classTypeArguments?: FqnOrRefTypePartCommon$ClassTypeArgumentsCstNode[];
};

export interface FqnOrRefTypePartCommon$ClassTypeArgumentsCstNode
extends CstNode {
name: "fqnOrRefTypePartCommon$classTypeArguments";
children: FqnOrRefTypePartCommon$ClassTypeArgumentsCtx;
}

export type FqnOrRefTypePartCommon$ClassTypeArgumentsCtx = {
typeArguments: TypeArgumentsCstNode[];
};

export interface FqnOrRefTypePartFirstCstNode extends CstNode {
Expand Down Expand Up @@ -3378,6 +3479,24 @@ export type MethodReferenceSuffixCtx = {
New?: IToken[];
};

export interface PatternCstNode extends CstNode {
name: "pattern";
children: PatternCtx;
}

export type PatternCtx = {
typePattern: TypePatternCstNode[];
};

export interface TypePatternCstNode extends CstNode {
name: "typePattern";
children: TypePatternCtx;
}

export type TypePatternCtx = {
localVariableDeclaration: LocalVariableDeclarationCstNode[];
};

export interface IdentifyNewExpressionTypeCstNode extends CstNode {
name: "identifyNewExpressionType";
children: IdentifyNewExpressionTypeCtx;
Expand Down Expand Up @@ -3434,4 +3553,5 @@ export type IsRefTypeInMethodRefCtx = {
typeArguments: TypeArgumentsCstNode[];
dims?: DimsCstNode[];
Dot?: IToken[];
classOrInterfaceType?: ClassOrInterfaceTypeCstNode[];
};
10 changes: 5 additions & 5 deletions packages/java-parser/scripts/generate-signature.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ const enrichChild = ({ definition, children, optional }) => {
enrichChildrenWithMandatoryRepetition(definition, children);
break;
case "Flat":
enrichChild({ definition: definition.definition[0], optional, children });
definition.definition.forEach(innerDefinition => {
enrichChild({ definition: innerDefinition, optional, children });
});
break;
case "Alternative":
case "Option":
enrichChild({
definition: definition.definition[0],
optional: true,
children
definition.definition.forEach(innerDefinition => {
enrichChild({ definition: innerDefinition, optional: true, children });
});
break;
// TODO: add other GAST structures
Expand Down
Loading

0 comments on commit 53c6d2b

Please sign in to comment.