Skip to content

Commit

Permalink
fix(generator): add prefix to es5keyword parameters (#356)
Browse files Browse the repository at this point in the history
* fix(types): remove generic from axios query function generator (#284)

* fix(generator): add prefix to es5keyword parameters
  • Loading branch information
yekver authored Apr 7, 2022
1 parent ee09ac8 commit 940c729
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
5 changes: 2 additions & 3 deletions src/core/generators/verbsOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { getProps } from '../getters/props';
import { getQueryParams } from '../getters/queryParams';
import { getResponse } from '../getters/response';
import { generateMutator } from './mutator';
import { sanitize } from '../../utils/string';

const generateVerbOptions = async ({
verb,
Expand Down Expand Up @@ -79,9 +80,7 @@ const generateVerbOptions = async ({
const overriddenOperationName = overrideOperationName
? overrideOperationName(operation, route, verb)
: camel(operationId);
const operationName = keyword.isKeywordES5(overriddenOperationName, true)
? `_${overriddenOperationName}`
: overriddenOperationName;
const operationName = sanitize(overriddenOperationName, { es5keyword: true })

const response = await getResponse(responses, operationName, context);

Expand Down
4 changes: 2 additions & 2 deletions src/core/getters/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const getParams = ({
params.map(async (p) => {
const pathParam = pathParams.find(
({ parameter }) =>
camel(sanitize(parameter.name, { underscore: true, dash: true })) ===
sanitize(camel(parameter.name), { es5keyword: true, underscore: true, dash: true }) ===
p,
);

Expand All @@ -58,7 +58,7 @@ export const getParams = ({
schema,
} = pathParam.parameter;

const name = sanitize(camel(nameWithoutSanitize));
const name = sanitize(camel(nameWithoutSanitize), { es5keyword:true });

if (!schema) {
return {
Expand Down
2 changes: 1 addition & 1 deletion src/core/getters/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const getRoutePath = (path: string) => {
const matches = path.match(/([^{]*){?([\w_-]*)}?(.*)/)
if (!matches?.length) return path // impossible due to regexp grouping here, but for TS
const prev = matches[1]
const param = camel(sanitize(matches[2], { underscore: true, dash: true, dot: true }))
const param = sanitize(camel(matches[2]), { es5keyword: true, underscore: true, dash: true, dot: true })
const next: string = hasParam(matches[3]) ? getRoutePath(matches[3]) : matches[3]
if (hasParam(path)) {
return `${prev}\${${param}}${next}`
Expand Down
7 changes: 7 additions & 0 deletions src/utils/string.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { keyword } from 'esutils';
import get from 'lodash.get';
import {
isBoolean,
Expand Down Expand Up @@ -52,13 +53,15 @@ export const sanitize = (
underscore?: string | true;
dot?: string | true;
dash?: string | true;
es5keyword?: boolean
},
) => {
const {
whitespace = '',
underscore = '',
dot = '',
dash = '',
es5keyword = false
} = options || {};
let newValue = value.replace(/[^\w\s.-]/g, '');

Expand All @@ -78,6 +81,10 @@ export const sanitize = (
newValue = newValue.replace(/[-]/g, dash);
}

if (es5keyword) {
newValue = keyword.isKeywordES5(newValue, true) ? `_${newValue}` : newValue
}

return newValue;
};

Expand Down

1 comment on commit 940c729

@vercel
Copy link

@vercel vercel bot commented on 940c729 Apr 7, 2022

Choose a reason for hiding this comment

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

Please sign in to comment.