-
Notifications
You must be signed in to change notification settings - Fork 149
Created init rule #341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Created init rule #341
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { INIT_NULL_ARG } from 'messages/javascript'; | ||
import { getNodeReference } from 'utils'; | ||
|
||
export function init_null_arg(context) { | ||
return { | ||
CallExpression: function(node) { | ||
// Check if what's being called is nsiTransferable.init() | ||
if (typeof node.callee !== 'undefined') { | ||
let nodeReference = getNodeReference(context, node.callee); | ||
let nodeObject = getNodeReference(context, nodeReference.object); | ||
|
||
if (nodeObject === undefined) { | ||
return; | ||
} | ||
if (nodeObject.name === 'nsITransferable' && | ||
nodeReference.property.name === 'init') { | ||
|
||
if (node.arguments.length > 0) { | ||
// Get the reference to the first arg and check if it's null. | ||
let arg = getNodeReference(context, node.arguments[0]); | ||
if (arg.value === null) { | ||
return context.report({node: node, message: INIT_NULL_ARG.code}); | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
}; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import JavaScriptScanner from 'scanners/javascript'; | ||
import { VALIDATION_WARNING } from 'const'; | ||
import * as messages from 'messages'; | ||
|
||
describe('init_null_arg', () => { | ||
it('should not allow init called with null', () => { | ||
var code = 'nsITransferable.init(null);'; | ||
var jsScanner = new JavaScriptScanner(code, 'badcode.js'); | ||
|
||
return jsScanner.scan() | ||
.then((validationMessages) => { | ||
assert.equal(validationMessages.length, 1); | ||
assert.equal(validationMessages[0].code, messages.INIT_NULL_ARG.code); | ||
assert.equal(validationMessages[0].type, VALIDATION_WARNING); | ||
}); | ||
}); | ||
|
||
it('should not allow init called with a null ref', () => { | ||
var code = 'var foo = null; nsITransferable.init(foo);'; | ||
var jsScanner = new JavaScriptScanner(code, 'badcode.js'); | ||
|
||
return jsScanner.scan() | ||
.then((validationMessages) => { | ||
assert.equal(validationMessages.length, 1); | ||
assert.equal(validationMessages[0].code, messages.INIT_NULL_ARG.code); | ||
assert.equal(validationMessages[0].type, VALIDATION_WARNING); | ||
}); | ||
}); | ||
|
||
it('should allow init called with a non-null arg', () => { | ||
var code = 'nsITransferable.init();'; | ||
var jsScanner = new JavaScriptScanner(code, 'badcode.js'); | ||
|
||
return jsScanner.scan() | ||
.then((validationMessages) => { | ||
assert.equal(validationMessages.length, 0); | ||
}); | ||
}); | ||
|
||
it('should not allow init called with an aliased varaible and null', () => { | ||
var code = 'var foo = nsITransferable; foo.init(null);'; | ||
var jsScanner = new JavaScriptScanner(code, 'badcode.js'); | ||
|
||
return jsScanner.scan() | ||
.then((validationMessages) => { | ||
assert.equal(validationMessages.length, 1); | ||
assert.equal(validationMessages[0].code, messages.INIT_NULL_ARG.code); | ||
assert.equal(validationMessages[0].type, VALIDATION_WARNING); | ||
}); | ||
}); | ||
|
||
it('should not allow nsITransferable.init called aliased with null', () => { | ||
var code = 'var foo = nsITransferable.init; foo(null);'; | ||
var jsScanner = new JavaScriptScanner(code, 'badcode.js'); | ||
|
||
return jsScanner.scan() | ||
.then((validationMessages) => { | ||
assert.equal(validationMessages.length, 1); | ||
assert.equal(validationMessages[0].code, messages.INIT_NULL_ARG.code); | ||
assert.equal(validationMessages[0].type, VALIDATION_WARNING); | ||
}); | ||
}); | ||
|
||
it('should allow nsITransferable.init called aliased without null', () => { | ||
var code = 'var foo = nsITransferable.init; foo();'; | ||
var jsScanner = new JavaScriptScanner(code, 'badcode.js'); | ||
|
||
return jsScanner.scan() | ||
.then((validationMessages) => { | ||
assert.equal(validationMessages.length, 0); | ||
}); | ||
}); | ||
|
||
it('should allow init called with an aliased varaible but not null', () => { | ||
var code = 'var foo = nsITransferable; foo.init();'; | ||
var jsScanner = new JavaScriptScanner(code, 'badcode.js'); | ||
|
||
return jsScanner.scan() | ||
.then((validationMessages) => { | ||
assert.equal(validationMessages.length, 0); | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, can you add one more test to show it catches this?