java-parser
Advanced tools
Comparing version 0.5.1 to 0.6.0
{ | ||
"name": "java-parser", | ||
"version": "0.5.1", | ||
"version": "0.6.0", | ||
"description": "Java Parser in JavaScript", | ||
@@ -17,3 +17,3 @@ "main": "src/index.js", | ||
}, | ||
"gitHead": "addd92b0bbc1260c5f76de7758d2cc1199db4776" | ||
"gitHead": "e63c141ec03b079b76317db4fa2a75d7c01fa267" | ||
} |
"use strict"; | ||
const _ = require("lodash"); | ||
/** | ||
@@ -37,2 +39,8 @@ * Search where is the position of the comment in the token array by | ||
function isFormatterOffOnComment(comment) { | ||
return comment.image.match( | ||
/(\/\/(\s*)@formatter:(off|on)(\s*))|(\/\*(\s*)@formatter:(off|on)(\s*)\*\/)/gm | ||
); | ||
} | ||
/** | ||
@@ -62,2 +70,20 @@ * Pre-processing of tokens in order to | ||
function extendRangeOffset(comments, tokens) { | ||
let position; | ||
comments.forEach(comment => { | ||
position = findUpperBoundToken(tokens, comment); | ||
const extendedStartOffset = | ||
position - 1 < 0 ? comment.startOffset : tokens[position - 1].endOffset; | ||
const extendedEndOffset = | ||
position == tokens.length | ||
? comment.endOffset | ||
: tokens[position].startOffset; | ||
comment.extendedOffset = { | ||
startOffset: extendedStartOffset, | ||
endOffset: extendedEndOffset | ||
}; | ||
}); | ||
} | ||
/** | ||
@@ -69,24 +95,13 @@ * Create two data structures we use to know at which offset a comment can be attached. | ||
* @param {ITokens[]} tokens - array of tokens | ||
* @param {[]} comments - array of comments | ||
* | ||
* @return {{commentsByExtendedStartOffset: {[extendedStartOffset: number]: Comment[]}, commentsByExtendedEndOffset: {[extendedEndOffset: number]: Comment[]}}} | ||
*/ | ||
function mapCommentsByExtendedRange(tokens, comments) { | ||
function mapCommentsByExtendedRange(comments) { | ||
const commentsByExtendedEndOffset = {}; | ||
const commentsByExtendedStartOffset = {}; | ||
let position; | ||
comments.forEach(comment => { | ||
position = findUpperBoundToken(tokens, comment); | ||
const extendedStartOffset = comment.extendedOffset.startOffset; | ||
const extendedEndOffset = comment.extendedOffset.endOffset; | ||
const extendedStartOffset = | ||
position - 1 < 0 ? comment.startOffset : tokens[position - 1].endOffset; | ||
const extendedEndOffset = | ||
position == tokens.length | ||
? comment.endOffset | ||
: tokens[position].startOffset; | ||
comment.extendedOffset = { | ||
endOffset: extendedEndOffset | ||
}; | ||
if (commentsByExtendedEndOffset[extendedEndOffset] === undefined) { | ||
@@ -175,6 +190,8 @@ commentsByExtendedEndOffset[extendedEndOffset] = [comment]; | ||
); | ||
extendRangeOffset(comments, tokens); | ||
const { | ||
commentsByExtendedStartOffset, | ||
commentsByExtendedEndOffset | ||
} = mapCommentsByExtendedRange(tokens, comments); | ||
} = mapCommentsByExtendedRange(comments); | ||
@@ -240,4 +257,64 @@ /* | ||
/** | ||
* Create pairs of formatter:off and formatter:on | ||
* @param comments | ||
* @returns pairs of formatter:off and formatter:on | ||
*/ | ||
function matchFormatterOffOnPairs(comments) { | ||
const onOffComments = comments.filter(comment => | ||
isFormatterOffOnComment(comment) | ||
); | ||
let isPreviousCommentOff = false; | ||
let isCurrentCommentOff = true; | ||
const pairs = []; | ||
let paired = {}; | ||
onOffComments.forEach(comment => { | ||
isCurrentCommentOff = comment.image.slice(-3) === "off"; | ||
if (!isPreviousCommentOff) { | ||
if (isCurrentCommentOff) { | ||
paired.off = comment; | ||
} | ||
} else { | ||
if (!isCurrentCommentOff) { | ||
paired.on = comment; | ||
pairs.push(paired); | ||
paired = {}; | ||
} | ||
} | ||
isPreviousCommentOff = isCurrentCommentOff; | ||
}); | ||
if (onOffComments.length > 0 && isCurrentCommentOff) { | ||
paired.on = undefined; | ||
pairs.push(paired); | ||
} | ||
return pairs; | ||
} | ||
/** | ||
* Check if the node is between formatter:off and formatter:on and change his ignore state | ||
* @param node | ||
* @param commentPairs | ||
*/ | ||
function shouldNotFormat(node, commentPairs) { | ||
const matchingPair = _.findLast( | ||
commentPairs, | ||
comment => comment.off.endOffset < node.location.startOffset | ||
); | ||
if ( | ||
matchingPair !== undefined && | ||
(matchingPair.on === undefined || | ||
matchingPair.on.startOffset > node.location.endOffset) | ||
) { | ||
node.ignore = true; | ||
} | ||
} | ||
module.exports = { | ||
matchFormatterOffOnPairs, | ||
shouldNotFormat, | ||
attachComments | ||
}; |
"use strict"; | ||
const JavaLexer = require("./lexer"); | ||
const JavaParser = require("./parser"); | ||
const { attachComments } = require("./comments"); | ||
const { attachComments, matchFormatterOffOnPairs } = require("./comments"); | ||
@@ -31,2 +31,6 @@ const parser = new JavaParser(); | ||
parser.setOnOffCommentPairs( | ||
matchFormatterOffOnPairs(lexResult.groups.comments) | ||
); | ||
// Automatic CST created when parsing | ||
@@ -33,0 +37,0 @@ const cst = parser[entryPoint](); |
@@ -14,2 +14,3 @@ "use strict"; | ||
const { getSkipValidations } = require("./utils"); | ||
const { shouldNotFormat } = require("./comments"); | ||
@@ -42,5 +43,3 @@ /** | ||
super(allTokens, { | ||
// TODO: Try to Specify max lookahead 2 only where needed | ||
// and use `1` by default | ||
maxLookahead: 2, | ||
maxLookahead: 1, | ||
nodeLocationTracking: "full", | ||
@@ -92,2 +91,4 @@ // traceInitPerf: 2, | ||
] = ruleCstResult; | ||
shouldNotFormat(ruleCstResult, this.onOffCommentPairs); | ||
} | ||
@@ -117,4 +118,8 @@ } | ||
} | ||
setOnOffCommentPairs(onOffCommentPairs) { | ||
this.onOffCommentPairs = onOffCommentPairs; | ||
} | ||
} | ||
module.exports = JavaParser; |
@@ -75,10 +75,13 @@ "use strict"; | ||
$.RULE("statement", () => { | ||
$.OR([ | ||
{ ALT: () => $.SUBRULE($.statementWithoutTrailingSubstatement) }, | ||
{ ALT: () => $.SUBRULE($.labeledStatement) }, | ||
// Spec deviation: combined "IfThenStatement" and "IfThenElseStatement" | ||
{ ALT: () => $.SUBRULE($.ifStatement) }, | ||
{ ALT: () => $.SUBRULE($.whileStatement) }, | ||
{ ALT: () => $.SUBRULE($.forStatement) } | ||
]); | ||
$.OR({ | ||
DEF: [ | ||
{ ALT: () => $.SUBRULE($.statementWithoutTrailingSubstatement) }, | ||
{ ALT: () => $.SUBRULE($.labeledStatement) }, | ||
// Spec deviation: combined "IfThenStatement" and "IfThenElseStatement" | ||
{ ALT: () => $.SUBRULE($.ifStatement) }, | ||
{ ALT: () => $.SUBRULE($.whileStatement) }, | ||
{ ALT: () => $.SUBRULE($.forStatement) } | ||
], | ||
MAX_LOOKAHEAD: 2 | ||
}); | ||
}); | ||
@@ -346,22 +349,25 @@ | ||
$.RULE("tryStatement", () => { | ||
$.OR([ | ||
{ | ||
ALT: () => { | ||
$.CONSUME(t.Try); | ||
$.SUBRULE($.block); | ||
$.OR2([ | ||
{ | ||
ALT: () => { | ||
$.SUBRULE($.catches); | ||
$.OPTION(() => { | ||
$.SUBRULE($.finally); | ||
}); | ||
} | ||
}, | ||
{ ALT: () => $.SUBRULE2($.finally) } | ||
]); | ||
} | ||
}, | ||
{ ALT: () => $.SUBRULE($.tryWithResourcesStatement) } | ||
]); | ||
$.OR({ | ||
DEF: [ | ||
{ | ||
ALT: () => { | ||
$.CONSUME(t.Try); | ||
$.SUBRULE($.block); | ||
$.OR2([ | ||
{ | ||
ALT: () => { | ||
$.SUBRULE($.catches); | ||
$.OPTION(() => { | ||
$.SUBRULE($.finally); | ||
}); | ||
} | ||
}, | ||
{ ALT: () => $.SUBRULE2($.finally) } | ||
]); | ||
} | ||
}, | ||
{ ALT: () => $.SUBRULE($.tryWithResourcesStatement) } | ||
], | ||
MAX_LOOKAHEAD: 2 | ||
}); | ||
}); | ||
@@ -368,0 +374,0 @@ |
@@ -236,31 +236,37 @@ "use strict"; | ||
$.RULE("primarySuffix", () => { | ||
$.OR([ | ||
{ | ||
ALT: () => { | ||
$.CONSUME(t.Dot); | ||
$.OR2([ | ||
{ ALT: () => $.CONSUME(t.This) }, | ||
{ | ||
ALT: () => $.SUBRULE($.unqualifiedClassInstanceCreationExpression) | ||
}, | ||
{ | ||
ALT: () => { | ||
$.OPTION(() => { | ||
$.SUBRULE($.typeArguments); | ||
}); | ||
$.CONSUME(t.Identifier); | ||
$.OR({ | ||
DEF: [ | ||
{ | ||
ALT: () => { | ||
$.CONSUME(t.Dot); | ||
$.OR2([ | ||
{ ALT: () => $.CONSUME(t.This) }, | ||
{ | ||
ALT: () => | ||
$.SUBRULE($.unqualifiedClassInstanceCreationExpression) | ||
}, | ||
{ | ||
ALT: () => { | ||
$.OPTION(() => { | ||
$.SUBRULE($.typeArguments); | ||
}); | ||
$.CONSUME(t.Identifier); | ||
} | ||
} | ||
} | ||
]); | ||
} | ||
}, | ||
{ ALT: () => $.SUBRULE($.methodInvocationSuffix) }, | ||
{ ALT: () => $.SUBRULE($.classLiteralSuffix) }, | ||
{ ALT: () => $.SUBRULE($.arrayAccessSuffix) }, | ||
{ ALT: () => $.SUBRULE($.methodReferenceSuffix) } | ||
]); | ||
]); | ||
} | ||
}, | ||
{ ALT: () => $.SUBRULE($.methodInvocationSuffix) }, | ||
{ ALT: () => $.SUBRULE($.classLiteralSuffix) }, | ||
{ ALT: () => $.SUBRULE($.arrayAccessSuffix) }, | ||
{ ALT: () => $.SUBRULE($.methodReferenceSuffix) } | ||
], | ||
MAX_LOOKAHEAD: 2 | ||
}); | ||
}); | ||
// See https://github.com/jhipster/prettier-java/pull/154 to understand | ||
// why fqnOrRefTypePart is split in two rules (First and Rest) | ||
$.RULE("fqnOrRefType", () => { | ||
$.SUBRULE($.fqnOrRefTypePart); | ||
$.SUBRULE($.fqnOrRefTypePartFirst); | ||
@@ -276,3 +282,3 @@ $.MANY2({ | ||
$.CONSUME(t.Dot); | ||
$.SUBRULE2($.fqnOrRefTypePart); | ||
$.SUBRULE2($.fqnOrRefTypePartRest); | ||
} | ||
@@ -300,3 +306,3 @@ }); | ||
// 5. "Super" may be last or one before last (last may also be first if there is only a single part). | ||
$.RULE("fqnOrRefTypePart", () => { | ||
$.RULE("fqnOrRefTypePartRest", () => { | ||
$.MANY(() => { | ||
@@ -313,2 +319,6 @@ $.SUBRULE($.annotation); | ||
$.SUBRULE($.fqnOrRefTypePartCommon); | ||
}); | ||
$.RULE("fqnOrRefTypePartCommon", () => { | ||
$.OR([ | ||
@@ -341,2 +351,10 @@ { ALT: () => $.CONSUME(t.Identifier) }, | ||
$.RULE("fqnOrRefTypePartFirst", () => { | ||
$.MANY(() => { | ||
$.SUBRULE($.annotation); | ||
}); | ||
$.SUBRULE($.fqnOrRefTypePartCommon); | ||
}); | ||
$.RULE("parenthesisExpression", () => { | ||
@@ -439,6 +457,9 @@ $.CONSUME(t.LBrace); | ||
$.RULE("typeArgumentsOrDiamond", () => { | ||
$.OR([ | ||
{ ALT: () => $.SUBRULE($.diamond) }, | ||
{ ALT: () => $.SUBRULE($.typeArguments) } | ||
]); | ||
$.OR({ | ||
DEF: [ | ||
{ ALT: () => $.SUBRULE($.diamond) }, | ||
{ ALT: () => $.SUBRULE($.typeArguments) } | ||
], | ||
MAX_LOOKAHEAD: 2 | ||
}); | ||
}); | ||
@@ -445,0 +466,0 @@ |
@@ -8,4 +8,7 @@ "use strict"; | ||
// Spec Deviation: extracted the common "interfaceModifier" prefix to avoid backtracking. | ||
$.MANY(() => { | ||
$.SUBRULE($.interfaceModifier); | ||
$.MANY({ | ||
DEF: () => { | ||
$.SUBRULE($.interfaceModifier); | ||
}, | ||
MAX_LOOKAHEAD: 2 | ||
}); | ||
@@ -259,3 +262,4 @@ | ||
], | ||
IGNORE_AMBIGUITIES: true | ||
IGNORE_AMBIGUITIES: true, | ||
MAX_LOOKAHEAD: 2 | ||
}); | ||
@@ -262,0 +266,0 @@ $.CONSUME(t.RBrace); |
@@ -1,2 +0,1 @@ | ||
/* eslint-disable no-unused-vars */ | ||
"use strict"; | ||
@@ -3,0 +2,0 @@ const { createToken: createTokenOrg, Lexer } = require("chevrotain"); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
169295
6122
0