eslint-plugin-relay
Advanced tools
Comparing version
@@ -52,9 +52,21 @@ /** | ||
const MODULE_NAME_REGEX = /(?:\/|^)(\w+)(?:\.[\w\.]+|\/index\.\w+)$/; | ||
function getModuleName(context) { | ||
const match = context.getFilename().match(MODULE_NAME_REGEX); | ||
if (match) { | ||
return match[1]; | ||
} | ||
return null; | ||
// Copied directly from Relay | ||
function getModuleName(filePath) { | ||
const filename = path.basename(filePath, path.extname(filePath)); | ||
// /path/to/button/index.js -> button | ||
let moduleName = filename === 'index' | ||
? path.basename(path.dirname(filePath)) | ||
: filename; | ||
// Example.ios -> Example | ||
// Example.product.android -> Example | ||
moduleName = moduleName.replace(/(?:\.\w+)+/, ''); | ||
// foo-bar -> fooBar | ||
// Relay compatibility mode splits on _, so we can't use that here. | ||
moduleName = moduleName.replace(/[^a-zA-Z0-9]+(\w?)/g, (match, next) => | ||
next.toUpperCase(), | ||
); | ||
return moduleName; | ||
} | ||
@@ -141,3 +153,3 @@ | ||
} | ||
const moduleName = getModuleName(context); | ||
const moduleName = getModuleName(context.getFilename()); | ||
ast.definitions.forEach(def => { | ||
@@ -236,6 +248,35 @@ if (!def.name) { | ||
} | ||
if (!/react-relay\/compat|RelayCompat/.test(context.getSourceCode().text)) { | ||
if ( | ||
!/react-relay\/compat|RelayCompat/.test(context.getSourceCode().text) | ||
) { | ||
// Only run in for compat mode files | ||
return {}; | ||
} | ||
function isInScope(name) { | ||
var scope = context.getScope(); | ||
var sourceCode = context.getSourceCode(); | ||
var variables = scope.variables; | ||
while (scope.type !== 'global') { | ||
scope = scope.upper; | ||
variables = scope.variables.concat(variables); | ||
} | ||
if (scope.childScopes.length) { | ||
variables = scope.childScopes[0].variables.concat(variables); | ||
// Temporary fix for babel-eslint | ||
if (scope.childScopes[0].childScopes.length) { | ||
variables = scope.childScopes[0].childScopes[0].variables.concat( | ||
variables | ||
); | ||
} | ||
} | ||
for (var i = 0, len = variables.length; i < len; i++) { | ||
if (variables[i].name === name) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
return { | ||
@@ -256,7 +297,3 @@ TaggedTemplateExpression(taggedTemplateExpression) { | ||
const componentName = m[1]; | ||
if ( | ||
context | ||
.getScope(taggedTemplateExpression) | ||
.isUsedName(componentName) | ||
) { | ||
if (isInScope(componentName)) { | ||
// if this variable is defined, mark it as used | ||
@@ -309,3 +346,3 @@ context.markVariableAsUsed(componentName); | ||
case 'OperationDefinition': | ||
const moduleName = getModuleName(context); | ||
const moduleName = getModuleName(context.getFilename()); | ||
const name = definition.name; | ||
@@ -312,0 +349,0 @@ if (!name) { |
{ | ||
"name": "eslint-plugin-relay", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "ESLint plugin for Relay.", | ||
@@ -5,0 +5,0 @@ "main": "eslint-plugin-relay", |
16907
7.4%388
8.68%