Skip to content

Commit 11e5846

Browse files
committed
remove global _ const
1 parent 683b3e5 commit 11e5846

File tree

7 files changed

+10
-37
lines changed

7 files changed

+10
-37
lines changed

packages/insomnia/src/common/render.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import type { WebSocketRequest } from '../models/websocket-request';
1717
import { isWorkspace, type Workspace } from '../models/workspace';
1818
import { getOrInheritAuthentication, getOrInheritHeaders } from '../network/network';
1919
import { RenderError } from '../templating/render-error';
20-
import { NUNJUCKS_TEMPLATE_GLOBAL_PROPERTY_NAME, render as templateRendering } from '../templating/template-rendering';
20+
import { render as templateRendering } from '../templating/template-rendering';
2121
import type {
2222
BaseRenderContext,
2323
BaseRenderContextOptions,
@@ -442,7 +442,7 @@ export async function getRenderContext({
442442
}
443443
}
444444

445-
const inKey = NUNJUCKS_TEMPLATE_GLOBAL_PROPERTY_NAME;
445+
const inKey = '_';
446446

447447
if (rootGlobalEnvironment) {
448448
getKeySource(rootGlobalEnvironment.data || {}, inKey, 'rootGlobal');

packages/insomnia/src/templating/template-rendering.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { extractUndefinedVariableKey, RenderError } from './render-error';
1010
export const RENDER_ALL = 'all';
1111
export const RENDER_VARS = 'variables';
1212
export const RENDER_TAGS = 'tags';
13-
export const NUNJUCKS_TEMPLATE_GLOBAL_PROPERTY_NAME = '_';
1413

1514
type NunjucksEnvironment = Environment & {
1615
extensions: Record<string, any>;
@@ -48,7 +47,7 @@ export function render(
4847
// context needs to exist on the root for the old templating syntax, and in _ for the new templating syntax
4948
// old: {{ arr[0].prop }}
5049
// new: {{ _['arr-name-with-dash'][0].prop }}
51-
const templatingContext = { ...context, [NUNJUCKS_TEMPLATE_GLOBAL_PROPERTY_NAME]: context };
50+
const templatingContext = { ...context, ['_']: context };
5251
const path = config.path || null;
5352
const renderMode = config.renderMode || RENDER_ALL;
5453
return new Promise<string | null>(async (resolve, reject) => {

packages/insomnia/src/ui/components/editors/__tests__/environment-editor.test.ts

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// @vitest-environment jsdom
22
import { describe, expect, it } from 'vitest';
33

4-
import { NUNJUCKS_TEMPLATE_GLOBAL_PROPERTY_NAME } from '../../../../templating/template-rendering';
54
import { checkNestedKeys, ensureKeyIsValid } from '../environment-utils';
65

76
describe('ensureKeyIsValid()', () => {
@@ -21,28 +20,11 @@ describe('ensureKeyIsValid()', () => {
2120
expect(ensureKeyIsValid(key, true)).toBe(`"${key}" is a reserved key`);
2221
});
2322

24-
it.each([
25-
'_',
26-
'a',
27-
'ab',
28-
'a$',
29-
'a$b',
30-
'a-b',
31-
`a${NUNJUCKS_TEMPLATE_GLOBAL_PROPERTY_NAME}b`,
32-
`${NUNJUCKS_TEMPLATE_GLOBAL_PROPERTY_NAME}ab`,
33-
])('"%s" should be valid as a nested key', key => {
23+
it.each(['_', 'a', 'ab', 'a$', 'a$b', 'a-b', `a_b`, `_ab`])('"%s" should be valid as a nested key', key => {
3424
expect(ensureKeyIsValid(key, false)).toBe(null);
3525
});
3626

37-
it.each([
38-
'a',
39-
'ab',
40-
'a$',
41-
'a$b',
42-
'a-b',
43-
`a${NUNJUCKS_TEMPLATE_GLOBAL_PROPERTY_NAME}b`,
44-
`${NUNJUCKS_TEMPLATE_GLOBAL_PROPERTY_NAME}ab`,
45-
])('"%s" should be valid as a root value', key => {
27+
it.each(['a', 'ab', 'a$', 'a$b', 'a-b', `a_b`, `_ab`])('"%s" should be valid as a root value', key => {
4628
expect(ensureKeyIsValid(key, true)).toBe(null);
4729
});
4830
});

packages/insomnia/src/ui/components/editors/environment-utils.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
vaultEnvironmentPath,
77
vaultEnvironmentRuntimePath,
88
} from '../../../models/environment';
9-
import { NUNJUCKS_TEMPLATE_GLOBAL_PROPERTY_NAME } from '../../../templating/template-rendering';
109
import { showModal } from '../modals';
1110
import { AlertModal } from '../modals/alert-modal';
1211
import { AskModal } from '../modals/ask-modal';
@@ -20,8 +19,8 @@ export const ensureKeyIsValid = (key: string, isRoot: boolean): string | null =>
2019
return `"${key}" cannot begin with '$' or contain a '.'`;
2120
}
2221

23-
if (key === NUNJUCKS_TEMPLATE_GLOBAL_PROPERTY_NAME && isRoot) {
24-
return `"${NUNJUCKS_TEMPLATE_GLOBAL_PROPERTY_NAME}" is a reserved key`;
22+
if (key === '_' && isRoot) {
23+
return `"_" is a reserved key`;
2524
}
2625

2726
if (key === vaultEnvironmentPath && isRoot) {

packages/insomnia/src/ui/components/templating/variable-editor.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React, { type FC, useEffect, useMemo, useState } from 'react';
22

33
import { vaultEnvironmentMaskValue, vaultEnvironmentRuntimePath } from '../../../models/environment';
4-
import { NUNJUCKS_TEMPLATE_GLOBAL_PROPERTY_NAME } from '../../../templating/template-rendering';
54
import type { RenderPurpose } from '../../../templating/types';
65
import { useNunjucks } from '../../context/nunjucks/use-nunjucks';
76

@@ -23,11 +22,7 @@ export const VariableEditor: FC<Props> = ({ onChange, defaultValue }) => {
2322
const [error, setError] = useState('');
2423
const isVaultVariable =
2524
selected &&
26-
selected
27-
.replace('{{', '')
28-
.replace('}}', '')
29-
.trim()
30-
.startsWith(`${NUNJUCKS_TEMPLATE_GLOBAL_PROPERTY_NAME}.${vaultEnvironmentRuntimePath}`) &&
25+
selected.replace('{{', '').replace('}}', '').trim().startsWith(`_.${vaultEnvironmentRuntimePath}`) &&
3126
preview === vaultEnvironmentMaskValue;
3227

3328
useEffect(() => {

packages/insomnia/src/ui/context/nunjucks/use-nunjucks.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { useCallback } from 'react';
33
import { getRenderContext, getRenderContextAncestors, render } from '~/common/render';
44
import { useWorkspaceLoaderData } from '~/routes/organization.$organizationId.project.$projectId.workspace.$workspaceId';
55
import { useRequestLoaderData } from '~/routes/organization.$organizationId.project.$projectId.workspace.$workspaceId.debug.request.$requestId';
6-
import { NUNJUCKS_TEMPLATE_GLOBAL_PROPERTY_NAME } from '~/templating/template-rendering';
76
import type { HandleRender, RenderContextOptions } from '~/templating/types';
87
import { getKeys } from '~/templating/utils';
98

@@ -46,7 +45,7 @@ export const useNunjucks = (options?: UseNunjucksOptions) => {
4645
contextCacheKey && getRenderContextPromiseCache[contextCacheKey]
4746
? await getRenderContextPromiseCache[contextCacheKey]
4847
: await fetchRenderContext();
49-
const keys = getKeys(context, NUNJUCKS_TEMPLATE_GLOBAL_PROPERTY_NAME);
48+
const keys = getKeys(context, '_');
5049
return { context, keys };
5150
},
5251
[fetchRenderContext],

packages/insomnia/src/ui/worker/template-rendering.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { fetchFromTemplateWorkerDatabase, NunjucksExtension } from '~/ui/worker/
1010
export const RENDER_ALL = 'all';
1111
export const RENDER_VARS = 'variables';
1212
export const RENDER_TAGS = 'tags';
13-
export const NUNJUCKS_TEMPLATE_GLOBAL_PROPERTY_NAME = '_';
1413

1514
type NunjucksEnvironment = Environment & {
1615
extensions: Record<string, any>;
@@ -48,7 +47,7 @@ export function render(
4847
// context needs to exist on the root for the old templating syntax, and in _ for the new templating syntax
4948
// old: {{ arr[0].prop }}
5049
// new: {{ _['arr-name-with-dash'][0].prop }}
51-
const templatingContext = { ...context, [NUNJUCKS_TEMPLATE_GLOBAL_PROPERTY_NAME]: context };
50+
const templatingContext = { ...context, ['_']: context };
5251
const path = config.path || null;
5352
const renderMode = config.renderMode || RENDER_ALL;
5453
return new Promise<string | null>(async (resolve, reject) => {

0 commit comments

Comments
 (0)