Skip to content

Commit

Permalink
Add support for graphql imports
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-goformative committed Jun 3, 2022
1 parent 3776553 commit 82625aa
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/component.json
Expand Up @@ -4,6 +4,7 @@
"coffee",
"es6",
"es7",
"graphql",
"jsx",
"sass",
"typescript",
Expand Down
1 change: 1 addition & 0 deletions src/constants.js
Expand Up @@ -52,6 +52,7 @@ export const defaultOptions = {
'**/*.coffee': availableParsers.coffee,
'**/*.litcoffee': availableParsers.coffee,
'**/*.coffee.md': availableParsers.coffee,
'**/*.graphql': availableParsers.graphql,
'**/*.ts': availableParsers.typescript,
'**/*.tsx': availableParsers.typescript,
'**/*.sass': availableParsers.sass,
Expand Down
23 changes: 23 additions & 0 deletions src/parser/graphql.js
@@ -0,0 +1,23 @@
const { readFileSync } = require('fs');
const requirePackageName = require('require-package-name');
const JSON5 = require('json5');

export default function graphqlParser(filePath) {
const foundDeps = [];
const content = readFileSync(filePath, { encoding: 'utf8' });
const lines = content.split(/\r\n|\r|\n/);
lines.some((line) => {
if (line[0] === '#' && line.slice(1).split(' ')[0] === 'import') {
const importFileExpr = line.slice(1).split(' ')[1];
if (importFileExpr) {
// Need to support single and double quotes, so use JSON5
const importFile = JSON5.parse(importFileExpr);
if (importFile && typeof importFile === 'string') {
foundDeps.push(importFile);
}
}
}
return line.length !== 0 && line[0] !== '#';
});
return foundDeps.map((p) => requirePackageName(p)).filter(Boolean);
}
9 changes: 9 additions & 0 deletions test/fake_modules/graphql/package.json
@@ -0,0 +1,9 @@
{
"name": "graphql-depcheck-test",
"version": "1.0.0",
"dependencies": {
"unused": "*",
"@scope/ok": "*",
"ok": "*"
}
}
5 changes: 5 additions & 0 deletions test/fake_modules/graphql/test.graphql
@@ -0,0 +1,5 @@
#import "./foo.graphql"
#import "ok/test.graphql"
#import "@scope/ok/test.graphql"
#import "@scope/missing/missing.graphql"
#import "missing/missing.graphql"
20 changes: 20 additions & 0 deletions test/spec.js
Expand Up @@ -868,4 +868,24 @@ export default [
},
expectedErrorCode: -1,
},
{
name: 'find dependencies in graphql files',
module: 'graphql',
options: {},
expected: {
dependencies: ['unused'],
devDependencies: [],
missing: {
'@scope/missing': ['test.graphql'],
missing: ['test.graphql'],
},
using: {
'@scope/missing': ['test.graphql'],
'@scope/ok': ['test.graphql'],
missing: ['test.graphql'],
ok: ['test.graphql'],
},
},
expectedErrorCode: -1,
},
];

0 comments on commit 82625aa

Please sign in to comment.