Share
Embed
JavaScript Transformation - React Europe 2015
sebmck
July 06, 2015
Transcript
71 slides
-
{ type: "Program", body: [{ type: "VariableDeclaration" kind: "var", declarations:
[{ type: "VariableDeclarator", id: { type: "Identifier", name: "foo" }, init: { type: “FunctionExpression", id: { type: “Identifier”, name: “foo” }, params: [], body: [{ type: "BlockStatement", body: [{ type: "ReturnStatement", argument: { type: "Identifier", name: "bar" } }] }] } }] }] } Data structure -
Identifier Graph Variable Declaration Program Variable Declarator Identifier Function Expression
Block Statement Return Statement Identifier -
List • Program • VariableDeclaration • VariableDeclarator • Identifier •
FunctionExpression • Identifier • BlockStatement • ReturnStatement • Identifier -
Parser Turns code into an AST Transformer Manipulates AST Generator
Turns AST back into code Parser Turns code into an AST Transformer and Generator Traverses AST and manipulates it via strings jstransform Parser Turns code into an AST -
Lexical Analysis Turn code into a stream of tokens Syntactic
Analysis Turn tokens into a tree Parser Turns code into an AST -
foo = 1 + “bar”; What is a token? Identifier
Punctuator Number String 1. Identifier<foo> 2. Punctuator<=> 3. Number<1> 4. Punctuator<+> 5. String<bar> 6. Puncutator<;> -
class Foo extends Bar {} pp.parseClassDeclaration = function () {
let node = this.startNode(); this.expect(tt._class); node.id = this.parseIdentifier(); if (this.type === tt._extends) { this.next(); node.superClass = this.parseExprSubscripts(); }; node.body = this.parseClassBody(); return this.finishNode(node, "ClassDeclaration"); }; Identifier Punctuator Keyword 1. Keyword<class> 2. Identifier<Foo> 3. Keyword<extends> 4. Identifier<Bar> 5. Punctuator<{> 6. Punctuator<}> Syntactic analysis -
pp.parseClassDeclaration = function () { let node = this.startNode(); this.expect(tt._class);
node.id = this.parseIdentifier(); if (this.type === tt._extends) { this.next(); node.superClass = this.parseExprSubscripts(); }; node.body = this.parseClassBody(); return this.finishNode(node, "ClassDeclaration"); }; 1. Keyword<class> 2. Identifier<Foo> 3. Keyword<extends> 4. Identifier<Bar> 5. Punctuator<{> 6. Punctuator<}> Syntactic analysis -
pp.parseClassDeclaration = function () { let node = this.startNode(); this.expect(tt._class);
node.id = this.parseIdentifier(); if (this.type === tt._extends) { this.next(); node.superClass = this.parseExprSubscripts(); }; node.body = this.parseClassBody(); return this.finishNode(node, "ClassDeclaration"); }; 1. Keyword<class> 2. Identifier<Foo> 3. Keyword<extends> 4. Identifier<Bar> 5. Punctuator<{> 6. Punctuator<}> Syntactic analysis -
pp.parseClassDeclaration = function () { let node = this.startNode(); this.expect(tt._class);
node.id = this.parseIdentifier(); if (this.type === tt._extends) { this.next(); node.superClass = this.parseExprSubscripts(); }; node.body = this.parseClassBody(); return this.finishNode(node, "ClassDeclaration"); }; 1. Keyword<class> 2. Identifier<Foo> 3. Keyword<extends> 4. Identifier<Bar> 5. Punctuator<{> 6. Punctuator<}> Syntactic analysis -
pp.parseClassDeclaration = function () { let node = this.startNode(); this.expect(tt._class);
node.id = this.parseIdentifier(); if (this.type === tt._extends) { this.next(); node.superClass = this.parseExprSubscripts(); }; node.body = this.parseClassBody(); return this.finishNode(node, "ClassDeclaration"); }; 1. Keyword<class> 2. Identifier<Foo> 3. Keyword<extends> 4. Identifier<Bar> 5. Punctuator<{> 6. Punctuator<}> Syntactic analysis -
pp.parseClassDeclaration = function () { let node = this.startNode(); this.expect(tt._class);
node.id = this.parseIdentifier(); if (this.type === tt._extends) { this.next(); node.superClass = this.parseExprSubscripts(); }; node.body = this.parseClassBody(); return this.finishNode(node, "ClassDeclaration"); }; 1. Keyword<class> 2. Identifier<Foo> 3. Keyword<extends> 4. Identifier<Bar> 5. Punctuator<{> 6. Punctuator<}> Syntactic analysis -
pp.parseClassDeclaration = function () { let node = this.startNode(); this.expect(tt._class);
node.id = this.parseIdentifier(); if (this.type === tt._extends) { this.next(); node.superClass = this.parseExprSubscripts(); }; node.body = this.parseClassBody(); return this.finishNode(node, "ClassDeclaration"); }; 1. Keyword<class> 2. Identifier<Foo> 3. Keyword<extends> 4. Identifier<Bar> 5. Punctuator<{> 6. Punctuator<}> Syntactic analysis -
pp.parseClassDeclaration = function () { let node = this.startNode(); this.expect(tt._class);
node.id = this.parseIdentifier(); if (this.type === tt._extends) { this.next(); node.superClass = this.parseExprSubscripts(); }; node.body = this.parseClassBody(); return this.finishNode(node, "ClassDeclaration"); }; 1. Keyword<class> 2. Identifier<Foo> 3. Keyword<extends> 4. Identifier<Bar> 5. Punctuator<{> 6. Punctuator<}> Syntactic analysis -
pp.parseClassDeclaration = function () { let node = this.startNode(); this.expect(tt._class);
node.id = this.parseIdentifier(); if (this.type === tt._extends) { this.next(); node.superClass = this.parseExprSubscripts(); }; node.body = this.parseClassBody(); return this.finishNode(node, "ClassDeclaration"); }; 1. Keyword<class> 2. Identifier<Foo> 3. Keyword<extends> 4. Identifier<Bar> 5. Punctuator<{> 6. Punctuator<}> Syntactic analysis -
{ type: "ClassDeclaration", start: 0, end: 23, id: { type:
“Identifier", start: 6, end: 9, name: “Foo" }, superClass: { type: “Identifier", end: 21, start: 18, name: “Bar" }, body: { type: “ClassBody", start: 21, end: 23, body: [] } } Syntactic analysis -
Transformer Manipulates AST Traversal Visit the tree Static Analysis Infer
what nodes represent -
Visitor • Controls traversal state • Interface for visiting, replacing
and removing nodes • Consolidates visiting logic -
Traversal Visitor Variable Declaration Program Variable Declarator Identifier Function Expression
Block Statement Return Statement Identifier -
var foo = bar + “”; Type inference Variable Declaration
Program Variable Declarator Identifier Binary Expression Literal Identifier -
Transpilation var [x, y] = [1, 2]; var _ref =
[1, 2]; var x = _ref[0]; var y = _ref[1]; -
Transpilation var [x, y] = [1, 2]; var x =
1; var y = 2; -
Identifier Function Expression Block Statement Return Statement Identifier Unidirectional tree
Variable Declaration Program Variable Declarator Identifier Identifier -
Path Path Path Path Variable Declaration Program Variable Declarator Function
Expression Identifier -
Identifier Variable Declaration Program Variable Declarator Identifier Function Expression Block
Statement Return Statement Identifier argument body[0] Root declarations [0] id init body body[0] id Bidirectional tree -
Path • Abstraction for dealing with node relationships • Constant,
same-instance always • Reactive to tree changes - propagates updates -
Style Inference Retain input style in output Code Generation Turn
the tree into code Generator Turns AST back into code -
Transformer Manipulates AST Generator Turns AST back into code Parser
Turns code into an AST Lexical Analysis Turn code into tokens Syntactic Analysis Turn tokens into a tree Traversal Visit the tree Static Analysis Infer what nodes represent Code Generation Turn the tree into code Style Inference Retain input style in output -
CONSTANT ELEMENTS A React element is conceptually equivalent if all
it’s values are the same. -
Constant value types function render() { return <div className=“header” />;
} render() === render(); -
var _ref = React.createElement("div", { className: “foo" }); function render()
{ return _ref; } React 0.14 Constant hoisting function render() { return React.createElement("div", { className: “foo" }); } React <= 0.13 -
Inline elements ({ type: "div", props: { className: "foo", children:
[ bar, { type: Baz, props: {}, key: "baz", ref: null } ] }, key: null, ref: null }) -
class Counter extends React.Component { static propTypes = { initialCount:
React.PropTypes.number }; static defaultProps = { initialCount: 0 }; state = { count: this.props.initialCount }; } Class properties -
function autobind(target, key, descriptor) { return { get: function ()
{ return descriptor.value.bind(this); } }; } class Person { @autobind getFullName() { return `${this.firstName} ${this.lastName}`; } } Decorators -
class Counter extends React.Component { tick() { this.setState({ count: this.state.count
+ 1 }); } render() { return <div onClick={::this.tick}> Clicks: {this.state.count} </div>; } } Bind syntax -
THE FUTURE • Dead code elimination/minification • Constant folding/static evaluation
• Static analysis/linting