Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 141 lines (107 sloc) 4.764 kB
b676cbf Added a README file.
Ryan Patterson authored
1 # jsgrep: a syntactically-aware grep for JavaScript
2
4c6b655 Add support for loose object matching
Ryan Patterson authored
3 Jsgrep is program that searches for a particular JavaScript pattern using the
4 abstract syntax tree (AST) of the program. This enables matching expressions
5 based on their JavaScript meaning, rather than based on simple strings.
b676cbf Added a README file.
Ryan Patterson authored
6
58384b5 Updated README
Ryan Patterson authored
7 ## Examples
b676cbf Added a README file.
Ryan Patterson authored
8
9 **Find calls to window.setTimeout with a 0 timeout**
10
8beed8e Swap out option parser
Ryan Patterson authored
11 $ jsgrep 'setTimeout(A, 0)' tests/*.js
b676cbf Added a README file.
Ryan Patterson authored
12 tests/jquery.js: setTimeout( function() {
13 tests/jquery.js: setTimeout( clearFxNow, 0 );
14
4c6b655 Add support for loose object matching
Ryan Patterson authored
15 Jsgrep uses metavariables as wildcards. Metavariables match any valid JavaScript
16 chunk, so in this case, the first match was an entire inline function. In both
17 cases, the second parameter to setTimeout was a literal value of 0.
8beed8e Swap out option parser
Ryan Patterson authored
18
19 **Find value defaulting**
20
4c6b655 Add support for loose object matching
Ryan Patterson authored
21 $ jsgrep "A = A || B;" tests/*.js
8beed8e Swap out option parser
Ryan Patterson authored
22 tests/jquery.js: args = args || [];
23 tests/jquery.js: dataType = dataType || options.dataTypes[ 0 ];
24
25 When the pattern references the same metavariable multiple times, jsgrep ensures
26 that the value of the metavariable is the same throughout the match.
27
2cf1d47 Add support for ...
Ryan Patterson authored
28 **Find the names of all invoked events**
29
3ea9eac matchStrict/findStrict
Ryan Patterson authored
30 $ ./jsgrep -p B "A.fire(B, ...)" tests/*.js
2cf1d47 Add support for ...
Ryan Patterson authored
31 tests/connect.js: 'auth.logout'
32 tests/connect.js: 'auth.login'
33
34 The `...` operator matches 0 or more expressions in function calls and array
612f1f0 [WIP] jspatch
Ryan Patterson authored
35 initializers. This example also uses the `-p` flag to print only a particular
36 matched variable.
2cf1d47 Add support for ...
Ryan Patterson authored
37
4c6b655 Add support for loose object matching
Ryan Patterson authored
38 **Find classes that have a 'path' property**
39
40 $ jsgrep -p C "JX.install(C, { properties: { path: X } })" tests/*.js
41 tests/javelin.js: 'Event'
42 tests/javelin.js: 'URI'
43
44 Jsgrep allows you to search object initializations partially, which enables
612f1f0 [WIP] jspatch
Ryan Patterson authored
45 easily drilling into the structure of JavaScript classes.
4c6b655 Add support for loose object matching
Ryan Patterson authored
46
759c7c1 Sed mode for jspatch
Ryan Patterson authored
47 ## jspatch
48
49 Jsgrep allows you to identify source code that resembles a certain pattern.
50 Jspatch is a tool to take that information and programmatically modify the
51 original source code.
52
53 **Replace setTimeout with window.setTimeout**
54
25774f5 Improved patch file processing
Ryan Patterson authored
55 $ jspatch -e 's/setTimeout( A, B )/window.setTimeout( A, B )/' tests/jquery.js
759c7c1 Sed mode for jspatch
Ryan Patterson authored
56 --- tests/jquery.js 2012-01-10 14:51:36.000000000 -0800
57 +++ /tmp/jspatch.jquery.js 2012-01-11 17:13:56.000000000 -0800
58 @@ -420,7 +420,7 @@
59 if ( (wait === true && !--jQuery.readyWait) || (wait !== true && !jQuery.isReady) ) {
60 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
61 if ( !document.body ) {
62 - return setTimeout( jQuery.ready, 1 );
63 + return window.setTimeout( jQuery.ready, 1 );
64 }
65
66 // Remember that the DOM is ready
67 @@ -1987,7 +1987,7 @@
68 ( src === "mark" || !jQuery._data(elem, markDataKey) ) ) {
69 // Give room for hard-coded callbacks to fire first
70 // and eventually mark/queue something else on the element
71 - setTimeout( function() {
72 + window.setTimeout( function() {
73 if ( !jQuery._data( elem, queueDataKey ) &&
74 !jQuery._data( elem, markDataKey ) ) {
75 jQuery.removeData( elem, deferDataKey, true );
76
77 Sed mode allows you to quickly perform simple substitutions on the source code.
78
25774f5 Improved patch file processing
Ryan Patterson authored
79 **Refactor properties/members into just properties**
80
81 $ cat tests/transform.spatch
82 JX.install(N, {
83 ...,
84 - properties : {
85 + members : {
86 ...
87 + ,
88 - },
89 - members : {
90 ...
91 },
92 ...
93 })
94
95 $ jspatch -f tests/transform.spatch tests/javelin.js
96 --- tests/javelin.js 2012-01-08 10:55:04.000000000 -0800
97 +++ /tmp/jspatch.javelin.js 2012-01-12 03:06:45.000000000 -0800
98 @@ -3615,16 +3615,14 @@
99 }
100 },
101
102 - properties : {
103 + members : {
104 protocol: undefined,
105 port: undefined,
106 path: undefined,
107 queryParams: undefined,
108 fragment: undefined,
109 - querySerializer: undefined
110 - },
111 -
112 - members : {
113 + querySerializer: undefined,
114 +
115 _domain: undefined,
116
117 /**
118
119 This complex spatch file leverages the fact that properties and members are
120 always adjacent to one another in the source. This could be verified using
121 jsgrep to ensure that no cases were missed.
122
58384b5 Updated README
Ryan Patterson authored
123 ## TODO
124
8bb3d60 Add some protection to metavars
Ryan Patterson authored
125 * Consider a metavar that matches but doesn't save, to prevent ambiguities.
25774f5 Improved patch file processing
Ryan Patterson authored
126 * Rewrite forEachNode to be iterative rather than recursive (to fix ...)
2cf1d47 Add support for ...
Ryan Patterson authored
127 * Consider support for --where/--eval and/or not-patterns
4c6b655 Add support for loose object matching
Ryan Patterson authored
128 * Support for most statements in patterns
612f1f0 [WIP] jspatch
Ryan Patterson authored
129 * Formally testing the pattern matchers
58384b5 Updated README
Ryan Patterson authored
130
759c7c1 Sed mode for jspatch
Ryan Patterson authored
131 jspatch:
132
25774f5 Improved patch file processing
Ryan Patterson authored
133 * Testing
759c7c1 Sed mode for jspatch
Ryan Patterson authored
134
58384b5 Updated README
Ryan Patterson authored
135 ## Contributors
136
137 Jsgrep was written by Ryan Patterson at Facebook. Inspiration for this project
138 draws heavily from pfff, a source code analyzer written at Facebook.
139
140 * pfff - https://github.com/facebook/pfff
Something went wrong with that request. Please try again.