Skip to content

Commit 0eaab36

Browse files
authored
fix(code-loader): do not load illegal files (#3190)
1 parent 4dfa5c2 commit 0eaab36

File tree

3 files changed

+44
-10
lines changed

3 files changed

+44
-10
lines changed

packages/loaders/code-file/src/index.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,23 +117,22 @@ export class CodeFileLoader implements Loader<CodeFileLoaderOptions> {
117117
return globby.sync([glob, ...ignores.map(v => `!${v}`).map(v => unixify(v))], createGlobbyOptions(options));
118118
}
119119

120-
async load(pointer: string, options: CodeFileLoaderOptions): Promise<Source[] | null> {
120+
async load(pointer: string, options: CodeFileLoaderOptions): Promise<Source[]> {
121121
options = this.getMergedOptions(options);
122+
const finalResult: Source[] = [];
123+
122124
if (isGlob(pointer)) {
123125
const resolvedPaths = await this.resolveGlobs(pointer, options);
124-
const finalResult: Source[] = [];
125126
await Promise.all(
126127
resolvedPaths.map(async path => {
127-
if (await this.canLoad(path, options)) {
128-
const result = await this.handleSinglePath(path, options);
129-
result?.forEach(result => finalResult.push(result));
130-
}
128+
finalResult.push(...(await this.handleSinglePath(path, options)));
131129
})
132130
);
133-
return finalResult;
131+
} else {
132+
finalResult.push(...(await this.handleSinglePath(pointer, options)));
134133
}
135134

136-
return this.handleSinglePath(pointer, options);
135+
return finalResult;
137136
}
138137

139138
loadSync(pointer: string, options: CodeFileLoaderOptions): Source[] | null {
@@ -153,7 +152,11 @@ export class CodeFileLoader implements Loader<CodeFileLoaderOptions> {
153152
return this.handleSinglePathSync(pointer, options);
154153
}
155154

156-
async handleSinglePath(location: string, options: CodeFileLoaderOptions): Promise<Source[] | null> {
155+
async handleSinglePath(location: string, options: CodeFileLoaderOptions): Promise<Source[]> {
156+
if (!(await this.canLoad(location, options))) {
157+
return [];
158+
}
159+
157160
options = this.getMergedOptions(options);
158161
const normalizedFilePath = ensureAbsolutePath(location, options);
159162

@@ -200,10 +203,13 @@ export class CodeFileLoader implements Loader<CodeFileLoaderOptions> {
200203
throw errors[0];
201204
}
202205

203-
return null;
206+
return [];
204207
}
205208

206209
handleSinglePathSync(location: string, options: CodeFileLoaderOptions): Source[] | null {
210+
if (!this.canLoadSync(location, options)) {
211+
return [];
212+
}
207213
options = this.getMergedOptions(options);
208214
const normalizedFilePath = ensureAbsolutePath(location, options);
209215

packages/loaders/code-file/tests/load-from-code-file.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,19 @@ describe('loadFromCodeFile', () => {
6363

6464
expect(doc?.kind).toEqual('Document');
6565
});
66+
67+
68+
it('does not try to load single file it cannot load', async () => {
69+
const loader = new CodeFileLoader({
70+
pluckConfig: {
71+
skipIndent: true
72+
}
73+
})
74+
const loaded = await loader.load('./test-files/other.graphql', {
75+
cwd: __dirname,
76+
});
77+
expect(loaded).toEqual([])
78+
})
6679
});
6780

6881
describe('loadFromCodeFileSync', () => {
@@ -174,4 +187,16 @@ describe('loadFromCodeFileSync', () => {
174187
"
175188
`);
176189
})
190+
191+
it('does not try to load single file it cannot load', async () => {
192+
const loader = new CodeFileLoader({
193+
pluckConfig: {
194+
skipIndent: true
195+
}
196+
})
197+
const loaded = loader.loadSync('./test-files/other.graphql', {
198+
cwd: __dirname,
199+
});
200+
expect(loaded).toEqual([])
201+
})
177202
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
query {
2+
foo
3+
}

0 commit comments

Comments
 (0)