Skip to content

Commit

Permalink
Move safe transforms to a separate AST visitor pass
Browse files Browse the repository at this point in the history
  • Loading branch information
wcjohnson committed Sep 24, 2017
1 parent 832cf9d commit cd7cdb0
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 24 deletions.
19 changes: 6 additions & 13 deletions src/index.lsc
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ import { getShadowingIdentifiers, checkFalsePositiveReference } from "./variable
import { ensureConstructor, bindMethodsInConstructor, bindMethods } from "./classes";
import * as matching from "./match";
import * as comprehension from "./comprehension";
import { transformExistentialExpression, maybeTransformSafe } from "./safe";
import { transformExistentialExpression } from "./safe";
import { replaceWithInlinedOperator } from "./inlinedOperators";
import { transformForInArrayStatement, transformForInObjectStatement, lintForInArrayStatement, lintForInObjectStatement } from "./for";
import { resetHelpers } from "./helpers";
import { markIdentifier } from "./stdlib";
import { locatePluginOpts, getParserOpts, parseConfigurationDirectives } from './config'
import { getFileTypeInfo, createCompilerState, initializeCompilerState, postprocess } from './compilerState'
import { fixAst } from './fixAst'
import { transformPlaceholders } from './placeholders'
import { fixAst } from './passes/fixAst'
import { transformPlaceholders } from './passes/transformPlaceholders'
import { transformSafeExprs } from './passes/transformSafeExprs'
import { transformPipeOperator } from './pipe'

Lightscript(babel) ->
Expand Down Expand Up @@ -51,10 +52,11 @@ Lightscript(babel) ->
//// AST transforms
// Perform basic ast fixups (block bodies, etc)
fixAst(path)

// Transform placeholder expressions first.
if compilerState.opts.placeholderArgs:
transformPlaceholders(path)
// Transform safe exprs
transformSafeExprs(path)

// Main LSC transforms
path.traverse({
Expand Down Expand Up @@ -90,9 +92,6 @@ Lightscript(babel) ->
comprehension.transformPlainObjectComprehension(path)

CallExpression: {
enter(path): void ->
maybeTransformSafe(path)

exit(path): void ->
{ node } = path
if replaceWithInlinedOperator(path, node.callee, node.arguments): return
Expand Down Expand Up @@ -223,12 +222,6 @@ Lightscript(babel) ->
awaitExpr = t.awaitExpression(awaitIife)~allAtLoc(getLoc(path.node))
path.replaceWith(awaitExpr)

MemberExpression(path): void ->
path~maybeTransformSafe()

NewExpression(path): void ->
path~maybeTransformSafe()

AwaitExpression(path): void ->
if (path.get("argument").isArrayExpression() || path.node.argument.type === "ArrayComprehension") {
const promiseDotAllCall = t.callExpression(
Expand Down
8 changes: 4 additions & 4 deletions src/fixAst.lsc → src/passes/fixAst.lsc
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Perform non-semantic AST transformations like ensuring function bodies
// are blocks, etc.
import { isNamedArrowFunction } from './functions'
import { toBlockStatement } from './blocks'
import { isa } from './is'
import { ensureBlockArrowFunctionExpression } from './functions'
import { isNamedArrowFunction } from '../functions'
import { toBlockStatement } from '../blocks'
import { isa } from '../is'
import { ensureBlockArrowFunctionExpression } from '../functions'

export fixAst(programPath) ->
programPath.traverse({
Expand Down
3 changes: 3 additions & 0 deletions src/passes/transformPlaceholders.lsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { transformPlaceholders } from '../placeholders'

export { transformPlaceholders }
7 changes: 7 additions & 0 deletions src/passes/transformSafeExprs.lsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { maybeTransformSafe } from '../safe'

export transformSafeExprs(programPath) ->
programPath.traverse({
CallExpression(path): void -> path~maybeTransformSafe()
MemberExpression(path): void -> path~maybeTransformSafe()
})
4 changes: 2 additions & 2 deletions src/safe.lsc
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import t from './types'
import atNode from 'ast-loc-utils/lib/placeAtNode'
import { hoistRef, isSimple } from './ref'
import is, { isa } from './is'
import { ensureBlockArrowFunctionExpression } from './functions'
import { isa } from './is'

import { getLoc, placeTreeAtLocWhenUnplaced as allAtLoc } from 'ast-loc-utils'

Expand All @@ -15,6 +14,7 @@ findReplacementPath(path) ->
if key == "left" and parentPath~isa("AssignmentExpression"): false
elif key == "object" and parentPath~isa("MemberExpression"): false
elif key == "callee" and (parentPath~isa("CallExpression") or parentPath~isa("NewExpression")): false
elif key == 0 and path.listKey == "arguments" and path.parent.tilde: false // lift past tilde calls
elif key == "argument" and parentPath~isa("UpdateExpression"): false
elif key == "argument" and (parentPath~isa("UnaryExpression") and parentPath.node.operator == "delete"): false
else: true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
var _obj;

(typeof (_obj = a(b, c != null, d)).e !== "function" ? void 0 : _obj.e(f, g != null)) ? h : typeof k.l !== "function" ? void 0 : k.l(typeof i !== "function" ? void 0 : i(j), m != null);
(typeof (_obj = a(b, c != null, d)).e !== "function" ? void 0 : _obj.e(f, g != null)) ? h : typeof k.l !== "function" ? void 0 : typeof i !== "function" ? void 0 : k.l(i(j), m != null);
8 changes: 4 additions & 4 deletions test/fixtures/safe-member-expression/calls-after/expected.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
var _ref, _ref2, _ref3;
var _a$b, _a$b$c, _c;

a == null ? void 0 : a.b();

(_ref = a == null ? void 0 : a.b()) == null ? void 0 : _ref.c().d.e();
a == null ? void 0 : (_a$b = a.b()) == null ? void 0 : _a$b.c().d.e();

(_ref2 = a == null ? void 0 : a.b().c) == null ? void 0 : _ref2.d().e.f();
a == null ? void 0 : (_a$b$c = a.b().c) == null ? void 0 : _a$b$c.d().e.f();

a == null ? void 0 : c(a.b);

(_ref3 = a == null ? void 0 : c(a.b)) == null ? void 0 : e(_ref3.d);
(_c = a == null ? void 0 : c(a.b)) == null ? void 0 : e(_c.d);
1 change: 1 addition & 0 deletions test/fixtures/safe-member-expression/tilde-call/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a?.b~c()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a == null ? void 0 : c(a.b);

0 comments on commit cd7cdb0

Please sign in to comment.