SlideShare a Scribd company logo
Lint, Coverage, Doc,
Autocompletion, Transpilation,
Minification... powered by AST
Alexandre Morgaut
NantesJS Meetup #39 - April 2019
● Ealy JavaScript Adopter (1996)
● Former Wakanda Server-Side JS platform Team (2008)
● JS & Web Standards Evangelist since 2009
● Former member of CommonJS (2010)
● Former W3C Advisory Committee member (2012)
● Initiated NantesJS (2012)
● Joined the Wiztivi company (2015)
Presentation
@amorgaut wiztivi.comAMorgaut
Summary
● Compilers
● BinAST
● Editors
● JavaScript AST
● Using AST in Plugins
● Task Managers
● AST Explorer
Lint, coverage, doc, autocompletion, transpilation, minification... powered by ast
Compilers
http://www.cs.man.ac.uk/~pjj/farrell/comp3.html
Anatomy of a Compiler
http://www-igm.univ-mlv.fr/~forax/ens/java-avance/cours/pdf/old/Generation%20de%20code%201.pdf
Some Token Types
Name Examples
identifiers x, color, UP​
keywords if, while, return​
punctuation }, (, ;
separators +, <, =​
literals true, 6.02e23, "music"​
comments // en ligne, /* en bloc */​
BinAST
Binary AST
● Give the JS engine a pre-tokenized, pre-parsed, script content
● Give it in binary format to make it lighter
● Current advanced prototypes already show JS parsing improvements of 30%-50%
on all the most common frameworks
https://github.com/tc39/proposal-binary-ast
https://github.com/binast/binjs-ref
https://yoric.github.io/Fosdem-2019/
Editors
Editor Features
● With Tokenizer Help
○ Syntax highlighting
○ Collapse / Expand
● With AST Help
○ Lint
○ Find Usages
○ Code Completion
○ Reformat
○ Statement level breakpoints
ACE - The Cloud9 Editor
// ### Abstract Syntax Tree
//
// The default AST structure is inspired by the Mozilla Parser API but can
// easily be customized by overriding these functions.
// Lexer
// -----
//
// The lexer, or the tokenizer reads the input string character by character
// and derives a token left-right. To be as efficient as possible the lexer
// prioritizes the common cases such as identifiers. It also works with
// character codes instead of characters as string comparisons was the
// biggest bottleneck of the parser.
https://github.com/ajaxorg/ace/blob/9b5b63d1dc7c1b81b58d30c87d14b5905d030ca5/lib/ace/mode/lua/luaparse.js
ternjs
code-analysis engine for JavaScript
● Autocompletion on variables and properties
● Function argument hints
● Querying the type of an expression
● Finding the definition of something
● Automatic refactoring
Built-in in the Bracket Editor (Adobe)
https://ternjs.net/
JavaScript AST
SpiderMonkey engine's JavaScript parser API
https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/Parser_API
Parsers
● espree
○ ESLint, estraverse, hint, javascript-obfuscator, JSDoc, protobuf.js,
v8/web-tooling-benchmark
● babylon / babel-parser
○ babel, ESDoc
● esprima
○ previous ESLint, flow, istanbul
● acorn
○ espree, buble, browserify, ternjs, webpack, optimize-js
Espree
● Fork of Esprima (v1.2.2)
○ Primary goal is to produce the exact same AST structure and tokens as Esprima
○ The AST structure being the ESTree API with JSX extensions
● Built on top of Acorn
○ use its modular architecture -> extensions of core functionalities
● Maintained by the ESLint project
https://github.com/eslint/espree
const ast = espree.parse(code);
ESTree
"This site will serve as a community standard"
"keep up with the evolution of the JavaScript language"
https://github.com/estree/estree
AST Example
Using AST in Plugins
ESLint Rule
Enforce a maximum number of parameters in function definitions
https://eslint.org/docs/rules/max-params
https://github.com/eslint/eslint/blob/master/lib/rules/max-params.js
Rule Option Management
Rule Validation Code
AST Node Type Binding
Babel Transform Plugin
babel-plugin-transform-class
A minimalist ES6 class babel transformer
● Remove ES6 forbidden actions controls
○ ex: throw on this modification before calling super()
○ Encourage to check that at edit time using ESLint rules
● Remove Polyfill generations
○ Generate Full ES5 compatible code
○ Even compatible with ES3 using very minimalist Polyfills
https://github.com/AMorgaut/babel-plugin-transform-class
Return a visitor object
● keys:
AST Node types
● values:
callback
or map of callbacks
○ enter:
beginning of the node block
○ exit:
end of the node block
babel.types
● provides useful helpers
https://github.com/AMorgaut/babel-plugin-transform-class/blob/master/src/index.js
Task Managers
Task Manager
Grunt, Gulp, npm, ...
webpack
● $ npm run build
○ lint > transpile* > concat* > minify*
○ * with SourceMap
○ each step using it's own AST
● Some of those tools (ex: babel) can use an existing AST
AST Explorer
https://astexplorer.net
https://astexplorer.net/#/gist/0178b41edea28820b2452e3422059cbd/latest
Input
Source
Transform
Code
Input
AST
Output
Source
AST Explorer
1. Select (or use default) a:
a. Source Type (JavaScript, CSS, HTML, …)
b. Parser Lib (babel, eslint, …)
c. (Optional) Transformer Lib
2. Provide a Source Code
a. A Browsable matching AST is automatically generated
3. Provide a Transform Code
a. An Output Source is automatically updated
b. Error messages code be shown instead to help fixing the code
Questions ?
Thank you !

More Related Content

PDF
Ruxmon.2013-08.-.CodeBro!
PPTX
Introducing type script
PPTX
Groovy / comparison with java
PPTX
Getting started with typescript
PDF
TypeScript Best Practices
PDF
Typescript for the programmers who like javascript
PDF
TypeScript introduction to scalable javascript application
PDF
TypeScript 2 in action
Ruxmon.2013-08.-.CodeBro!
Introducing type script
Groovy / comparison with java
Getting started with typescript
TypeScript Best Practices
Typescript for the programmers who like javascript
TypeScript introduction to scalable javascript application
TypeScript 2 in action

What's hot (20)

PDF
TypeScript: coding JavaScript without the pain
PDF
Development: What they don't teach you in college
PDF
Frameworks
ODP
Introduction to Go for Java Developers
PDF
So R Debugging
PPTX
TypeScript Modules
PDF
TypeScript - An Introduction
PPTX
Introduction about type script
PDF
NoSQL Yes, But YesCQL, No?
PPTX
Typescript 101 introduction
PPTX
TypeScript intro
PDF
Dmytro Kochergin Angular 2 and New Java Script Technologies
PDF
Getting Started with TypeScript
PDF
TypeScript: Angular's Secret Weapon
PPTX
TypeScript Overview
PPTX
Typescript Fundamentals
PDF
Power Leveling your TypeScript
PPTX
PPTX
Type script - advanced usage and practices
PDF
Not Your Fathers C - C Application Development In 2016
TypeScript: coding JavaScript without the pain
Development: What they don't teach you in college
Frameworks
Introduction to Go for Java Developers
So R Debugging
TypeScript Modules
TypeScript - An Introduction
Introduction about type script
NoSQL Yes, But YesCQL, No?
Typescript 101 introduction
TypeScript intro
Dmytro Kochergin Angular 2 and New Java Script Technologies
Getting Started with TypeScript
TypeScript: Angular's Secret Weapon
TypeScript Overview
Typescript Fundamentals
Power Leveling your TypeScript
Type script - advanced usage and practices
Not Your Fathers C - C Application Development In 2016
Ad

Similar to Lint, coverage, doc, autocompletion, transpilation, minification... powered by ast (20)

PPTX
Dart the Better JavaScript
PDF
Develop realtime web with Scala and Xitrum
KEY
Eclipse meets e4
PPTX
Challenges of angular in production (Tasos Bekos) - GreeceJS #17
PPTX
Typescript in 30mins
PPTX
Apache Camel K - Copenhagen v2
PPTX
Apache Camel K - Copenhagen
PDF
1.6 米嘉 gobuildweb
PDF
Inside the JVM - Follow the white rabbit!
PDF
apachecamelk-april2019-190409093034.pdf
PDF
"Xapi-lang For declarative code generation" By James Nelson
PDF
Daniel Steigerwald - Este.js - konec velkého Schizma
PPTX
SenchaCon 2016: Learn the Top 10 Best ES2015 Features - Lee Boonstra
PPTX
Java Script recruiting
PPTX
JavaOne 2017 CON3282 - Code Generation with Annotation Processors: State of t...
PDF
Tangram
PDF
Killing the Angle Bracket
PDF
Node.js Course 2 of 2 - Advanced techniques
PDF
DSLs in JavaScript
PPTX
Technology Stack Discussion
Dart the Better JavaScript
Develop realtime web with Scala and Xitrum
Eclipse meets e4
Challenges of angular in production (Tasos Bekos) - GreeceJS #17
Typescript in 30mins
Apache Camel K - Copenhagen v2
Apache Camel K - Copenhagen
1.6 米嘉 gobuildweb
Inside the JVM - Follow the white rabbit!
apachecamelk-april2019-190409093034.pdf
"Xapi-lang For declarative code generation" By James Nelson
Daniel Steigerwald - Este.js - konec velkého Schizma
SenchaCon 2016: Learn the Top 10 Best ES2015 Features - Lee Boonstra
Java Script recruiting
JavaOne 2017 CON3282 - Code Generation with Annotation Processors: State of t...
Tangram
Killing the Angle Bracket
Node.js Course 2 of 2 - Advanced techniques
DSLs in JavaScript
Technology Stack Discussion
Ad

More from Alexandre Morgaut (20)

PDF
Past, present, and future of web assembly - Devfest Nantes 2017
PDF
angular-wakanda ngParis meetup 15 at 42
PDF
Carnet de Route du Développeur - ENSIMAG 2012
PDF
Conquer Architectural Challenges with End-to-End JavaScript - enterJS 2014
PDF
HTML5 in automotive - web2day 2014
PDF
JS in SMS - JS.everywhere(2013)
PDF
Js in Automotive - JS.everywhere(2013)
PDF
End-to-end HTML5 APIs - The Geek Gathering 2013
PDF
From Web App Model Design to Production with Wakanda
PDF
NoSQL and JavaScript: a love story
PDF
Wakanda - apps.berlin.js - 2012-11-29
PDF
End to-end W3C - JS.everywhere(2012) Europe
PDF
Wakanda and the top 5 security risks - JS.everyrwhere(2012) Europe
PDF
End-to-end W3C APIs - tpac 2012
PDF
End-to-end W3C APIs
PDF
NoSQL and JavaScript: a Love Story
PDF
Wakanda: NoSQL for Model-Driven Web applications - NoSQL matters 2012
PDF
Wakanda: NoSQL & SSJS for Model-driven Web Applications - SourceDevCon 2012
KEY
State of the art - server side JavaScript - web-5 2012
PDF
Etat de l'art Server-Side JavaScript - JS Geneve
Past, present, and future of web assembly - Devfest Nantes 2017
angular-wakanda ngParis meetup 15 at 42
Carnet de Route du Développeur - ENSIMAG 2012
Conquer Architectural Challenges with End-to-End JavaScript - enterJS 2014
HTML5 in automotive - web2day 2014
JS in SMS - JS.everywhere(2013)
Js in Automotive - JS.everywhere(2013)
End-to-end HTML5 APIs - The Geek Gathering 2013
From Web App Model Design to Production with Wakanda
NoSQL and JavaScript: a love story
Wakanda - apps.berlin.js - 2012-11-29
End to-end W3C - JS.everywhere(2012) Europe
Wakanda and the top 5 security risks - JS.everyrwhere(2012) Europe
End-to-end W3C APIs - tpac 2012
End-to-end W3C APIs
NoSQL and JavaScript: a Love Story
Wakanda: NoSQL for Model-Driven Web applications - NoSQL matters 2012
Wakanda: NoSQL & SSJS for Model-driven Web Applications - SourceDevCon 2012
State of the art - server side JavaScript - web-5 2012
Etat de l'art Server-Side JavaScript - JS Geneve

Recently uploaded (20)

PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
cuic standard and advanced reporting.pdf
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPT
Teaching material agriculture food technology
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
Spectroscopy.pptx food analysis technology
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Cloud computing and distributed systems.
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
Programs and apps: productivity, graphics, security and other tools
20250228 LYD VKU AI Blended-Learning.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
cuic standard and advanced reporting.pdf
sap open course for s4hana steps from ECC to s4
Chapter 3 Spatial Domain Image Processing.pdf
Teaching material agriculture food technology
The Rise and Fall of 3GPP – Time for a Sabbatical?
Digital-Transformation-Roadmap-for-Companies.pptx
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Spectroscopy.pptx food analysis technology
Building Integrated photovoltaic BIPV_UPV.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
NewMind AI Weekly Chronicles - August'25 Week I
Diabetes mellitus diagnosis method based random forest with bat algorithm
Cloud computing and distributed systems.
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Per capita expenditure prediction using model stacking based on satellite ima...

Lint, coverage, doc, autocompletion, transpilation, minification... powered by ast

  • 1. Lint, Coverage, Doc, Autocompletion, Transpilation, Minification... powered by AST Alexandre Morgaut NantesJS Meetup #39 - April 2019
  • 2. ● Ealy JavaScript Adopter (1996) ● Former Wakanda Server-Side JS platform Team (2008) ● JS & Web Standards Evangelist since 2009 ● Former member of CommonJS (2010) ● Former W3C Advisory Committee member (2012) ● Initiated NantesJS (2012) ● Joined the Wiztivi company (2015) Presentation @amorgaut wiztivi.comAMorgaut
  • 3. Summary ● Compilers ● BinAST ● Editors ● JavaScript AST ● Using AST in Plugins ● Task Managers ● AST Explorer
  • 8. Some Token Types Name Examples identifiers x, color, UP​ keywords if, while, return​ punctuation }, (, ; separators +, <, =​ literals true, 6.02e23, "music"​ comments // en ligne, /* en bloc */​
  • 10. Binary AST ● Give the JS engine a pre-tokenized, pre-parsed, script content ● Give it in binary format to make it lighter ● Current advanced prototypes already show JS parsing improvements of 30%-50% on all the most common frameworks https://github.com/tc39/proposal-binary-ast https://github.com/binast/binjs-ref
  • 13. Editor Features ● With Tokenizer Help ○ Syntax highlighting ○ Collapse / Expand ● With AST Help ○ Lint ○ Find Usages ○ Code Completion ○ Reformat ○ Statement level breakpoints
  • 14. ACE - The Cloud9 Editor // ### Abstract Syntax Tree // // The default AST structure is inspired by the Mozilla Parser API but can // easily be customized by overriding these functions. // Lexer // ----- // // The lexer, or the tokenizer reads the input string character by character // and derives a token left-right. To be as efficient as possible the lexer // prioritizes the common cases such as identifiers. It also works with // character codes instead of characters as string comparisons was the // biggest bottleneck of the parser. https://github.com/ajaxorg/ace/blob/9b5b63d1dc7c1b81b58d30c87d14b5905d030ca5/lib/ace/mode/lua/luaparse.js
  • 15. ternjs code-analysis engine for JavaScript ● Autocompletion on variables and properties ● Function argument hints ● Querying the type of an expression ● Finding the definition of something ● Automatic refactoring Built-in in the Bracket Editor (Adobe) https://ternjs.net/
  • 17. SpiderMonkey engine's JavaScript parser API https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/Parser_API
  • 18. Parsers ● espree ○ ESLint, estraverse, hint, javascript-obfuscator, JSDoc, protobuf.js, v8/web-tooling-benchmark ● babylon / babel-parser ○ babel, ESDoc ● esprima ○ previous ESLint, flow, istanbul ● acorn ○ espree, buble, browserify, ternjs, webpack, optimize-js
  • 19. Espree ● Fork of Esprima (v1.2.2) ○ Primary goal is to produce the exact same AST structure and tokens as Esprima ○ The AST structure being the ESTree API with JSX extensions ● Built on top of Acorn ○ use its modular architecture -> extensions of core functionalities ● Maintained by the ESLint project https://github.com/eslint/espree const ast = espree.parse(code);
  • 20. ESTree "This site will serve as a community standard" "keep up with the evolution of the JavaScript language" https://github.com/estree/estree
  • 22. Using AST in Plugins
  • 23. ESLint Rule Enforce a maximum number of parameters in function definitions https://eslint.org/docs/rules/max-params https://github.com/eslint/eslint/blob/master/lib/rules/max-params.js
  • 24. Rule Option Management Rule Validation Code AST Node Type Binding
  • 25. Babel Transform Plugin babel-plugin-transform-class A minimalist ES6 class babel transformer ● Remove ES6 forbidden actions controls ○ ex: throw on this modification before calling super() ○ Encourage to check that at edit time using ESLint rules ● Remove Polyfill generations ○ Generate Full ES5 compatible code ○ Even compatible with ES3 using very minimalist Polyfills https://github.com/AMorgaut/babel-plugin-transform-class
  • 26. Return a visitor object ● keys: AST Node types ● values: callback or map of callbacks ○ enter: beginning of the node block ○ exit: end of the node block babel.types ● provides useful helpers https://github.com/AMorgaut/babel-plugin-transform-class/blob/master/src/index.js
  • 28. Task Manager Grunt, Gulp, npm, ... webpack ● $ npm run build ○ lint > transpile* > concat* > minify* ○ * with SourceMap ○ each step using it's own AST ● Some of those tools (ex: babel) can use an existing AST
  • 31. AST Explorer 1. Select (or use default) a: a. Source Type (JavaScript, CSS, HTML, …) b. Parser Lib (babel, eslint, …) c. (Optional) Transformer Lib 2. Provide a Source Code a. A Browsable matching AST is automatically generated 3. Provide a Transform Code a. An Output Source is automatically updated b. Error messages code be shown instead to help fixing the code