SlideShare una empresa de Scribd logo
1 de 11
Esprima – What is that ?
https://github.com/ariya/esprima
http://esprima.org
What is Esprima
Esprima is an ECMAScript parser written in ECMAScript
(Javascript)
Esprima produces a source tree of your JavaScript code (in json
format), which in turn can be traversed to analyze the code for
various aspects. Also static code analysis can be applied to give an
insight to the code.
It can help to determine meta information about the Javascript
code.
Full support for ECMAScript 5.1 - Any Javascript code written in
EcmaScript 5.1 or below can be parsed throgh current version of
Esprima(v1.0.4).
Availabilty
Esprima is available:
✔

on Browser

✔

as a node package

✔

through module loader(RequireJS)

✔

as Bower component

✔

with Yeoman
Usage
esprima.parse(code, options);
This produces a sytax tree format that is compatible with
Parser API of SpiderMonkey (Mozilla).
Options parameter can be provided to generate the detailed
syntax tree.

esprima.tokenize(code, options);
This produces an array of tokens used in JavaScript Program
Usage (parameters)
code is the actual JavaScript code as string.
options parameter can have following:
loc - when true, nodes have line and column-based location info
range - when true, nodes have an index-based location range (array)
raw – when true, literals have extra property to store the verbatim source
tokens – when true, an extra array is provided containing all found tokens
comment – when true, an extra array is provided containing all line and
block comments
tolerant - when true, an extra array containing all errors found, attempts
to continue parsing when an error is encountered

All are set to false by default.
Example
esprima.parse('var answer = 42;');
Outputs:
{

}

"type": "Program",
"body": [{
"type": "VariableDeclaration",
"kind": "var",
"declarations": [{
"type": "VariableDeclarator",
"id": {
"type": "Identifier",
"name": "answer"
},
"init": {
"type": "Literal",
"value": 42,
"raw": "42"
}
}]
}]
How can it help
●

Check for general bad practices
–
–
–

●

Identify Double Negatives - component.setHidden(false);
Identify declared, but not called functions;
Identify code/comment ratio

Tool to create utilities as:
–
–
–

Syntax validator
Code Autocomplete
Function Instrumentation
Applications
●

http://nornagon.github.io/scrubby/

●

https://github.com/jedhunsaker/codepainter

●

https://github.com/coveraje/coveraje

●

https://github.com/ariya/esmorph
escodegen
https://github.com/Constellation/escodegen

escodegen generates the ecmascript code from Abstract
Source Tree (the source tree generated from Esprima)
escodegen.generate({
type: 'BinaryExpression',
operator: '+',
left: { type: 'Literal', value: 40 },
right: { type: 'Literal', value: 2 }
});

produces the string '40 + 2'
Similar things
●

acorn.js http://marijnhaverbeke.nl/acorn/

●

Traceur-compiler https://github.com/google/traceur-compiler
Traceur is a JavaScript.next-to-JavaScript-of-today compiler

●

ZeParser https://github.com/qfox/ZeParser

●

Narcissus https://github.com/mozilla/narcissus

●

UglifyJS 2 https://github.com/mishoo/UglifyJS2
References
●

Esprima - http://esprima.org

●

Presentation by Ariya Hidayat http://www.youtube.com/watch?v=ACYZFkvq0Sk

●

Parser API by Mozilla https://developer.mozilla.org/en-US/docs/SpiderMonkey/Parser_API

Más contenido relacionado

La actualidad más candente

Yapcasia2011 - Hello Embed Perl
Yapcasia2011 - Hello Embed PerlYapcasia2011 - Hello Embed Perl
Yapcasia2011 - Hello Embed Perl
Hideaki Ohno
 
PHP Tips for certification - OdW13
PHP Tips for certification - OdW13PHP Tips for certification - OdW13
PHP Tips for certification - OdW13
julien pauli
 

La actualidad más candente (20)

ES6 PPT FOR 2016
ES6 PPT FOR 2016ES6 PPT FOR 2016
ES6 PPT FOR 2016
 
Building fast interpreters in Rust
Building fast interpreters in RustBuilding fast interpreters in Rust
Building fast interpreters in Rust
 
Interceptors: Into the Core of Pedestal
Interceptors: Into the Core of PedestalInterceptors: Into the Core of Pedestal
Interceptors: Into the Core of Pedestal
 
Rails on Oracle 2011
Rails on Oracle 2011Rails on Oracle 2011
Rails on Oracle 2011
 
Rails-like JavaScript Using CoffeeScript, Backbone.js and Jasmine
Rails-like JavaScript Using CoffeeScript, Backbone.js and JasmineRails-like JavaScript Using CoffeeScript, Backbone.js and Jasmine
Rails-like JavaScript Using CoffeeScript, Backbone.js and Jasmine
 
Looping the Loop with SPL Iterators
Looping the Loop with SPL IteratorsLooping the Loop with SPL Iterators
Looping the Loop with SPL Iterators
 
Electrify your code with PHP Generators
Electrify your code with PHP GeneratorsElectrify your code with PHP Generators
Electrify your code with PHP Generators
 
