Skip to content

Commit a288b59

Browse files
committed
Fix octal and hex values in output
1 parent 575b1e3 commit a288b59

File tree

5 files changed

+26
-11
lines changed

5 files changed

+26
-11
lines changed

lib/detector.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var util = require('util');
22
var EventEmitter = require('events').EventEmitter;
33
var Promise = require('bluebird');
4-
var acorn = require('acorn');
4+
var parse = require('acorn/acorn_loose').parse_dammit;
55
var acornWalk = require('acorn/util/walk');
66
var fs = Promise.promisifyAll(require('fs'));
77
var utils = require('./utils');
@@ -122,7 +122,7 @@ Detector.prototype._parseContents = function(filePath, contents) {
122122
contents = contents.replace('#!', '//');
123123
}
124124

125-
var syntaxTree = acorn.parse(contents, {
125+
var syntaxTree = parse(contents, {
126126
ecmaVersion: 6,
127127
allowReturnOutsideFunction: true,
128128
locations: true,
@@ -196,7 +196,7 @@ Detector.prototype._detectMagicNumbers = function(syntaxTree, contents,
196196
}
197197

198198
self.emit('found', {
199-
value: node.value,
199+
value: (node.raw !== undefined) ? node.raw : node.value,
200200
file: node.loc.source,
201201
fileLength: fileLength,
202202
lineNumber: node.loc.start.line,

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
},
2424
"dependencies": {
2525
"bluebird": "*",
26-
"acorn": "0.9.0",
26+
"acorn": "0.10.0",
2727
"commander": "*",
2828
"node-filepaths": "0.0.2",
2929
"chalk": "*",
@@ -37,6 +37,6 @@
3737
"buddy": "./bin/buddy"
3838
},
3939
"scripts": {
40-
"test": "./node_modules/.bin/mocha --recursive -R spec spec"
40+
"test": "./node_modules/.bin/mocha -R spec spec/unit spec/integration"
4141
}
4242
}

spec/fixtures/hexOctal.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
console.log(0x1A + 0x02);
2+
console.log(071);

spec/fixtures/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var absolutePaths = {};
44
var fixtures = ['emptyFile', 'singleVariable', 'es5', 'secondsInMinute',
55
'lineIgnore', 'blockIgnore', 'objectProperties',
66
'ignore', 'constVariable', 'constObject', 'shebang',
7-
'testFile'];
7+
'testFile', 'hexOctal'];
88

99
absolutePaths.testFileOutput = path.resolve(__dirname, 'testFileOutput');
1010

spec/unit/detectorSpec.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ describe('Detector', function() {
131131

132132
detector.run().then(function() {
133133
expect(found).to.have.length(1);
134-
expect(found[0].value).to.be(60);
134+
expect(found[0].value).to.be('60');
135135
expect(found[0].file.substr(-18)).to.be('secondsInMinute.js');
136136
expect(found[0].startColumn).to.be(9);
137137
expect(found[0].endColumn).to.be(11);
@@ -146,13 +146,26 @@ describe('Detector', function() {
146146
}).catch(done);
147147
});
148148

149+
it('correctly emits hex and octal values', function(done) {
150+
var detector = new Detector([fixtures.hexOctal]);
151+
detector.on('found', listener);
152+
153+
detector.run().then(function() {
154+
expect(found).to.have.length(3);
155+
expect(found[0].value).to.be('0x1A');
156+
expect(found[1].value).to.be('0x02');
157+
expect(found[2].value).to.be('071');
158+
done();
159+
}).catch(done);
160+
});
161+
149162
it('skips unnamed constants within the ignore list', function(done) {
150163
var detector = new Detector([fixtures.ignore], false, [0]);
151164
detector.on('found', listener);
152165

153166
detector.run().then(function() {
154167
expect(found).to.have.length(1);
155-
expect(found[0].value).to.be(1);
168+
expect(found[0].value).to.be('1');
156169
done();
157170
}).catch(done);
158171
});
@@ -164,7 +177,7 @@ describe('Detector', function() {
164177
detector.run().then(function() {
165178
expect(found).to.have.length(1);
166179
expect(found[0].lineNumber).to.be(4);
167-
expect(found[0].value).to.be(100);
180+
expect(found[0].value).to.be('100');
168181
done();
169182
}).catch(done);
170183
});
@@ -176,7 +189,7 @@ describe('Detector', function() {
176189

177190
detector.run().then(function() {
178191
expect(found).to.have.length(1);
179-
expect(found[0].value).to.be(10);
192+
expect(found[0].value).to.be('10');
180193
done();
181194
}).catch(done);
182195
});
@@ -187,7 +200,7 @@ describe('Detector', function() {
187200

188201
detector.run().then(function() {
189202
expect(found).to.have.length(1);
190-
expect(found[0].value).to.be(10);
203+
expect(found[0].value).to.be('10');
191204
done();
192205
}).catch(done);
193206
});

0 commit comments

Comments
 (0)