Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Account for missing filepath #104

Merged
merged 1 commit into from
Jun 1, 2023
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
9 changes: 4 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ export interface PrettierOptions
/** Subset of options that need to be normalized, or affect normalization */
export type NormalizableOptions = Pick<
PrettierOptions,
| 'importOrder'
| 'importOrderParserPlugins'
| 'importOrderTypeScriptVersion'
| 'filepath'
>;
'importOrder' | 'importOrderParserPlugins' | 'importOrderTypeScriptVersion'
> &
// filepath can be undefined when running prettier via the api on text input
Pick<Partial<PrettierOptions>, 'filepath'>;

export type ChunkType = typeof chunkTypeOther | typeof chunkTypeUnsortable;
export type FlavorType =
Expand Down
19 changes: 19 additions & 0 deletions src/utils/__tests__/normalize-plugin-options.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,23 @@ describe('examineAndNormalizePluginOptions', () => {
provideGapAfterTopOfFileComments: false,
});
});
test('it should not have a problem with a missing filepath', () => {
expect(
examineAndNormalizePluginOptions({
importOrder: [],
importOrderParserPlugins: [],
importOrderTypeScriptVersion: '1.0.0',
filepath: undefined,
} as NormalizableOptions),
).toEqual({
hasAnyCustomGroupSeparatorsInImportOrder: false,
importOrder: [
BUILTIN_MODULES_REGEX_STR,
THIRD_PARTY_MODULES_SPECIAL_WORD,
],
importOrderCombineTypeAndValueImports: true,
plugins: [],
provideGapAfterTopOfFileComments: false,
});
});
});
2 changes: 1 addition & 1 deletion src/utils/normalize-plugin-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export function examineAndNormalizePluginOptions(

let plugins = getExperimentalParserPlugins(importOrderParserPlugins);
// Do not inject jsx plugin for non-jsx ts files
if (filepath.endsWith('.ts')) {
if (filepath?.endsWith('.ts')) {
plugins = plugins.filter((p) => p !== 'jsx');
}

Expand Down