Skip to content

Commit 69c3048

Browse files
committed
🐛 fix Expose options weren't passed correctly
fix #102
1 parent ad1a5fe commit 69c3048

File tree

6 files changed

+112
-23
lines changed

6 files changed

+112
-23
lines changed

lib/features/validate-headers/impl.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,27 @@ export class ValidateHeaders extends Feature<ValidateHeadersOptions> {
4747
: ValidationGroup.I18n,
4848
];
4949

50+
const transformerGroups = whitelist
51+
? groups
52+
: [ValidationGroup.Main, ValidationGroup.I18n];
53+
5054
const headers = plainToInstance(HeadersClass, headersProps, {
5155
exposeDefaultValues: true,
5256
excludeExtraneousValues: whitelist,
5357
exposeUnsetFields: false,
54-
groups,
58+
groups: transformerGroups,
5559
});
5660

5761
if (strict) {
5862
const errors = validateSync(headers, {
5963
forbidNonWhitelisted: true,
6064
whitelist: true,
6165
stopAtFirstError: false,
62-
groups,
66+
groups: [
67+
locale === DEFAULT_LOCALE_KEY
68+
? ValidationGroup.Main
69+
: ValidationGroup.I18n,
70+
],
6371
});
6472

6573
if (errors.length > 0) {
@@ -72,6 +80,9 @@ export class ValidateHeaders extends Feature<ValidateHeadersOptions> {
7280
}
7381
}
7482

75-
return instanceToPlain(headers, { exposeUnsetFields: false, groups });
83+
return instanceToPlain(headers, {
84+
exposeUnsetFields: false,
85+
groups: transformerGroups,
86+
});
7687
}
7788
}

lib/features/validate-headers/utils.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,29 @@ export type ValidatorFactory<T extends any[] = []> = (...args: T) => Validator;
1616
* wrap it in a new `@Expose()` implementation to stack for `groups` options.
1717
*/
1818
export const Expose: Validator =
19-
({ groups } = {}) =>
19+
(options: transformer.ExposeOptions = {}) =>
2020
(target, prop) => {
2121
const metadata = defaultMetadataStorage.findExposeMetadata(
2222
target.constructor,
2323
prop as string,
2424
);
2525

2626
if (!metadata) {
27-
transformer.Expose()(target, prop);
27+
transformer.Expose(options)(target, prop);
2828

2929
return;
3030
}
3131

32-
if (!groups) return;
33-
34-
metadata.options.groups = (metadata.options.groups ?? []).concat(groups);
32+
// merge expose options
33+
Object.assign(
34+
metadata.options,
35+
options,
36+
options.groups
37+
? {
38+
groups: (metadata.options.groups ?? []).concat(options.groups),
39+
}
40+
: undefined,
41+
);
3542
};
3643

3744
export const partialGroups =

test/integration/i18n/fixtures.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@ import { File, GlobalFixtures } from '../fixtures';
33
export class Fixtures extends GlobalFixtures {
44
@File(__dirname, 'i18n.headers.txt')
55
public static readonly i18nHeaders: string;
6+
7+
@File(__dirname, 'non-strict-i18n.headers.txt')
8+
public static readonly nonStrictI18nHeaders: string;
69
}

test/integration/i18n/i18n.headers.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// ==UserScript==
22
// @name i18n
3+
// @name:en localized name
34
// @description this is a fantastic userscript
45
// @description:en i18n description
56
// @version 0.0.0

test/integration/i18n/index.test.ts

Lines changed: 75 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ describe('i18n', () => {
2424
},
2525
i18n: {
2626
en: {
27+
name: 'localized name',
2728
description: 'i18n description',
2829
},
2930
},
@@ -41,6 +42,7 @@ describe('i18n', () => {
4142
input.writeFileSync(
4243
'/headers.json',
4344
JSON.stringify({
45+
name: 'localized name',
4446
description: 'i18n description',
4547
}),
4648
);
@@ -76,6 +78,7 @@ describe('i18n', () => {
7678
i18n: {
7779
en: (headers): HeadersProps => ({
7880
...headers,
81+
name: 'localized name',
7982
description: 'i18n description',
8083
}),
8184
},
@@ -89,23 +92,80 @@ describe('i18n', () => {
8992
});
9093
});
9194

92-
it('unlocalizable tags', () => {
93-
const promise = compile(input, {
94-
...Fixtures.webpackConfig,
95-
plugins: [
96-
new UserscriptPlugin({
97-
headers: {
98-
name: 'i18n',
99-
},
100-
i18n: {
101-
en: {
102-
downloadURL: 'https://example.com',
95+
describe('unlocalizable tags', () => {
96+
it('are rejected in strict mode', () => {
97+
const promise = compile(input, {
98+
...Fixtures.webpackConfig,
99+
plugins: [
100+
new UserscriptPlugin({
101+
headers: {
102+
name: 'i18n',
103103
},
104-
},
105-
}),
106-
],
104+
i18n: {
105+
en: {
106+
name: 'localized name',
107+
downloadURL: 'https://example.com',
108+
},
109+
},
110+
}),
111+
],
112+
});
113+
114+
return expect(promise).toReject();
107115
});
108116

109-
return expect(promise).toReject();
117+
it('are allowed in non-strict mode', async () => {
118+
const output = await compile(input, {
119+
...Fixtures.webpackConfig,
120+
plugins: [
121+
new UserscriptPlugin({
122+
headers: {
123+
name: 'non-strict i18n',
124+
},
125+
i18n: {
126+
en: (headers): HeadersProps => ({
127+
...headers,
128+
downloadURL: 'https://example.com',
129+
}),
130+
},
131+
strict: false,
132+
}),
133+
],
134+
});
135+
136+
expect(output.toJSON()).toEqual({
137+
'/dist/output.user.js': Fixtures.entryUserJs(
138+
Fixtures.nonStrictI18nHeaders,
139+
),
140+
'/dist/output.meta.js': Fixtures.nonStrictI18nHeaders,
141+
});
142+
});
143+
144+
it('are stripped in whitelist mode', async () => {
145+
const output = await compile(input, {
146+
...Fixtures.webpackConfig,
147+
plugins: [
148+
new UserscriptPlugin({
149+
headers: {
150+
name: 'i18n',
151+
},
152+
i18n: {
153+
en: {
154+
name: 'localized name',
155+
description: 'i18n description',
156+
// downloadURL will be stripped
157+
downloadURL: 'https://example.com',
158+
},
159+
},
160+
whitelist: true,
161+
}),
162+
],
163+
});
164+
165+
expect(output.toJSON()).toEqual({
166+
'/dist/output.user.js': Fixtures.entryUserJs(Fixtures.i18nHeaders),
167+
'/dist/output.meta.js': Fixtures.i18nHeaders,
168+
});
169+
});
110170
});
111171
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// ==UserScript==
2+
// @name non-strict i18n
3+
// @description this is a fantastic userscript
4+
// @version 0.0.0
5+
// @match *://*/*
6+
// @downloadURL:en https://example.com
7+
// ==/UserScript==

0 commit comments

Comments
 (0)