Ruby 2.0
Ruby 2.0Ruby 2.0
Ruby 2.0
 
ES6 in Production [JSConfUY2015]
ES6 in Production [JSConfUY2015]ES6 in Production [JSConfUY2015]
ES6 in Production [JSConfUY2015]
 
Working with databases in Perl
Working with databases in PerlWorking with databases in Perl
Working with databases in Perl
 
P6 OO vs Moose (&Moo)
P6 OO vs Moose (&Moo)P6 OO vs Moose (&Moo)
P6 OO vs Moose (&Moo)
 
Yapcasia2011 - Hello Embed Perl
Yapcasia2011 - Hello Embed PerlYapcasia2011 - Hello Embed Perl
Yapcasia2011 - Hello Embed Perl
 
PHP 7 – What changed internally? (PHP Barcelona 2015)
PHP 7 – What changed internally? (PHP Barcelona 2015)PHP 7 – What changed internally? (PHP Barcelona 2015)
PHP 7 – What changed internally? (PHP Barcelona 2015)
 
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
 
Stupid Awesome Python Tricks
Stupid Awesome Python TricksStupid Awesome Python Tricks
Stupid Awesome Python Tricks
 
A Few of My Favorite (Python) Things
A Few of My Favorite (Python) ThingsA Few of My Favorite (Python) Things
A Few of My Favorite (Python) Things
 
PHP Tips for certification - OdW13
PHP Tips for certification - OdW13PHP Tips for certification - OdW13
PHP Tips for certification - OdW13
 
I, For One, Welcome Our New Perl6 Overlords
I, For One, Welcome Our New Perl6 OverlordsI, For One, Welcome Our New Perl6 Overlords
I, For One, Welcome Our New Perl6 Overlords
 
Perl Basics for Pentesters Part 1
Perl Basics for Pentesters Part 1Perl Basics for Pentesters Part 1
Perl Basics for Pentesters Part 1
 
Consuming Web Services with Swift and Rx
Consuming Web Services with Swift and RxConsuming Web Services with Swift and Rx
Consuming Web Services with Swift and Rx
 

Similar a Esprima - What is that

