Skip to content

Commit ff2749c

Browse files
author
John Messerly
committed
small refactor: use JS.* prefix for our other JS extensions
it's not clear what will end up in js_ast vs ddc, so nice to use js_ast conventions for the JS nodes [email protected] Review URL: https://codereview.chromium.org/1111893002
1 parent 83a40f4 commit ff2749c

File tree

5 files changed

+41
-40
lines changed

5 files changed

+41
-40
lines changed

pkg/dev_compiler/lib/src/codegen/js_codegen.dart

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ import 'package:dev_compiler/src/utils.dart';
3131

3232
import 'code_generator.dart';
3333
import 'js_field_storage.dart';
34-
import 'js_names.dart' show JSTemporary, invalidJSStaticMethodName;
35-
import 'js_metalet.dart';
34+
import 'js_names.dart' as JS;
35+
import 'js_metalet.dart' as JS;
3636
import 'js_printer.dart' show writeJsLibrary;
3737
import 'side_effect_analysis.dart';
3838

@@ -71,17 +71,17 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor {
7171
final _exports = new Set<String>();
7272
final _lazyFields = <VariableDeclaration>[];
7373
final _properties = <FunctionDeclaration>[];
74-
final _privateNames = new HashMap<String, JSTemporary>();
74+
final _privateNames = new HashMap<String, JS.TemporaryId>();
7575
final _extensionMethodNames = new HashSet<String>();
7676
final _pendingStatements = <JS.Statement>[];
77-
final _temps = new HashMap<Element, JSTemporary>();
77+
final _temps = new HashMap<Element, JS.TemporaryId>();
7878

7979
/// The name for the library's exports inside itself.
8080
/// This much be a constant because we interpolate it into template strings,
8181
/// and otherwise it would break caching for them.
8282
/// `exports` was chosen as the most similar to ES module patterns.
83-
final JSTemporary _exportsVar = new JSTemporary('exports');
84-
final JSTemporary _namedArgTemp = new JSTemporary('opts');
83+
final _exportsVar = new JS.TemporaryId('exports');
84+
final _namedArgTemp = new JS.TemporaryId('opts');
8585

8686
/// Classes we have not emitted yet. Values can be [ClassDeclaration] or
8787
/// [ClassTypeAlias].
@@ -1097,8 +1097,8 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor {
10971097
return new JS.Identifier(name);
10981098
}
10991099

1100-
JSTemporary _getTemp(Object key, String name) =>
1101-
_temps.putIfAbsent(key, () => new JSTemporary(name));
1100+
JS.TemporaryId _getTemp(Object key, String name) =>
1101+
_temps.putIfAbsent(key, () => new JS.TemporaryId(name));
11021102

11031103
JS.ArrayInitializer _emitTypeNames(List<DartType> types) {
11041104
return new JS.ArrayInitializer(types.map(_emitTypeName).toList());
@@ -1197,7 +1197,7 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor {
11971197
context: node);
11981198
}
11991199

1200-
JSMetaLet _emitOpAssign(
1200+
JS.MetaLet _emitOpAssign(
12011201
Expression left, Expression right, String op, ExecutableElement element,
12021202
{Expression context}) {
12031203
// Desugar `x += y` as `x = x + y`, ensuring that if `x` has subexpressions
@@ -1207,7 +1207,7 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor {
12071207
var inc = AstBuilder.binaryExpression(lhs, op, right);
12081208
inc.staticElement = element;
12091209
inc.staticType = getStaticType(left);
1210-
return new JSMetaLet(vars, [_emitSet(lhs, inc)]);
1210+
return new JS.MetaLet(vars, [_emitSet(lhs, inc)]);
12111211
}
12121212

12131213
JS.Expression _emitSet(Expression lhs, Expression rhs) {
@@ -1778,7 +1778,7 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor {
17781778
/// // psuedocode mix of Scheme and JS:
17791779
/// (let* (x1=expr1, x2=expr2, t=expr1[expr2]) { x1[x2] = t + 1; t })
17801780
///
1781-
/// The [JSMetaLet] nodes automatically simplify themselves if they can.
1781+
/// The [JS.JS.MetaLet] nodes automatically simplify themselves if they can.
17821782
/// For example, if the result value is not used, then `t` goes away.
17831783
@override
17841784
JS.Expression visitPostfixExpression(PostfixExpression node) {
@@ -1809,7 +1809,7 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor {
18091809
..staticType = getStaticType(expr);
18101810

18111811
var body = [_emitSet(left, increment), _visit(x)];
1812-
return new JSMetaLet(vars, body, statelessResult: true);
1812+
return new JS.MetaLet(vars, body, statelessResult: true);
18131813
}
18141814

18151815
@override
@@ -1827,7 +1827,7 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor {
18271827
var vars = {};
18281828
var x = _bindLeftHandSide(vars, expr, context: expr);
18291829
var body = js.call('# = # $mathop 1', [_visit(x), notNull(x)]);
1830-
return new JSMetaLet(vars, [body]);
1830+
return new JS.MetaLet(vars, [body]);
18311831
} else {
18321832
return js.call('$op#', notNull(expr));
18331833
}
@@ -1856,7 +1856,7 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor {
18561856
_cascadeTarget = _bindValue(vars, '_', node.target, context: node);
18571857
var sections = _visitList(node.cascadeSections);
18581858
sections.add(_visit(_cascadeTarget));
1859-
var result = new JSMetaLet(vars, sections, statelessResult: true);
1859+
var result = new JS.MetaLet(vars, sections, statelessResult: true);
18601860
_cascadeTarget = savedCascadeTemp;
18611861
return result;
18621862
}
@@ -2360,7 +2360,7 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor {
23602360
{DartType type, bool unary: false, bool isStatic: false}) {
23612361
if (name.startsWith('_')) {
23622362
return _privateNames.putIfAbsent(
2363-
name, () => _initSymbol(new JSTemporary(name)));
2363+
name, () => _initSymbol(new JS.TemporaryId(name)));
23642364
}
23652365

23662366
// Check for extension method:
@@ -2374,7 +2374,7 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor {
23742374
name = 'unary-';
23752375
}
23762376

2377-
if (isStatic && invalidJSStaticMethodName(name)) {
2377+
if (isStatic && JS.invalidStaticMethodName(name)) {
23782378
// Choose an string name. Use an invalid identifier so it won't conflict
23792379
// with any valid member names.
23802380
// TODO(jmesserly): this works around the problem, but I'm pretty sure we

pkg/dev_compiler/lib/src/codegen/js_metalet.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ library dev_compiler.src.codegen.js_metalet;
88
import 'package:dev_compiler/src/js/js_ast.dart';
99
import 'package:dev_compiler/src/js/precedence.dart';
1010

11-
import 'js_names.dart' show JSTemporary;
11+
import 'js_names.dart' show TemporaryId;
1212

1313
/// A synthetic `let*` node, similar to that found in Scheme.
1414
///
@@ -17,7 +17,7 @@ import 'js_names.dart' show JSTemporary;
1717
/// // psuedocode mix of Scheme and JS:
1818
/// (let* (x1=expr1, x2=expr2, t=x1[x2]) { x1[x2] = t + 1; t })
1919
///
20-
/// [JSMetaLet] will simplify itself automatically when [toExpression],
20+
/// [MetaLet] will simplify itself automatically when [toExpression],
2121
/// [toStatement], or [toReturn] is called.
2222
///
2323
/// * variables used once will be inlined.
@@ -29,7 +29,7 @@ import 'js_names.dart' show JSTemporary;
2929
/// around statelessness (such as `final` variables). [variables] should not
3030
/// be created for these Dart expressions.
3131
///
32-
class JSMetaLet extends Expression {
32+
class MetaLet extends Expression {
3333
/// Creates a temporary to contain the value of [expr]. The temporary can be
3434
/// used multiple times in the resulting expression. For example:
3535
/// `expr ** 2` could be compiled as `expr * expr`. The temporary scope will
@@ -52,7 +52,7 @@ class JSMetaLet extends Expression {
5252
/// This happens multiple times, so ensure the expression form is cached.
5353
Expression _expression;
5454

55-
JSMetaLet(this.variables, this.body, {this.statelessResult: false});
55+
MetaLet(this.variables, this.body, {this.statelessResult: false});
5656

5757
/// Returns an expression that ignores the result. This is a cross between
5858
/// [toExpression] and [toStatement]. Used for C-style for-loop updaters,
@@ -74,7 +74,7 @@ class JSMetaLet extends Expression {
7474

7575
var exprs = body.toList();
7676
exprs.add(exprs.removeLast().toAssignExpression(left));
77-
return new JSMetaLet(variables, exprs);
77+
return new MetaLet(variables, exprs);
7878
}
7979
return super.toAssignExpression(left);
8080
}
@@ -136,7 +136,7 @@ class JSMetaLet extends Expression {
136136
[new VariableDeclarationList('let', vars).toStatement(), block]);
137137
}
138138

139-
Node _build(List<JSTemporary> params, List<Expression> values, Node node) {
139+
Node _build(List<TemporaryId> params, List<Expression> values, Node node) {
140140
// Visit the tree and count how many times each temp was used.
141141
var counter = new _VariableUseCounter();
142142
node.accept(counter);
@@ -154,7 +154,7 @@ class JSMetaLet extends Expression {
154154
if (n == null || n < 2) {
155155
substitutions[name] = _substitute(init);
156156
} else {
157-
params.add(substitutions[name] = new JSTemporary(name));
157+
params.add(substitutions[name] = new TemporaryId(name));
158158
values.add(init);
159159
}
160160
});
@@ -178,7 +178,7 @@ class JSMetaLet extends Expression {
178178
///
179179
/// ((_) => _.addAll(result), _.add(2), result = _)([])
180180
///
181-
JSMetaLet _simplifyAssignment(Identifier left, {bool isDeclaration: false}) {
181+
MetaLet _simplifyAssignment(Identifier left, {bool isDeclaration: false}) {
182182
// See if the result value is a let* temporary variable.
183183
if (body.last is! InterpolatedExpression) return null;
184184

@@ -204,7 +204,7 @@ class JSMetaLet extends Expression {
204204
if (isDeclaration) {
205205
// Technically, putting one of these in a comma expression is not
206206
// legal. However when isDeclaration is true, toStatement will be
207-
// called immediately on the JSMetaLet, which results in legal JS.
207+
// called immediately on the MetaLet, which results in legal JS.
208208
assign = new VariableDeclarationList(
209209
'let', [new VariableInitialization(left, value)]);
210210
} else {
@@ -213,7 +213,7 @@ class JSMetaLet extends Expression {
213213

214214
var newBody = new Expression.binary([assign]..addAll(body), ',');
215215
Binary comma = new Template(null, newBody).safeCreate({name: left});
216-
return new JSMetaLet(vars, comma.commaToExpressionList(),
216+
return new MetaLet(vars, comma.commaToExpressionList(),
217217
statelessResult: statelessResult);
218218
}
219219
}

pkg/dev_compiler/lib/src/codegen/js_names.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import 'package:dev_compiler/src/js/js_ast.dart';
1313
/// generation without needing global knowledge. See [JSNamer].
1414
///
1515
// TODO(jmesserly): move into js_ast? add a boolean to Identifier?
16-
class JSTemporary extends Identifier {
17-
JSTemporary(String name) : super(name);
16+
class TemporaryId extends Identifier {
17+
TemporaryId(String name) : super(name);
1818
}
1919

2020
/// This class has two purposes:
@@ -28,10 +28,10 @@ class JSTemporary extends Identifier {
2828
/// [Identifiers] are never renamed unless they are an invalid identifier, like
2929
/// `function` or `instanceof`, and their `name` field controls whether they
3030
/// refer to the same variable.
31-
class JSNamer extends LocalNamer {
31+
class TemporaryNamer extends LocalNamer {
3232
final Map<Object, String> renames;
3333

34-
JSNamer(Node node) : renames = new _RenameVisitor.build(node).renames;
34+
TemporaryNamer(Node node) : renames = new _RenameVisitor.build(node).renames;
3535

3636
String getName(Identifier node) {
3737
var rename = renames[identifierKey(node)];
@@ -152,9 +152,9 @@ class _RenameVisitor extends VariableDeclarationVisitor {
152152
static String _findName(Object id, Set<String> usedNames) {
153153
String name;
154154
bool valid;
155-
if (id is JSTemporary) {
155+
if (id is TemporaryId) {
156156
name = id.name;
157-
valid = !invalidJSVariableName(name);
157+
valid = !invalidVariableName(name);
158158
} else {
159159
name = id;
160160
valid = false;
@@ -179,14 +179,14 @@ class _RenameVisitor extends VariableDeclarationVisitor {
179179
}
180180

181181
bool needsRename(Identifier node) =>
182-
node is JSTemporary || node.allowRename && invalidJSVariableName(node.name);
182+
node is TemporaryId || node.allowRename && invalidVariableName(node.name);
183183

184184
Object /*String|JSTemporary*/ identifierKey(Identifier node) =>
185-
node is JSTemporary ? node : node.name;
185+
node is TemporaryId ? node : node.name;
186186

187187
/// Returns true for invalid JS variable names, such as keywords.
188188
/// Also handles invalid variable names in strict mode, like "arguments".
189-
bool invalidJSVariableName(String keyword, {bool strictMode: true}) {
189+
bool invalidVariableName(String keyword, {bool strictMode: true}) {
190190
switch (keyword) {
191191
case "break":
192192
case "case":
@@ -233,7 +233,7 @@ bool invalidJSVariableName(String keyword, {bool strictMode: true}) {
233233

234234
/// Returns true for invalid static method names in strict mode.
235235
/// In particular, "caller" "callee" and "arguments" cannot be used.
236-
bool invalidJSStaticMethodName(String name) {
236+
bool invalidStaticMethodName(String name) {
237237
switch (name) {
238238
case "arguments":
239239
case "caller":

pkg/dev_compiler/lib/src/codegen/js_printer.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import 'package:dev_compiler/src/js/js_ast.dart' as JS;
1515
import 'package:dev_compiler/src/utils.dart'
1616
show computeHash, locationForOffset;
1717

18-
import 'js_names.dart' show JSNamer;
18+
import 'js_names.dart' show TemporaryNamer;
1919

2020
String writeJsLibrary(JS.Program jsTree, String outputPath,
2121
{bool emitSourceMaps: false}) {
@@ -40,7 +40,8 @@ String writeJsLibrary(JS.Program jsTree, String outputPath,
4040

4141
void writeNodeToContext(JS.Node node, JS.JavaScriptPrintingContext context) {
4242
var opts = new JS.JavaScriptPrintingOptions(allowKeywordsInProperties: true);
43-
node.accept(new JS.Printer(opts, context, localNamer: new JSNamer(node)));
43+
node.accept(
44+
new JS.Printer(opts, context, localNamer: new TemporaryNamer(node)));
4445
}
4546

4647
String jsNodeToString(JS.Node node) {

pkg/dev_compiler/lib/src/utils.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import 'package:crypto/crypto.dart' show CryptoUtils, MD5;
3131
import 'package:source_span/source_span.dart';
3232
import 'package:yaml/yaml.dart';
3333

34-
import 'codegen/js_names.dart' show invalidJSVariableName;
34+
import 'codegen/js_names.dart' show invalidVariableName;
3535

3636
bool isDartPrivateLibrary(LibraryElement library) {
3737
var uri = library.source.uri;
@@ -69,7 +69,7 @@ String _toIdentifier(String name) {
6969
var result = buffer != null ? '$buffer' : name;
7070
// Ensure the identifier first character is not numeric and that the whole
7171
// identifier is not a keyword.
72-
if (result.startsWith(new RegExp('[0-9]')) || invalidJSVariableName(result)) {
72+
if (result.startsWith(new RegExp('[0-9]')) || invalidVariableName(result)) {
7373
return '\$$result';
7474
}
7575
return result;

0 commit comments

Comments
 (0)