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(axios): ignore commented out lines #1031

Merged
merged 1 commit into from
Nov 13, 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
14 changes: 13 additions & 1 deletion packages/core/src/generators/mutator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ export const generateMutator = async ({
const importName = mutator.name ? mutator.name : `${name}Mutator`;
const importPath = mutator.path;

const rawFile = await fs.readFile(importPath, 'utf8');
let rawFile = await fs.readFile(importPath, 'utf8');

rawFile = removeComments(rawFile);

const hasErrorType =
rawFile.includes('export type ErrorType') ||
Expand Down Expand Up @@ -146,6 +148,16 @@ const getEcmaVersion = (
}
};

const removeComments = (file: string) => {
// Regular expression to match single-line and multi-line comments
const commentRegex = /\/\/.*|\/\*[\s\S]*?\*\//g;

// Remove comments from the rawFile string
const cleanedFile = file.replace(commentRegex, '');

return cleanedFile;
};

const parseFile = (
file: string,
name: string,
Expand Down
9 changes: 7 additions & 2 deletions packages/core/src/utils/assertion.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ReferenceObject, SchemaObject } from 'openapi3-ts';
import validatorIsUrl from 'validator/lib/isURL';
import { SchemaType, Verbs } from '../types';
import { extname } from './path';

Expand Down Expand Up @@ -81,5 +80,11 @@ export const isRootKey = (specKey: string, target: string) => {
};

export const isUrl = (str: string) => {
return validatorIsUrl(str, { require_tld: false });
let givenURL;
try {
givenURL = new URL(str);
} catch (error) {
return false;
}
return givenURL.protocol === 'http:' || givenURL.protocol === 'https:';
};