File tree Expand file tree Collapse file tree 3 files changed +56
-3
lines changed Expand file tree Collapse file tree 3 files changed +56
-3
lines changed Original file line number Diff line number Diff line change 1
1
const generate = require ( 'babel-generator' ) . default ;
2
2
3
3
const looksLike = require ( './utils/looks-like' ) ;
4
+ const { removeComments } = require ( './utils' ) ;
4
5
5
6
module . exports = function ( { template, types } ) {
6
7
return {
@@ -51,9 +52,7 @@ module.exports = function({ template, types }) {
51
52
52
53
// generate the code
53
54
const { code } = generate ( body ) ;
54
-
55
- // remove comments
56
- const normalisedCode = code . replace ( / \/ \/ ( .* ) / g, '' ) . replace ( / \/ \* ( [ \s \S ] * ?) \* \/ / g, '' ) ;
55
+ const normalisedCode = removeComments ( code ) ;
57
56
58
57
const count = ( normalisedCode . match ( / e x p e c t \( / g) || [ ] ) . length ;
59
58
const containsExpectAssertions = normalisedCode . includes ( 'expect.assertions(' ) ;
Original file line number Diff line number Diff line change
1
+ const EMPTY = '' ;
2
+ const SINGLE_LINE_COMMENT = / \/ \/ ( .* ) / g;
3
+ const MULTI_LINE_COMMENT = / \/ \* ( [ \s \S ] * ?) \* \/ / g;
4
+
5
+ const removeComments = code => code . replace ( SINGLE_LINE_COMMENT , EMPTY ) . replace ( MULTI_LINE_COMMENT , EMPTY ) ;
6
+
7
+ module . exports = {
8
+ removeComments
9
+ } ;
Original file line number Diff line number Diff line change
1
+ const { removeComments } = require ( './' ) ;
2
+
3
+ describe ( 'Utils' , ( ) => {
4
+ describe ( '.removeComments' , ( ) => {
5
+ it ( 'returns given string when string does not contain any comments' , ( ) => {
6
+ const str = 'hello world' ;
7
+ expect ( removeComments ( str ) ) . toEqual ( str ) ;
8
+ } ) ;
9
+ it ( 'returns empty string when given a single line comment' , ( ) => {
10
+ expect ( removeComments ( '// hello world' ) ) . toEqual ( '' ) ;
11
+ } ) ;
12
+ it ( 'returns empty string when given a multi line comment' , ( ) => {
13
+ expect (
14
+ removeComments ( `/*
15
+ hello world
16
+ */` )
17
+ ) . toEqual ( '' ) ;
18
+ } ) ;
19
+ it ( 'returns string without comments when given a string containing a singleline comment' , ( ) => {
20
+ expect ( removeComments ( 'abc//hello world' ) ) . toEqual ( 'abc' ) ;
21
+ } ) ;
22
+ it ( 'returns string without comments when given a string containing a multi line comment' , ( ) => {
23
+ expect (
24
+ removeComments ( `abc/*
25
+ hello world
26
+ */def` )
27
+ ) . toEqual ( 'abcdef' ) ;
28
+ } ) ;
29
+ it ( 'returns string without comments when given a string with both single and multi line comments' , ( ) => {
30
+ expect (
31
+ removeComments ( `abc /* hello world */
32
+ // foo
33
+ /*
34
+ bar
35
+ */
36
+ xyz
37
+ /*
38
+ baz
39
+ */
40
+ def // qux
41
+ ` ) . replace ( / \s / g, '' )
42
+ ) . toEqual ( 'abcxyzdef' ) ;
43
+ } ) ;
44
+ } ) ;
45
+ } ) ;
You can’t perform that action at this time.
0 commit comments