Skip to content

Commit

Permalink
#33 Rule tested
Browse files Browse the repository at this point in the history
  • Loading branch information
gkouziik committed Sep 22, 2019
1 parent d793b38 commit 178e42d
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,70 @@
* @fileoverview detect opion:unsafe in serialize method in serialize-javasript npm package
* @author Gkouziik
*/
"use strict";

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
'use strict'

module.exports = {
meta: {
docs: {
description: "detect opion:unsafe in serialize method in serialize-javasript npm package",
category: "Fill me in",
recommended: false
},
fixable: null, // or "code" or "whitespace"
schema: [
// fill in your schema
]
meta: {
type: 'suggestion',
messages: {
msg: 'detect option:unsafe in serialize'
},
docs: {
description: 'detect opion:unsafe in serialize method in serialize-javasript npm package',
category: 'Possible errors',
recommended: true
},
fixable: null
},

create: function(context) {

// variables should be defined here

//----------------------------------------------------------------------
// Helpers
//----------------------------------------------------------------------

// any helper functions should go here or else delete this section

//----------------------------------------------------------------------
// Public
//----------------------------------------------------------------------

return {
create: function (context) {
var serializeVar

// give me methods
return {
'VariableDeclaration': function (node) {
if (node.declarations[0].init.hasOwnProperty('callee')) {
if (node.declarations[0].init.callee.hasOwnProperty('name')) {
if (
node.declarations[0].init.callee.name === 'require' &&
node.declarations[0].init.arguments[0].type === 'Literal' &&
node.declarations[0].init.arguments[0].value === 'serialize-javascript'
) {
serializeVar = node.declarations[0].id.name
console.log(serializeVar)
}
}
}
},
'CallExpression': function (node) {
if (node.callee.type === 'Identifier') {
if (node.callee.name === serializeVar || node.callee.name === 'serialize') {
if (node.arguments.length > 0 && node.arguments[1] != undefined) {
if (node.arguments[1].type === 'ObjectExpression') {
var flag = false
for (var i in node.arguments[1].properties) {
if (node.arguments[1].properties[i].key.name === 'unsafe') {
if (node.arguments[1].properties[i].value.raw === 'true') {
flag = true
break
}
}
}
if (flag == true) {
context.report({
node: node,
messageId: 'msg',
loc: {
start: node.arguments[1].loc.start,
end: node.arguments[1].loc.end
}

};
})
}
}
}
}
}
}
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,29 @@
* @fileoverview detect opion:unsafe in serialize method in serialize-javasript npm package
* @author Gkouziik
*/
"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

var rule = require("../../../lib/rules/detect-option-unsafe-in-serialize-javascript-npm-package"),

RuleTester = require("eslint").RuleTester;


//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

var ruleTester = new RuleTester();
ruleTester.run("detect-option-unsafe-in-serialize-javascript-npm-package", rule, {

valid: [

// give me some code that won't trigger a warning
],

invalid: [
{
code: "",
errors: [{
message: "Fill me in.",
type: "Me too"
}]
}
]
});
'use strict'

var rule = require('../../../lib/rules/detect-option-unsafe-in-serialize-javascript-npm-package')
var RuleTester = require('eslint').RuleTester
const ERROR_MSG = 'detect option:unsafe in serialize'
const valid = 'serialize({str: "string", obj : {foo: "foo"},arr : [1, 2, 3],bool : true})'
const invalid = 'serialize(object,{unsafe:true})'

var ruleTester = new RuleTester()
ruleTester.run('detect-option-unsafe-in-serialize-javascript-npm-package', rule, {

valid: [{

code: valid
}
],

invalid: [
{
code: invalid,
errors: [{
message: ERROR_MSG
}]
}
]
})

0 comments on commit 178e42d

Please sign in to comment.