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

fix: correctly remove file extension on types import path #742

Merged
merged 2 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ describe('GraphQL statements Formatter', () => {
expect(formattedOutput).toMatchSnapshot();
});

it('Generates formatted output and only remove file extension', () => {
const formattedOutput = new GraphQLStatementsFormatter('typescript', 'queries', '../Components/Data/API.tsx').format(statements);
expect(formattedOutput).toMatchSnapshot();
});

it('Generates formatted output for Flow frontend', () => {
const formattedOutput = new GraphQLStatementsFormatter('flow').format(statements);
expect(formattedOutput).toMatchSnapshot();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`GraphQL statements Formatter Generates formatted output and only remove file extension 1`] = `
"/* tslint:disable */
/* eslint-disable */
// this is an auto generated file. This will be overwritten

import * as APITypes from \\"../Components/Data/API\\";
type GeneratedQuery<InputType, OutputType> = string & {
__generatedQueryInput: InputType;
__generatedQueryOutput: OutputType;
};

export const getProject = /* GraphQL */ \`query GetProject($id: ID!) {
getProject(id: $id) {
id
name
createdAt
updatedAt
}
}
\` as GeneratedQuery<
APITypes.GetProjectQueryVariables,
APITypes.GetProjectQuery
>;
"
`;

exports[`GraphQL statements Formatter Generates formatted output for Angular frontend 1`] = `
"# this is an auto generated file. This will be overwritten

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class GraphQLStatementsFormatter {
this.lintOverrides = [];
this.headerComments = [];
this.typesPath = typesPath
? typesPath.replace(/.ts/i, '')
? typesPath.replace(/\.[^.]+$/, '') // remove file extensions
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use Node's path module for this? path.parse(typesPath).ext will give you the extension to remove; .name will give the name exclusive of the extension, .dir + .name seems like what you want here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about this and thought the regex would be cleaner. But looking at it again path.parse would be more error resistant.

// ensure posix path separators are used
.split(path.win32.sep)
.join(path.posix.sep)
Expand Down