Skip to content

Commit 9605da8

Browse files
author
stephaniekatterwe
committed
feat(transloco): 🎸 update replace-in-flight
Update replace-in-flight to ^8.1.0 to resolve CWE-772 in inflight ✅ Closes: jsverse#768
1 parent e327f3b commit 9605da8

File tree

4 files changed

+144
-88
lines changed

4 files changed

+144
-88
lines changed

libs/transloco-schematics/jest.config.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
import type {Config} from 'jest';
1+
import type { Config } from 'jest';
22

33
export default {
44
displayName: 'transloco-schematics',
55
testEnvironment: 'node',
66
globals: {},
77
transform: {
8-
'^.+\\.[tj]sx?$': [
9-
'ts-jest',
8+
'^.+.(ts|mjs|js|html)$': [
9+
'jest-preset-angular',
1010
{
1111
tsconfig: '<rootDir>/tsconfig.spec.json',
12+
stringifyContentPathRegex: '\\.(html|svg)$',
1213
},
1314
],
1415
},
16+
transformIgnorePatterns: ['node_modules/?!(.\\*.mjs$|replace-in-file)'],
1517
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
1618
coverageDirectory: '../../coverage/libs/transloco-schematics',
1719
preset: '../../jest.preset.js',
Lines changed: 73 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,60 @@
11
// noinspection AngularUndefinedBinding
22

33
import * as nodePath from 'node:path';
4-
import {readFile} from 'node:fs/promises';
4+
import { readFile } from 'node:fs/promises';
55

6-
import {replaceInFile, ReplaceInFileConfig} from 'replace-in-file';
7-
import {glob} from 'glob';
6+
import { replaceInFile, ReplaceInFileConfig } from 'replace-in-file';
7+
import { glob } from 'glob';
88

9-
import {PIPE_IN_BINDING_REGEX, PIPE_REGEX, run} from '../migrate/ngx-translate-migration';
9+
import {
10+
PIPE_IN_BINDING_REGEX,
11+
PIPE_REGEX,
12+
run,
13+
} from '../migrate/ngx-translate-migration';
1014

11-
jest.mock('replace-in-file');
15+
jest.mock('replace-in-file', () => ({
16+
__esModule: true,
17+
replaceInFile: jest.fn(),
18+
}));
1219

1320
describe('ngx-translate migration', () => {
14-
1521
describe('Positive regex tests', () => {
16-
1722
describe('Pipe in binding', () => {
1823
test.each([
1924
{
2025
testCase: `<component [header]="'hello.world' | translate">`,
21-
match: [`]="'hello.world' | translate"`]
26+
match: [`]="'hello.world' | translate"`],
2227
},
2328
{
2429
testCase: `<component [header]="'hello.world' | translate | anotherPipe">`,
25-
match: [`]="'hello.world' | translate | anotherPipe"`]
30+
match: [`]="'hello.world' | translate | anotherPipe"`],
2631
},
2732
{
2833
testCase: `<component [header]="'hello' | translate:params | anotherPipe">`,
29-
match: [`]="'hello' | translate:params | anotherPipe"`]
34+
match: [`]="'hello' | translate:params | anotherPipe"`],
3035
},
3136
{
3237
testCase: `<component [title]="titleMap[reportType] | translate">`,
33-
match: [`]="titleMap[reportType] | translate"`]
38+
match: [`]="titleMap[reportType] | translate"`],
3439
},
3540
{
3641
testCase: `<component [matTooltip]="('foo.bar' | translate) + ': ' + (value | number: '1.0-2')">`,
37-
match: [`]="('foo.bar' | translate) + ': ' + (value | number: '1.0-2')"`]
42+
match: [
43+
`]="('foo.bar' | translate) + ': ' + (value | number: '1.0-2')"`,
44+
],
3845
},
3946
{
4047
testCase: `<compnent [title]="'Hello, ' + ('mom' | translate) | fooBar">`,
41-
match: [`]="'Hello, ' + ('mom' | translate) | fooBar"`]
48+
match: [`]="'Hello, ' + ('mom' | translate) | fooBar"`],
4249
},
4350
{
4451
testCase: `<edge-wizard-step [label]="'Restore Options' | translate" [validatingMessage]="'Processing archive...'|translate"`,
45-
match: [`]="'Restore Options' | translate"`, `]="'Processing archive...'|translate"`]
46-
}
47-
])('Case: $testCase; Match: $match', ({testCase, match}) => {
52+
match: [
53+
`]="'Restore Options' | translate"`,
54+
`]="'Processing archive...'|translate"`,
55+
],
56+
},
57+
])('Case: $testCase; Match: $match', ({ testCase, match }) => {
4858
const regex = new RegExp(PIPE_IN_BINDING_REGEX, 'gm');
4959
const result = testCase.match(regex);
5060

@@ -56,33 +66,35 @@ describe('ngx-translate migration', () => {
5666
test.each([
5767
{
5868
testCase: `<component>{{ "hello.world" | translate }}</component>`,
59-
match: [`{{ "hello.world" | translate }}`]
69+
match: [`{{ "hello.world" | translate }}`],
6070
},
6171
{
6272
testCase: `<component>{{ "hello.world" | translate | anotherPipe | oneMore }}</component>`,
63-
match: [`{{ "hello.world" | translate | anotherPipe | oneMore }}`]
73+
match: [`{{ "hello.world" | translate | anotherPipe | oneMore }}`],
6474
},
6575
{
6676
testCase: `<component>{{ "hello" | translate: { name: 'John' } }}</component>`,
67-
match: [`{{ "hello" | translate: { name: 'John' } }}`]
77+
match: [`{{ "hello" | translate: { name: 'John' } }}`],
6878
},
6979
{
7080
testCase: `<component>{{ titleMap[reportType] | translate }}</component>`,
71-
match: [`{{ titleMap[reportType] | translate }}`]
81+
match: [`{{ titleMap[reportType] | translate }}`],
7282
},
7383
{
7484
testCase: `<component>{{ ('foo.bar' | translate) + ': ' + (value | number: '1.0-2') }}</component>`,
75-
match: [`{{ ('foo.bar' | translate) + ': ' + (value | number: '1.0-2') }}`]
85+
match: [
86+
`{{ ('foo.bar' | translate) + ': ' + (value | number: '1.0-2') }}`,
87+
],
7688
},
7789
{
7890
testCase: `<compnent>{{ 'Hello, ' + ('mom' | translate) | fooBar }}</compnent>`,
79-
match: [`{{ 'Hello, ' + ('mom' | translate) | fooBar }}`]
91+
match: [`{{ 'Hello, ' + ('mom' | translate) | fooBar }}`],
8092
},
8193
{
8294
testCase: `{{"1" | translate}} {{errorCounter}} {{"2" | translate}}`,
83-
match: [`{{"1" | translate}}`, `{{"2" | translate}}`]
84-
}
85-
])('Case: $testCase; Match: $match', ({testCase, match}) => {
95+
match: [`{{"1" | translate}}`, `{{"2" | translate}}`],
96+
},
97+
])('Case: $testCase; Match: $match', ({ testCase, match }) => {
8698
const regex = new RegExp(PIPE_REGEX, 'gm');
8799
const result = testCase.match(regex);
88100

@@ -92,106 +104,109 @@ describe('ngx-translate migration', () => {
92104
});
93105

94106
describe('Negative regex tests', () => {
95-
96107
describe('Pipe in binding', () => {
97108
test.each([
98109
{
99-
testCase: `<component [header]="'hello.world' | transloco">`
110+
testCase: `<component [header]="'hello.world' | transloco">`,
100111
},
101112
{
102-
testCase: `<component [header]="'hello.world' | somePipe | anotherPipe">`
113+
testCase: `<component [header]="'hello.world' | somePipe | anotherPipe">`,
103114
},
104115
{
105-
testCase: `<component [header]="'hello' | transloco:params | anotherPipe">`
116+
testCase: `<component [header]="'hello' | transloco:params | anotherPipe">`,
106117
},
107118
{
108-
testCase: `<component [title]="titleMap[reportType] | fooBar">`
119+
testCase: `<component [title]="titleMap[reportType] | fooBar">`,
109120
},
110121
{
111-
testCase: `<component [matTooltip]="('foo.bar' | transloco) + ': ' + (value | number: '1.0-2')">`
122+
testCase: `<component [matTooltip]="('foo.bar' | transloco) + ': ' + (value | number: '1.0-2')">`,
112123
},
113124
{
114-
testCase: `<compnent [title]="'Hello World ' + ('mom' | transloco) | fooBar">`
125+
testCase: `<compnent [title]="'Hello World ' + ('mom' | transloco) | fooBar">`,
115126
},
116127
{
117128
testCase: `<a [title]="'admin.1' | lowercase
118129
| translate"
119-
</a>`
120-
}
121-
])('Case: $testCase', ({testCase}) => {
130+
</a>`,
131+
},
132+
])('Case: $testCase', ({ testCase }) => {
122133
const regex = new RegExp(PIPE_IN_BINDING_REGEX, 'gm');
123134
const result = testCase.match(regex);
124135

125136
expect(result).toBeNull();
126137
});
127138
});
128-
139+
129140
describe('Pipe', () => {
130141
test.each([
131142
{
132-
testCase: `<component>{{ "hello.world" | transloco }}</component>`
143+
testCase: `<component>{{ "hello.world" | transloco }}</component>`,
133144
},
134145
{
135-
testCase: `<component>{{ "hello.world" | transloco | anotherPipe | oneMore }}</component>`
146+
testCase: `<component>{{ "hello.world" | transloco | anotherPipe | oneMore }}</component>`,
136147
},
137148
{
138-
testCase: `<component>{{ "hello" | transloco: { name: 'John' } }}</component>`
149+
testCase: `<component>{{ "hello" | transloco: { name: 'John' } }}</component>`,
139150
},
140151
{
141-
testCase: `<component>{{ titleMap[reportType] | somePipe }}</component>`
152+
testCase: `<component>{{ titleMap[reportType] | somePipe }}</component>`,
142153
},
143154
{
144-
testCase: `<component>{{ ('foo.bar' | transloco) + ': ' + (value | number: '1.0-2') }}</component>`
155+
testCase: `<component>{{ ('foo.bar' | transloco) + ': ' + (value | number: '1.0-2') }}</component>`,
145156
},
146157
{
147-
testCase: `<compnent>{{ 'Hello, ' + ('mom' | transloco) | fooBar }}</compnent>`
148-
}
149-
])('Case: $testCase', ({testCase}) => {
158+
testCase: `<compnent>{{ 'Hello, ' + ('mom' | transloco) | fooBar }}</compnent>`,
159+
},
160+
])('Case: $testCase', ({ testCase }) => {
150161
const regex = new RegExp(PIPE_REGEX, 'gm');
151162
const result = testCase.match(regex);
152163

153164
expect(result).toBeNull();
154165
});
155166
});
156-
157167
});
158168

159169
describe('HTML template', () => {
160-
161170
it('should replace html template content', async () => {
162171
const replacements: Record<string, string> = {},
163172
isWindows = process.platform === 'win32';
164-
173+
165174
(replaceInFile as unknown as jest.Mock).mockImplementation(
166175
async (config: ReplaceInFileConfig): Promise<void> => {
167176
const path = config.files as string,
168177
regex = config.from as RegExp,
169178
replacer = config.to as (match: string) => string;
170-
171-
const files = await glob(path, {windowsPathsNoEscape: isWindows});
172-
179+
180+
const files = await glob(path, { windowsPathsNoEscape: isWindows });
181+
173182
for (const fullPath of files) {
174183
const filename = nodePath.parse(fullPath).base,
175-
content = replacements[filename] ?? await readFile(fullPath, {encoding: 'utf-8'});
176-
184+
content =
185+
replacements[filename] ??
186+
(await readFile(fullPath, { encoding: 'utf-8' }));
187+
177188
replacements[filename] = content.replace(regex, replacer);
178189
}
179-
}
190+
},
180191
);
181192

182-
const ngxTranslateTemplatePath = './src/tests/templates/pipes/ngx-translate';
193+
const ngxTranslateTemplatePath =
194+
'./src/tests/templates/pipes/ngx-translate';
183195

184196
await run(ngxTranslateTemplatePath);
185197

186198
const filenames = Object.keys(replacements);
187199

188-
for(const filename of filenames) {
189-
const resultPath = nodePath.join(__dirname, './templates/pipes/transloco', filename),
190-
resultContent = await readFile(resultPath, {encoding: 'utf-8'});
200+
for (const filename of filenames) {
201+
const resultPath = nodePath.join(
202+
__dirname,
203+
'./templates/pipes/transloco',
204+
filename,
205+
),
206+
resultContent = await readFile(resultPath, { encoding: 'utf-8' });
191207

192208
expect(replacements[filename]).toBe(resultContent);
193209
}
194210
});
195-
196211
});
197212
});

0 commit comments

Comments
 (0)