Title: | Tools for Working with JavaScript in R |
---|---|
Description: | A set of utilities for working with JavaScript syntax in R. Includes tools to parse, tokenize, compile, validate, reformat, optimize and analyze JavaScript code. |
Authors: | Jeroen Ooms [aut, cre] |
Maintainer: | Jeroen Ooms <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.2.1 |
Built: | 2024-11-02 05:43:16 UTC |
Source: | https://github.com/jeroen/js |
Compiles coffee script into JavaScript.
coffee_compile(code, ...)
coffee_compile(code, ...)
code |
a string with JavaScript code |
... |
additional options passed to the compiler |
# Hello world coffee_compile("square = (x) -> x * x") coffee_compile("square = (x) -> x * x", bare = TRUE) # Simple script demo <- readLines(system.file("example/demo.coffee", package = "js")) js <- coffee_compile(demo) cat(js) cat(uglify_optimize(js))
# Hello world coffee_compile("square = (x) -> x * x") coffee_compile("square = (x) -> x * x", bare = TRUE) # Simple script demo <- readLines(system.file("example/demo.coffee", package = "js")) js <- coffee_compile(demo) cat(js) cat(uglify_optimize(js))
Esprima is a high performance, standard-compliant ECMAScript parser. It has full support for ECMAScript 2017 and returns a sensible syntax tree format as standardized by ESTree project.
esprima_tokenize(text, range = FALSE, loc = FALSE, comment = FALSE) esprima_parse(text, jsx = FALSE, range = FALSE, loc = FALSE, tolerant = FALSE, tokens = FALSE, comment = FALSE)
esprima_tokenize(text, range = FALSE, loc = FALSE, comment = FALSE) esprima_parse(text, jsx = FALSE, range = FALSE, loc = FALSE, tolerant = FALSE, tokens = FALSE, comment = FALSE)
text |
a character vector with JavaScript code |
range |
Annotate each token with its zero-based start and end location |
loc |
Annotate each token with its column and row-based location |
comment |
Include every line and block comment in the output |
jsx |
Support JSX syntax |
tolerant |
Tolerate a few cases of syntax errors |
tokens |
Collect every token |
The esprima_tokenize
function returns a data frame with JavaScript tokens. The
esprima_parse
function returns the Syntax Tree in JSON format. This can be parsed to R
using e.g. jsonlite::fromJSON
.
Esprima documentation: http://esprima.readthedocs.io/en/4.0/.
code <- "function test(x, y){ x = x || 1; y = y || 1; return x*y;}" esprima_tokenize(code) esprima_parse(code)
code <- "function test(x, y){ x = x || 1; y = y || 1; return x*y;}" esprima_tokenize(code) esprima_parse(code)
Evaluate a piece of JavaScript code in a disposable context.
js_eval(text)
js_eval(text)
text |
JavaScript code |
# Stateless evaluation js_eval("(function() {return 'foo'})()") # Use V8 for stateful evaluation ct <- V8::new_context() ct$eval("var foo = 123") ct$get("foo")
# Stateless evaluation js_eval("(function() {return 'foo'})()") # Use V8 for stateful evaluation ct <- V8::new_context() ct$eval("var foo = 123") ct$get("foo")
JavaScript wrapper to typeof
to test if a piece of JavaScript code is
syntactically valid, and the type of object it evaluates to. Useful to
verify that a piece of JavaScript code contains a proper function/object.
js_typeof(text)
js_typeof(text)
text |
JavaScript code |
js_typeof("function(x){return x+1}") js_typeof("(function() {return 'foo'})()") js_typeof("{foo : 123, bar : true}")
js_typeof("function(x){return x+1}") js_typeof("(function() {return 'foo'})()") js_typeof("{foo : 123, bar : true}")
Simple wrapper for ct$validate
in V8.
Tests if code constitutes a syntactically valid JS script.
js_validate_script(text, error = TRUE)
js_validate_script(text, error = TRUE)
text |
character vector with JavaScript code |
error |
raise error on invalid code |
js_validate_script("function foo(x){2*x}") #TRUE js_validate_script("foo = function(x){2*x}") #TRUE # Anonymous functions in global scope are invalid js_validate_script("function(x){2*x}", error = FALSE) #FALSE # Use ! or () to check anonymous function syntax js_validate_script("!function(x){2*x}") #TRUE js_validate_script("(function(x){2*x})") #TRUE
js_validate_script("function foo(x){2*x}") #TRUE js_validate_script("foo = function(x){2*x}") #TRUE # Anonymous functions in global scope are invalid js_validate_script("function(x){2*x}", error = FALSE) #FALSE # Use ! or () to check anonymous function syntax js_validate_script("!function(x){2*x}") #TRUE js_validate_script("(function(x){2*x})") #TRUE
JSHint is a community-driven tool to detect errors and potential problems in JavaScript code. It is very flexible so you can easily adjust it to your particular coding guidelines and the environment you expect your code to execute in.
jshint(text, ..., globals = NULL)
jshint(text, ..., globals = NULL)
text |
a string of JavaScript code |
... |
additional jshint configuration options |
globals |
a white list of global variables that are not formally defined in the source code |
a data frame where each row represents a jshint error or NULL
if there were no errors
code = "var foo = 123" jshint(code) jshint(code, asi = TRUE)
code = "var foo = 123" jshint(code) jshint(code, asi = TRUE)
UglifyJS is a JavaScript compressor/minifier written in JavaScript. It also contains tools that allow one to automate working with JavaScript code.
uglify_reformat(text, beautify = FALSE, ...) uglify_optimize(text, ...) uglify_files(files, ...)
uglify_reformat(text, beautify = FALSE, ...) uglify_optimize(text, ...) uglify_files(files, ...)
text |
a character vector with JavaScript code |
beautify |
prettify (instead of minify) code |
files |
a character vector of filenames |
... |
UglifyJS2 Documentation: https://lisperator.net/uglifyjs/.
code <- "function test(x, y){ x = x || 1; y = y || 1; return x*y;}" cat(uglify_optimize(code)) cat(uglify_reformat(code, beautify = TRUE, indent_level = 2))
code <- "function test(x, y){ x = x || 1; y = y || 1; return x*y;}" cat(uglify_optimize(code)) cat(uglify_reformat(code, beautify = TRUE, indent_level = 2))