Debian Codegraph @

README.md

raw
1 # Tippex
2
3 Erase comments, strings and regular expressions from JavaScript code.
4
5 ## Why?
6
7 Say you want to do some very simple code analysis, such as finding `import` and `export` statements. You *could* just skim over the code with a regex, but you'll get bad results if matches exist inside comments or strings:
8
9 ```js
10 import a from './a.js';
11 // import b from './b.js'; TODO do we need this?
12 ```
13
14 Instead, you might generate an abstract syntax tree with a parser like [Acorn](https://github.com/ternjs/acorn), and traverse the AST looking for nodes of a specific type. But for a lot of simple tasks that's overkill – parsing is expensive, traversing is a lot less simple than using regular expressions, and if you're doing anything in the browser it's better to avoid large dependencies.
15
16 Tippex offers some middle ground. It's as robust as a full-fledged parser, but miniscule – and much faster. (Americans: Tippex is what you oddballs call 'Liquid Paper' or 'Wite-Out'.)
17
18
19 ## What does it do?
20
21 Tippex simply replaces the contents of strings (including ES6 template strings), regular expressions and comments with the equivalent whitespace.
22
23 So this...
24
25 ```js
26 var a = 1; // line comment
27 /*
28 block comment
29 */
30 var b = 2;
31 var c = /\w+/;
32 var d = 'some text';
33 var e = "some more text";
34 var f = `an ${ 'unnecessarily' ? `${'complicated'}` : `${'template'}` } string`;
35 ```
36
37 ...becomes this:
38
39 ```js
40 var a = 1; //
41 /*
42
43 */
44 var b = 2;
45 var c = / /;
46 var d = ' ';
47 var e = " ";
48 var f = ` ${ ' ' ? `${' '}` : `${' '}` } `;
49 ```
50
51 Once that's done, you can search for patterns (such as `var` or ` = ` or `import`) in complete confidence that you won't get any false positives.
52
53
54 ## Installation
55
56 ```bash
57 npm install --save tippex
58 ```
59
60 ...or download from unpkg.com ([UMD version](https://unpkg.com/tippex), [ES6 exports version](https://unpkg.com/tippex/dist/tippex.es.js)).
61
62
63 ## Usage
64
65 ```js
66 import * as tippex from 'tippex'; // or `var tippex = require('tippex')`, etc
67
68 var erased = tippex.erase( 'var a = 1; // line comment' );
69 // -> 'var a = 1; // '
70
71 var found = tippex.find( 'var a = 1; // line comment' );
72 // -> [{
73 // start: 11,
74 // end: 26,
75 // type: 'line',
76 // outer: '// line comment',
77 // inner: ' line comment'
78 // }]
79 ```
80
81 Sometimes you might need to match a regular expression against the original string, but ignoring comments etc. For that you can use `tippex.match`:
82
83 ```js
84 var code = `
85 import a from './a.js';
86 // import b from './b.js'; TODO do we need this?
87 `;
88
89 var importPattern = /import (.+?) from '([^']+)'/g; // must have 'g' flag
90 var importDeclarations = [];
91
92 tippex.match( code, importPattern, ( match, name, source ) => {
93 // this callback will be called for each match that *doesn't* begin
94 // inside a comment, string or regular expression
95 importDeclarations.push({ name, source });
96 });
97
98 console.log( importDeclarations );
99 // -> [{
100 // name: 'a',
101 // source: './a.js'
102 // }]
103 ```
104
105 (A complete regular expression for ES6 imports would be a bit more complicated; this is for illustrative purposes.)
106
107 To replace occurrences of a pattern that aren't inside strings or comments, use `tippex.replace`:
108
109 ```js
110 code = tippex.replace( code, importPattern, ( match, name, source ) => {
111 return `var ${name} = require('${source}')`;
112 });
113 ```
114
115
116 ## Known issues
117
118 It's extremely difficult to distinguish between regular expression literals and division operators in certain edge cases at the lexical level. Fortunately, these cases are rare and generally somewhat contrived. If you encounter one in the wild, please raise an issue so we can try to accommodate it.
119
120
121
122 ## License
123
124 MIT
125
126 ----
127
128 Follow [@Rich_Harris](https://twitter.com/Rich_Harris) on Twitter for more artisanal, hand-crafted JavaScript.