@truebill/eslint-plugin
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -0,1 +1,7 @@ | ||
### 0.0.5 | ||
- Rewrite `disallow-literals-in-express-headers` to use Typescript | ||
type instead of `req.*` for deciding whether to lint or not. | ||
This would make the Rule work for any variable name of your choosing. | ||
### 0.0.4 | ||
@@ -2,0 +8,0 @@ |
@@ -8,6 +8,32 @@ "use strict"; | ||
var _typescript = _interopRequireDefault(require("typescript")); | ||
var tsutils = _interopRequireWildcard(require("tsutils")); | ||
var _common = require("./common"); | ||
const REQUEST_VAR_NAMES = ['req']; | ||
const REQUEST_METHOD_NAMES = ['get', 'set']; | ||
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; } | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
// These are the props we use to verify it is | ||
// an Express Request object | ||
const REQUEST_GET_PROP_FLAGS = { | ||
ip: _typescript.default.SymbolFlags.Property, | ||
url: _typescript.default.SymbolFlags.Property, | ||
originalUrl: _typescript.default.SymbolFlags.Property, | ||
httpVersion: _typescript.default.SymbolFlags.Property | ||
}; // These are the props we use to verify it is | ||
// an Express Response object | ||
const REQUEST_SET_PROP_FLAGS = { | ||
status: _typescript.default.SymbolFlags.Method, | ||
contentType: _typescript.default.SymbolFlags.Method, | ||
redirect: _typescript.default.SymbolFlags.Method, | ||
statusCode: _typescript.default.SymbolFlags.Property, | ||
statusMessage: _typescript.default.SymbolFlags.Property, | ||
writeHead: _typescript.default.SymbolFlags.Method | ||
}; | ||
const strictArrayArgs = (0, _common.createRule)({ | ||
@@ -17,7 +43,10 @@ name: 'disallow-literals-in-express-headers', | ||
messages: { | ||
unexpectedLiteral: 'expected literal value, try using constants for header names' | ||
unexpectedLiteral: 'unexpected literal value, use constants for HTTP header names' | ||
}, | ||
create({ | ||
context | ||
context, | ||
utils: { | ||
getNodeType | ||
} | ||
}) { | ||
@@ -27,3 +56,3 @@ return { | ||
// Ignore non-obj callee or if object isn't "req" or if method is not "get" or "set" | ||
if (node.callee.type !== 'MemberExpression' || node.callee.property.type !== 'Identifier' || node.callee.object.type !== 'Identifier' || !REQUEST_VAR_NAMES.includes(node.callee.object.name) || !REQUEST_METHOD_NAMES.includes(node.callee.property.name)) { | ||
if (node.callee.type !== 'MemberExpression' || node.callee.property.type !== 'Identifier' || node.callee.property.name !== 'get' && node.callee.property.name !== 'set') { | ||
return; | ||
@@ -41,8 +70,44 @@ } | ||
if (firstArg != null && firstArg.type === 'Literal') { | ||
context.report({ | ||
node: firstArg, | ||
messageId: 'unexpectedLiteral' | ||
}); | ||
if (firstArg.type !== 'Literal') { | ||
// Short-circut if already non-literal | ||
return; | ||
} | ||
const requestType = getNodeType(node.callee.object); | ||
if (requestType == null) { | ||
return; | ||
} | ||
let foundProps = 0; | ||
const requestProperties = requestType.getProperties(); | ||
const propsToTestAgainst = node.callee.property.name === 'get' ? REQUEST_GET_PROP_FLAGS : REQUEST_SET_PROP_FLAGS; | ||
for (let i = 0, { | ||
length | ||
} = requestProperties; i < length; i++) { | ||
const requestProperty = requestProperties[i]; | ||
const requestPropertyFlag = propsToTestAgainst[requestProperty.name]; | ||
if (requestPropertyFlag != null) { | ||
foundProps += 1; | ||
const matched = tsutils.isSymbolFlagSet(requestProperty, requestPropertyFlag); | ||
if (!matched) { | ||
// Hard-no. Not an Express object. | ||
return; | ||
} | ||
} | ||
} | ||
if (foundProps !== Object.keys(propsToTestAgainst).length) { | ||
// Not what we're looking for, doesn't have props matching that of | ||
// Express Request | ||
return; | ||
} | ||
context.report({ | ||
node: firstArg, | ||
messageId: 'unexpectedLiteral' | ||
}); | ||
} | ||
@@ -49,0 +114,0 @@ |
@@ -5,3 +5,3 @@ { | ||
"main": "lib/index.js", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"devDependencies": { | ||
@@ -8,0 +8,0 @@ "@babel/core": "^7.8.7", |
16166
368