Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions .eslintrc.json

This file was deleted.

16 changes: 16 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {defineConfig} from 'eslint/config';
import js from '@eslint/js';
import globals from 'globals';

export default defineConfig([
js.configs.recommended,
{
languageOptions: {
globals: globals.node
},
rules: {
'prefer-const': 'error',
'no-unused-vars': ['error', {argsIgnorePattern: '^_', caughtErrors: 'none'}],
}
}
]);
15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@
"graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
},
"devDependencies": {
"@typescript-eslint/parser": "^6.21.0",
"babel-eslint": "^10.1.0",
"eslint": "^7.8.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-prettier": "^3.1.4",
"@babel/core": "^7.26.10",
"@babel/eslint-parser": "^7.27.0",
"@babel/preset-flow": "^7.25.9",
"@babel/preset-react": "^7.26.3",
"@eslint/js": "^9.24.0",
"@typescript-eslint/parser": "^8.29.1",
"eslint": "^9.24.0",
"globals": "^16.0.0",
"mocha": "^9.1.3",
"prettier": "^2.4.1",
"typescript": "^4.5.2"
"typescript": "^5.8.3"
}
}
20 changes: 15 additions & 5 deletions src/rule-compat-uses-vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,18 @@ module.exports = {
}
},
create(context) {
const sourceCode = context.sourceCode ?? context.getSourceCode();
if (!shouldLint(context)) {
return {};
}
if (!/react-relay\/compat|RelayCompat/.test(context.getSourceCode().text)) {
if (!/react-relay\/compat|RelayCompat/.test(sourceCode.text)) {
// Only run in for compat mode files
return {};
}
function isInScope(name) {
var scope = context.getScope();
var scope = sourceCode.getScope
? sourceCode.getScope(context)
: context.getScope();
var variables = scope.variables;

while (scope.type !== 'global') {
Expand Down Expand Up @@ -90,8 +93,13 @@ module.exports = {
);
if (isInScope(componentName)) {
// if this variable is defined, mark it as used
context.markVariableAsUsed(componentName);
} else if (componentName === getModuleName(context.getFilename())) {
sourceCode.markVariableAsUsed
? sourceCode.markVariableAsUsed(componentName, spreadNode)
: context.markVariableAsUsed(componentName);
} else if (
componentName ===
getModuleName(context.filename ?? context.getFilename())
) {
if (!validateInlineDirective(spreadNode)) {
context.report({
message:
Expand Down Expand Up @@ -120,7 +128,9 @@ module.exports = {
loc: loc
});
}
context.markVariableAsUsed(propName);
sourceCode.markVariableAsUsed
? sourceCode.markVariableAsUsed(propName, spreadNode)
: context.markVariableAsUsed(propName);
} else {
// otherwise, yell about this needed to be defined
context.report({
Expand Down
13 changes: 7 additions & 6 deletions src/rule-generated-flow-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ function getPropTypeProperty(
propName,
visitedProps = new Set()
) {
const sourceCode = context.sourceCode ?? context.getSourceCode();
if (propType == null || visitedProps.has(propType)) {
return null;
}
Expand Down Expand Up @@ -117,10 +118,7 @@ function getPropTypeProperty(
tokenIndex++;
}

if (
context.getSourceCode().getFirstToken(property, tokenIndex).value ===
propName
) {
if (sourceCode.getFirstToken(property, tokenIndex).value === propName) {
return property;
}
}
Expand Down Expand Up @@ -303,6 +301,7 @@ module.exports = {
]
},
create(context) {
const sourceCode = context.sourceCode ?? context.getSourceCode();
if (!shouldLint(context)) {
return {};
}
Expand All @@ -324,8 +323,10 @@ module.exports = {
}
if (arg.type === 'Identifier') {
const name = arg.name;
let scope = context.getScope();
while (scope && scope.type != 'global') {
let scope = sourceCode.getScope
? sourceCode.getScope(arg)
: context.getScope();
while (scope != null && scope.type != 'global') {
for (const variable of scope.variables) {
if (variable.name === name) {
const definition = variable.defs.find(
Expand Down
3 changes: 2 additions & 1 deletion src/rule-generated-typescript-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ module.exports = {
]
},
create(context) {
const sourceCode = context.sourceCode ?? context.getSourceCode();
if (!shouldLint(context)) {
return {};
}
Expand All @@ -350,7 +351,7 @@ module.exports = {
}
if (arg.type === 'Identifier') {
const name = arg.name;
let scope = context.getScope();
let scope = sourceCode.getScope ? sourceCode.getScope(arg) : context.getScope();
while (scope != null) {
for (const variable of scope.variables) {
if (variable.name === name) {
Expand Down
6 changes: 4 additions & 2 deletions src/rule-graphql-naming.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function validateTemplate(context, taggedTemplateExpression, keyName) {
if (!ast) {
return;
}
const moduleName = getModuleName(context.getFilename());
const moduleName = getModuleName(context.filename ?? context.getFilename());
ast.definitions.forEach(def => {
if (!def.name) {
// no name, covered by graphql-naming/TaggedTemplateExpression
Expand Down Expand Up @@ -109,7 +109,9 @@ module.exports = {
ast.definitions.forEach(definition => {
switch (definition.kind) {
case 'OperationDefinition': {
const moduleName = getModuleName(context.getFilename());
const moduleName = getModuleName(
context.filename ?? context.getFilename()
);
const name = definition.name;
if (!name) {
return;
Expand Down
4 changes: 3 additions & 1 deletion src/rule-graphql-syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ module.exports = {
return;
}
try {
const filename = path.basename(context.getFilename());
const filename = path.basename(
context.filename ?? context.getFilename()
);
const ast = parse(new Source(quasi.value.cooked, filename));
if (ast.definitions.length !== 1) {
context.report({
Expand Down
140 changes: 72 additions & 68 deletions src/rule-must-colocate-fragment-spreads.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,79 +113,83 @@ function getGraphQLFragmentDefinitionName(graphQLAst) {
return name;
}

function rule(context) {
const foundImportedModules = [];
const graphqlLiterals = [];
module.exports = {
meta: {
docs: {},
schema: []
},
create(context) {
const foundImportedModules = [];
const graphqlLiterals = [];

return {
'Program:exit'(_node) {
const fragmentsInTheSameModule = [];
graphqlLiterals.forEach(({graphQLAst}) => {
const fragmentName = getGraphQLFragmentDefinitionName(graphQLAst);
if (fragmentName) {
fragmentsInTheSameModule.push(fragmentName);
}
});
graphqlLiterals.forEach(({node, graphQLAst}) => {
const queriedFragments = getGraphQLFragmentSpreads(graphQLAst);
for (const fragment in queriedFragments) {
const matchedModuleName = foundImportedModules.find(name =>
fragment.startsWith(name)
);
if (
!matchedModuleName &&
!fragmentsInTheSameModule.includes(fragment)
) {
context.report({
node,
loc: utils.getLoc(context, node, queriedFragments[fragment]),
message:
`This spreads the fragment \`${fragment}\` but ` +
'this module does not use it directly. If a different module ' +
'needs this information, that module should directly define a ' +
'fragment querying for that data, colocated next to where the ' +
'data is used.\n'
});
return {
'Program:exit'(_node) {
const fragmentsInTheSameModule = [];
graphqlLiterals.forEach(({graphQLAst}) => {
const fragmentName = getGraphQLFragmentDefinitionName(graphQLAst);
if (fragmentName) {
fragmentsInTheSameModule.push(fragmentName);
}
}
});
},

ImportDeclaration(node) {
if (node.importKind === 'value') {
foundImportedModules.push(utils.getModuleName(node.source.value));
}
},
});
graphqlLiterals.forEach(({node, graphQLAst}) => {
const queriedFragments = getGraphQLFragmentSpreads(graphQLAst);
for (const fragment in queriedFragments) {
const matchedModuleName = foundImportedModules.find(name =>
fragment.startsWith(name)
);
if (
!matchedModuleName &&
!fragmentsInTheSameModule.includes(fragment)
) {
context.report({
node,
loc: utils.getLoc(context, node, queriedFragments[fragment]),
message:
`This spreads the fragment \`${fragment}\` but ` +
'this module does not use it directly. If a different module ' +
'needs this information, that module should directly define a ' +
'fragment querying for that data, colocated next to where the ' +
'data is used.\n'
});
}
}
});
},

ImportExpression(node) {
if (node.source.type === 'Literal') {
// Allow dynamic imports like import(`test/${fileName}`); and (path) => import(path);
// These would have node.source.value undefined
foundImportedModules.push(utils.getModuleName(node.source.value));
}
},
ImportDeclaration(node) {
if (node.importKind === 'value') {
foundImportedModules.push(utils.getModuleName(node.source.value));
}
},

CallExpression(node) {
if (node.callee.name !== 'require') {
return;
}
const [source] = node.arguments;
if (source && source.type === 'Literal') {
foundImportedModules.push(utils.getModuleName(source.value));
}
},
ImportExpression(node) {
if (node.source.type === 'Literal') {
// Allow dynamic imports like import(`test/${fileName}`); and (path) => import(path);
// These would have node.source.value undefined
foundImportedModules.push(utils.getModuleName(node.source.value));
}
},

TaggedTemplateExpression(node) {
if (utils.isGraphQLTemplate(node)) {
const graphQLAst = utils.getGraphQLAST(node);
if (!graphQLAst) {
// ignore nodes with syntax errors, they're handled by rule-graphql-syntax
CallExpression(node) {
if (node.callee.name !== 'require') {
return;
}
graphqlLiterals.push({node, graphQLAst});
}
}
};
}
const [source] = node.arguments;
if (source && source.type === 'Literal') {
foundImportedModules.push(utils.getModuleName(source.value));
}
},

module.exports = rule;
TaggedTemplateExpression(node) {
if (utils.isGraphQLTemplate(node)) {
const graphQLAst = utils.getGraphQLAST(node);
if (!graphQLAst) {
// ignore nodes with syntax errors, they're handled by rule-graphql-syntax
return;
}
graphqlLiterals.push({node, graphQLAst});
}
}
};
}
};
37 changes: 22 additions & 15 deletions src/rule-no-future-added-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,27 @@

'use strict';

module.exports = context => {
function validateValue(node) {
context.report(
node,
"Do not use `'%future added value'`. It represents any potential " +
'value that the server might return in the future that the code ' +
'should handle.'
);
}
return {
"Literal[value='%future added value']": validateValue,
module.exports = {
meta: {
docs: {},
schema: []
},
create: context => {
function validateValue(node) {
context.report({
node: node,
message:
"Do not use `'%future added value'`. It represents any potential " +
'value that the server might return in the future that the code ' +
'should handle.'
});
}
return {
"Literal[value='%future added value']": validateValue,

// StringLiteralTypeAnnotations that are not children of a default case
":not(SwitchCase[test=null] StringLiteralTypeAnnotation)StringLiteralTypeAnnotation[value='%future added value']":
validateValue
};
// StringLiteralTypeAnnotations that are not children of a default case
":not(SwitchCase[test=null] StringLiteralTypeAnnotation)StringLiteralTypeAnnotation[value='%future added value']":
validateValue
};
}
};
Loading
Loading