Skip to content

Commit 2553338

Browse files
committed
JS: Autoformat {AST,HTML}Extractor.java
1 parent bac212c commit 2553338

File tree

2 files changed

+65
-50
lines changed

2 files changed

+65
-50
lines changed

javascript/extractor/src/com/semmle/js/extractor/ASTExtractor.java

Lines changed: 62 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -174,30 +174,31 @@ public class ASTExtractor {
174174
private final RegExpExtractor regexpExtractor;
175175
private final ExtractorConfig config;
176176

177-
public ASTExtractor(ExtractorConfig config, LexicalExtractor lexicalExtractor, ScopeManager scopeManager) {
177+
public ASTExtractor(
178+
ExtractorConfig config, LexicalExtractor lexicalExtractor, ScopeManager scopeManager) {
178179
this.config = config;
179180
this.trapwriter = lexicalExtractor.getTrapwriter();
180181
this.locationManager = lexicalExtractor.getLocationManager();
181182
this.contextManager = new SyntacticContextManager();
182183
this.scopeManager = scopeManager;
183184
this.lexicalExtractor = lexicalExtractor;
184185
this.regexpExtractor = new RegExpExtractor(trapwriter, locationManager);
185-
this.toplevelLabel = makeTopLevelLabel(trapwriter, locationManager.getFileLabel(), locationManager.getStartLine(), locationManager.getStartColumn());
186+
this.toplevelLabel =
187+
makeTopLevelLabel(
188+
trapwriter,
189+
locationManager.getFileLabel(),
190+
locationManager.getStartLine(),
191+
locationManager.getStartColumn());
186192
}
187193

188194
/**
189195
* Returns the label for the top-level starting at the given location.
190-
* <p>
191-
* May be used to refer to the top-level before it has been extracted.
196+
*
197+
* <p>May be used to refer to the top-level before it has been extracted.
192198
*/
193-
public static Label makeTopLevelLabel(TrapWriter trapWriter, Label fileLabel, int startLine, int startColumn) {
194-
return trapWriter.globalID(
195-
"script;{"
196-
+ fileLabel
197-
+ "},"
198-
+ startLine
199-
+ ','
200-
+ startColumn);
199+
public static Label makeTopLevelLabel(
200+
TrapWriter trapWriter, Label fileLabel, int startLine, int startColumn) {
201+
return trapWriter.globalID("script;{" + fileLabel + "}," + startLine + ',' + startColumn);
201202
}
202203

203204
public TrapWriter getTrapwriter() {
@@ -254,8 +255,8 @@ public enum IdContext {
254255
TYPE_LABEL,
255256

256257
/**
257-
* An identifier that refers to a variable from inside a type, i.e. the operand to a
258-
* <code>typeof</code> type or left operand to an <code>is</code> type.
258+
* An identifier that refers to a variable from inside a type, i.e. the operand to a <code>
259+
* typeof</code> type or left operand to an <code>is</code> type.
259260
*
260261
* <p>This is generally treated as a type, except a variable binding will be emitted for it.
261262
*/
@@ -273,16 +274,16 @@ public enum IdContext {
273274
/**
274275
* An identifier that occurs in a type-only import.
275276
*
276-
* These may declare a type and/or a namespace, but for compatibility with our AST,
277-
* must be emitted as a VarDecl (with no variable binding).
277+
* <p>These may declare a type and/or a namespace, but for compatibility with our AST, must be
278+
* emitted as a VarDecl (with no variable binding).
278279
*/
279280
TYPE_ONLY_IMPORT,
280281

281282
/**
282283
* An identifier that occurs in a type-only export.
283284
*
284-
* These may refer to a type and/or a namespace, but for compatibility with our AST,
285-
* must be emitted as an ExportVarAccess (with no variable binding).
285+
* <p>These may refer to a type and/or a namespace, but for compatibility with our AST, must be
286+
* emitted as an ExportVarAccess (with no variable binding).
286287
*/
287288
TYPE_ONLY_EXPORT,
288289

@@ -302,8 +303,8 @@ public enum IdContext {
302303
EXPORT,
303304

304305
/**
305-
* An identifier that occurs as a qualified name in a default export expression, such as
306-
* <code>A</code> in <code>export default A.B</code>.
306+
* An identifier that occurs as a qualified name in a default export expression, such as <code>A
307+
* </code> in <code>export default A.B</code>.
307308
*
308309
* <p>This acts like {@link #EXPORT}, except it cannot refer to a type (i.e. it must be a
309310
* variable and/or a namespace).
@@ -314,8 +315,8 @@ public enum IdContext {
314315
* True if this occurs as part of a type annotation, i.e. it is {@link #TYPE_BIND} or {@link
315316
* #TYPE_DECL}, {@link #TYPE_LABEL}, {@link #VAR_IN_TYPE_BIND}, or {@link #NAMESPACE_BIND}.
316317
*
317-
* <p>Does not hold for {@link #VAR_AND_TYPE_DECL}, {@link #TYPE_ONLY_IMPORT}, or @{link {@link #TYPE_ONLY_EXPORT}
318-
* as these do not occur in type annotations.
318+
* <p>Does not hold for {@link #VAR_AND_TYPE_DECL}, {@link #TYPE_ONLY_IMPORT}, or @{link {@link
319+
* #TYPE_ONLY_EXPORT} as these do not occur in type annotations.
319320
*/
320321
public boolean isInsideType() {
321322
return this == TYPE_BIND
@@ -577,7 +578,9 @@ public Label visit(Literal nd, Context c) {
577578
Matcher m = TemplateEngines.TEMPLATE_TAGS.matcher(nd.getRaw());
578579
int offset = nd.getLoc().getStart().getOffset();
579580
while (m.find()) {
580-
Label locationLbl = TemplateEngines.makeLocation(lexicalExtractor.getTextualExtractor(), offset + m.start(), offset + m.end());
581+
Label locationLbl =
582+
TemplateEngines.makeLocation(
583+
lexicalExtractor.getTextualExtractor(), offset + m.start(), offset + m.end());
581584
trapwriter.addTuple("expr_contains_template_tag_location", key, locationLbl);
582585
}
583586
}
@@ -716,7 +719,8 @@ public Label visit(Program nd, Context c) {
716719
+ locationManager.getStartLine()
717720
+ ","
718721
+ locationManager.getStartColumn());
719-
Scope moduleScope = scopeManager.enterScope(ScopeKind.MODULE, moduleScopeKey, toplevelLabel);
722+
Scope moduleScope =
723+
scopeManager.enterScope(ScopeKind.MODULE, moduleScopeKey, toplevelLabel);
720724
if (sourceType.hasNoGlobalScope()) {
721725
scopeManager.setImplicitVariableScope(moduleScope);
722726
}
@@ -1178,9 +1182,7 @@ public Label visit(Property nd, Context c) {
11781182
return propkey;
11791183
}
11801184

1181-
/**
1182-
* Extracts the string value of <code>expr</code> as an HTML snippet.
1183-
*/
1185+
/** Extracts the string value of <code>expr</code> as an HTML snippet. */
11841186
private void extractStringValueAsHtml(Expression expr, Label exprLabel) {
11851187
TextualExtractor textualExtractor = lexicalExtractor.getTextualExtractor();
11861188
if (textualExtractor.isSnippet()) {
@@ -1193,16 +1195,21 @@ private void extractStringValueAsHtml(Expression expr, Label exprLabel) {
11931195
String source = sourceAndOffset.fst();
11941196
SourceLocation loc = expr.getLoc();
11951197
Path originalFile = textualExtractor.getExtractedFile().toPath();
1196-
Path vfile = originalFile.resolveSibling(originalFile.getFileName().toString() + "." + loc.getStart().getLine() + "." + loc.getStart().getColumn() + ".html");
1197-
SourceMap sourceMap = textualExtractor.getSourceMap().offsetBy(loc.getStart().getOffset(), sourceAndOffset.snd());
1198-
TextualExtractor innerTextualExtractor = new TextualExtractor(
1199-
trapwriter,
1200-
locationManager,
1201-
source,
1202-
false,
1203-
getMetrics(),
1204-
vfile.toFile(),
1205-
sourceMap);
1198+
Path vfile =
1199+
originalFile.resolveSibling(
1200+
originalFile.getFileName().toString()
1201+
+ "."
1202+
+ loc.getStart().getLine()
1203+
+ "."
1204+
+ loc.getStart().getColumn()
1205+
+ ".html");
1206+
SourceMap sourceMap =
1207+
textualExtractor
1208+
.getSourceMap()
1209+
.offsetBy(loc.getStart().getOffset(), sourceAndOffset.snd());
1210+
TextualExtractor innerTextualExtractor =
1211+
new TextualExtractor(
1212+
trapwriter, locationManager, source, false, getMetrics(), vfile.toFile(), sourceMap);
12061213
HTMLExtractor html = HTMLExtractor.forEmbeddedHtml(config);
12071214
List<Label> rootNodes = html.extractEx(innerTextualExtractor).fst();
12081215
int rootNodeIndex = 0;
@@ -1212,7 +1219,7 @@ private void extractStringValueAsHtml(Expression expr, Label exprLabel) {
12121219
}
12131220

12141221
private String tryGetIdentifierName(Expression e) {
1215-
return e instanceof Identifier ? ((Identifier)e).getName() : null;
1222+
return e instanceof Identifier ? ((Identifier) e).getName() : null;
12161223
}
12171224

12181225
private Pair<String, OffsetTranslation> tryGetStringValueFromExpression(Expression e) {
@@ -1232,7 +1239,8 @@ private Pair<String, OffsetTranslation> tryGetStringValueFromExpression(Expressi
12321239
return null;
12331240
}
12341241
TemplateElement element = lit.getQuasis().get(0);
1235-
return Pair.make((String) element.getCooked(), makeStringLiteralOffsets("`" + element.getRaw() + "`"));
1242+
return Pair.make(
1243+
(String) element.getCooked(), makeStringLiteralOffsets("`" + element.getRaw() + "`"));
12361244
}
12371245
return null;
12381246
}
@@ -1677,9 +1685,9 @@ public Label visit(ExportNamedDeclaration nd, Context c) {
16771685
visit(nd.getDeclaration(), lbl, -1);
16781686
visit(nd.getSource(), lbl, -2);
16791687
IdContext childContext =
1680-
nd.hasSource() ? IdContext.LABEL :
1681-
nd.hasTypeKeyword() ? IdContext.TYPE_ONLY_EXPORT :
1682-
IdContext.EXPORT;
1688+
nd.hasSource()
1689+
? IdContext.LABEL
1690+
: nd.hasTypeKeyword() ? IdContext.TYPE_ONLY_EXPORT : IdContext.EXPORT;
16831691
visitAll(nd.getSpecifiers(), lbl, childContext, 0);
16841692
if (nd.hasTypeKeyword()) {
16851693
trapwriter.addTuple("has_type_keyword", lbl);
@@ -1699,7 +1707,10 @@ public Label visit(ExportSpecifier nd, Context c) {
16991707
public Label visit(ImportDeclaration nd, Context c) {
17001708
Label lbl = super.visit(nd, c);
17011709
visit(nd.getSource(), lbl, -1);
1702-
IdContext childContext = nd.hasTypeKeyword() ? IdContext.TYPE_ONLY_IMPORT : IdContext.VAR_AND_TYPE_AND_NAMESPACE_DECL;
1710+
IdContext childContext =
1711+
nd.hasTypeKeyword()
1712+
? IdContext.TYPE_ONLY_IMPORT
1713+
: IdContext.VAR_AND_TYPE_AND_NAMESPACE_DECL;
17031714
visitAll(nd.getSpecifiers(), lbl, childContext, 0);
17041715
emitNodeSymbol(nd, lbl);
17051716
if (nd.hasTypeKeyword()) {
@@ -2235,10 +2246,14 @@ public Label visit(AngularPipeRef nd, Context c) {
22352246

22362247
@Override
22372248
public Label visit(GeneratedCodeExpr nd, Context c) {
2238-
Label key = super.visit(nd, c);
2239-
Label templateLbl = TemplateEngines.makeLocation(lexicalExtractor.getTextualExtractor(), nd.getLoc().getStart().getOffset(), nd.getLoc().getEnd().getOffset());
2240-
trapwriter.addTuple("expr_contains_template_tag_location", key, templateLbl);
2241-
return key;
2249+
Label key = super.visit(nd, c);
2250+
Label templateLbl =
2251+
TemplateEngines.makeLocation(
2252+
lexicalExtractor.getTextualExtractor(),
2253+
nd.getLoc().getStart().getOffset(),
2254+
nd.getLoc().getEnd().getOffset());
2255+
trapwriter.addTuple("expr_contains_template_tag_location", key, templateLbl);
2256+
return key;
22422257
}
22432258
}
22442259

javascript/extractor/src/com/semmle/js/extractor/HTMLExtractor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,7 @@ public void handleElement(Element elt, HtmlPopulator.Context context) {
153153
@Override
154154
public void handleText(
155155
Source src, int textBegin, int textEnd, Label parentLabel, boolean isCData) {
156-
extractTemplateTags(
157-
textualExtractor, src, textBegin, textEnd, () -> parentLabel);
156+
extractTemplateTags(textualExtractor, src, textBegin, textEnd, () -> parentLabel);
158157
}
159158

160159
@Override
@@ -392,7 +391,8 @@ private void extractTemplateTags(
392391
if (isEmbedded) return; // Do not extract template tags for HTML snippets embedded in a JS file
393392

394393
TrapWriter trapwriter = textualExtractor.getTrapwriter();
395-
Matcher m = TemplateEngines.TEMPLATE_TAGS.matcher(textualExtractor.getSource()).region(start, end);
394+
Matcher m =
395+
TemplateEngines.TEMPLATE_TAGS.matcher(textualExtractor.getSource()).region(start, end);
396396
while (m.find()) {
397397
int startOffset = m.start();
398398
int endOffset = m.end();

0 commit comments

Comments
 (0)