Skip to content

Commit e4f64e4

Browse files
committedApr 17, 2014
Adding code fence syntax definitions
1 parent 55facfd commit e4f64e4

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed
 

‎posts/jsfmt.md

+12-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Analyze What?
1313

1414
The Rdio app uses a component architecture where a component is a collection of HTML, CSS and JavaScript that form a complete set of functionality. Buttons, forms, and views are all components as well as the overall Rdio app itself which in turn depends on a number of other components. We explicitly declare these dependencies in each component declaration so we know which subsequent components to load at runtime:
1515

16-
```
16+
```javascript
1717
R.Components.create('MyComponent', {
1818
dependencies: ['MyOtherComponent']
1919
});
@@ -44,7 +44,7 @@ I've spent the last year thinking about the problem and a more elegant solution
4444

4545
The first thing we want to do is take a plain JavaScript string that represents what we want to search for and parse it into an AST. For example, if we want to find the function call `"_.reduce()"` then we parse that string into an AST that looks something like this:
4646

47-
```
47+
```json
4848
{
4949
"type": "ExpressionStatement",
5050
"expression": {
@@ -76,23 +76,29 @@ I've iterated a lot on how to best handle a list in the AST, such as the `argume
7676

7777
As an example, if we wanted to find all occurrences of Underscore's reduce function with 3 arguments:
7878

79-
`jsfmt --search "_.reduce(a, b, c)" <source>`
79+
```bash
80+
jsfmt --search "_.reduce(a, b, c)" <source>
81+
```
8082

8183
The wildcards `a`, `b` and `c` will match any expression at that location. Furthermore, we match any `_.reduce` call with 3 _or more_ arguments.
8284

8385
Taking this example a step further, what if we wanted to replace all occurrences of one function with another such as a library upgrade or dropping support for an older browser? For example, replacing all `_.reduce` calls with the native JavaScript `Array.prototype.reduce`. We use the same syntax as before but also specifying the replacement after an arrow (`[match] -> [replacement]`):
8486

85-
`jsfmt --replace "_.reduce(a, b, c) -> a.reduce(b, c)" <source>`
87+
```bash
88+
jsfmt --replace "_.reduce(a, b, c) -> a.reduce(b, c)" <source>
89+
```
8690

8791
We can use the same wildcards in the "match" as placeholders in our "replacement".
8892

8993
Back to the original problem of finding component dependencies. Before `jsfmt` even our simple static analysis was unwieldy. Now searching for component dependencies is simple and intuitive:
9094

91-
`jsfmt --search "R.Component.create(a, { dependencies: z })" <source>`
95+
```bash
96+
jsfmt --search "R.Component.create(a, { dependencies: z })" <source>
97+
```
9298

9399
There's also a JavaScript API exposed:
94100

95-
```
101+
```javascript
96102
jsfmt.search(source, "R.Component.create(a, { dependencies: z })").forEach(function(matches, wildcards) {
97103
console.log(wildcards.z);
98104
});

0 commit comments

Comments
 (0)
Please sign in to comment.