Lab 1 Recursion  Introduction   Tracery (tracery.io.docx
Lab 1 Recursion  Introduction   Tracery (tracery.io.docxLab 1 Recursion  Introduction   Tracery (tracery.io.docx
Lab 1 Recursion  Introduction   Tracery (tracery.io.docx
smile790243
 
JavaScript Parser Infrastructure for Code Quality Analysis
JavaScript Parser Infrastructure for Code Quality AnalysisJavaScript Parser Infrastructure for Code Quality Analysis
JavaScript Parser Infrastructure for Code Quality Analysis
Ariya Hidayat
 
NOSQL and Cassandra
NOSQL and CassandraNOSQL and Cassandra
NOSQL and Cassandra
rantav
 
STC 2016 Programming Language Storytime
STC 2016 Programming Language StorytimeSTC 2016 Programming Language Storytime
STC 2016 Programming Language Storytime
Sarah Kiniry
 
Raleigh Code Camp 2103 - .Net Metaprogramming Essentials
Raleigh Code Camp 2103 - .Net Metaprogramming EssentialsRaleigh Code Camp 2103 - .Net Metaprogramming Essentials
Raleigh Code Camp 2103 - .Net Metaprogramming Essentials
Sean McCarthy
 
Scala Language Intro - Inspired by the Love Game
Scala Language Intro - Inspired by the Love GameScala Language Intro - Inspired by the Love Game
Scala Language Intro - Inspired by the Love Game
Antony Stubbs
 

Similar a Esprima - What is that (20)

Algorithm and Programming (Introduction of dev pascal, data type, value, and ...
Algorithm and Programming (Introduction of dev pascal, data type, value, and ...Algorithm and Programming (Introduction of dev pascal, data type, value, and ...
Algorithm and Programming (Introduction of dev pascal, data type, value, and ...
 
JavaParser - A tool to generate, analyze and refactor Java code
JavaParser - A tool to generate, analyze and refactor Java codeJavaParser - A tool to generate, analyze and refactor Java code
JavaParser - A tool to generate, analyze and refactor Java code
 
Lab 1 Recursion  Introduction   Tracery (tracery.io.docx
Lab 1 Recursion  Introduction   Tracery (tracery.io.docxLab 1 Recursion  Introduction   Tracery (tracery.io.docx
Lab 1 Recursion  Introduction   Tracery (tracery.io.docx
 
Developing web apps using Erlang-Web
Developing web apps using Erlang-WebDeveloping web apps using Erlang-Web
Developing web apps using Erlang-Web
 
Java programming basics
Java programming basicsJava programming basics
Java programming basics
 
Apache avro data serialization framework
Apache avro   data serialization frameworkApache avro   data serialization framework
Apache avro data serialization framework
 
JavaScript Parser Infrastructure for Code Quality Analysis
JavaScript Parser Infrastructure for Code Quality AnalysisJavaScript Parser Infrastructure for Code Quality Analysis
JavaScript Parser Infrastructure for Code Quality Analysis
 
Java OOP Concepts 1st Slide
Java OOP Concepts 1st SlideJava OOP Concepts 1st Slide
Java OOP Concepts 1st Slide
 
NOSQL and Cassandra
NOSQL and CassandraNOSQL and Cassandra
NOSQL and Cassandra
 
php&mysql with Ethical Hacking
php&mysql with Ethical Hackingphp&mysql with Ethical Hacking
php&mysql with Ethical Hacking
 
Scala presentationjune112011
Scala presentationjune112011Scala presentationjune112011
Scala presentationjune112011
 
Introduction java programming
Introduction java programmingIntroduction java programming
Introduction java programming
 
STC 2016 Programming Language Storytime
STC 2016 Programming Language StorytimeSTC 2016 Programming Language Storytime
STC 2016 Programming Language Storytime
 
Raleigh Code Camp 2103 - .Net Metaprogramming Essentials
Raleigh Code Camp 2103 - .Net Metaprogramming EssentialsRaleigh Code Camp 2103 - .Net Metaprogramming Essentials
Raleigh Code Camp 2103 - .Net Metaprogramming Essentials
 
Scala Language Intro - Inspired by the Love Game
Scala Language Intro - Inspired by the Love GameScala Language Intro - Inspired by the Love Game
Scala Language Intro - Inspired by the Love Game
 
Writing a REST Interconnection Library in Swift
Writing a REST Interconnection Library in SwiftWriting a REST Interconnection Library in Swift
Writing a REST Interconnection Library in Swift
 
A Brief Overview of (Static) Program Query Languages
A Brief Overview of (Static) Program Query LanguagesA Brief Overview of (Static) Program Query Languages
A Brief Overview of (Static) Program Query Languages
 
Java Annotations
Java AnnotationsJava Annotations
Java Annotations
 
JVM
JVMJVM
JVM
 
Avro
AvroAvro
Avro
 

Último

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Último (20)

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 

Esprima - What is that

  • 1. Esprima – What is that ? https://github.com/ariya/esprima http://esprima.org
  • 2. What is Esprima Esprima is an ECMAScript parser written in ECMAScript (Javascript) Esprima produces a source tree of your JavaScript code (in json format), which in turn can be traversed to analyze the code for various aspects. Also static code analysis can be applied to give an insight to the code. It can help to determine meta information about the Javascript code. Full support for ECMAScript 5.1 - Any Javascript code written in EcmaScript 5.1 or below can be parsed throgh current version of Esprima(v1.0.4).
  • 3. Availabilty Esprima is available: ✔ on Browser ✔ as a node package ✔ through module loader(RequireJS) ✔ as Bower component ✔ with Yeoman
  • 4. Usage esprima.parse(code, options); This produces a sytax tree format that is compatible with Parser API of SpiderMonkey (Mozilla). Options parameter can be provided to generate the detailed syntax tree. esprima.tokenize(code, options); This produces an array of tokens used in JavaScript Program
  • 5. Usage (parameters) code is the actual JavaScript code as string. options parameter can have following: loc - when true, nodes have line and column-based location info range - when true, nodes have an index-based location range (array) raw – when true, literals have extra property to store the verbatim source tokens – when true, an extra array is provided containing all found tokens comment – when true, an extra array is provided containing all line and block comments tolerant - when true, an extra array containing all errors found, attempts to continue parsing when an error is encountered All are set to false by default.
  • 6. Example esprima.parse('var answer = 42;'); Outputs: { } "type": "Program", "body": [{ "type": "VariableDeclaration", "kind": "var", "declarations": [{ "type": "VariableDeclarator", "id": { "type": "Identifier", "name": "answer" }, "init": { "type": "Literal", "value": 42, "raw": "42" } }] }]
  • 7. How can it help ● Check for general bad practices – – – ● Identify Double Negatives - component.setHidden(false); Identify declared, but not called functions; Identify code/comment ratio Tool to create utilities as: – – – Syntax validator Code Autocomplete Function Instrumentation
  • 9. escodegen https://github.com/Constellation/escodegen escodegen generates the ecmascript code from Abstract Source Tree (the source tree generated from Esprima) escodegen.generate({ type: 'BinaryExpression', operator: '+', left: { type: 'Literal', value: 40 }, right: { type: 'Literal', value: 2 } }); produces the string '40 + 2'
  • 10. Similar things ● acorn.js http://marijnhaverbeke.nl/acorn/ ● Traceur-compiler https://github.com/google/traceur-compiler Traceur is a JavaScript.next-to-JavaScript-of-today compiler ● ZeParser https://github.com/qfox/ZeParser ● Narcissus https://github.com/mozilla/narcissus ● UglifyJS 2 https://github.com/mishoo/UglifyJS2
  • 11. References ● Esprima - http://esprima.org ● Presentation by Ariya Hidayat http://www.youtube.com/watch?v=ACYZFkvq0Sk ● Parser API by Mozilla https://developer.mozilla.org/en-US/docs/SpiderMonkey/Parser_API