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 behavior of use-escaping #75

Merged
merged 2 commits into from
Oct 18, 2024
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to the "arbeditor" extension will be documented in this file

Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.

## [Unreleased]

- Fix behavior of `use-escaping` [#72](https://github.com/google/arb-editor/issues/72).

## [0.2.0]

- Fix [#62](https://github.com/google/arb-editor/issues/62).
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ An extension to help you in editing .arb files, used for localization of applica

### Customize

Escaping quotes can be turned off by including a `l10n.yaml` file with the line:
Escaping quotes can be turned on by including a `l10n.yaml` file with the line:
```yaml
use-escaping: false
use-escaping: true
```

To set a template file, either set the `@@x-template` element in your `arb` file
Expand Down
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,6 @@ export interface L10nYaml {
'required-resource-attributes'?: boolean | undefined;
'nullable-getter'?: boolean | undefined;
'format'?: boolean | undefined;
'use-escaping'?: string | undefined;
'use-escaping'?: boolean | undefined;
'suppress-warnings'?: boolean | undefined;
}
11 changes: 5 additions & 6 deletions src/messageParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,10 @@ interface ParseAndDecorateOptions {
}

function matchCurlyBrackets(v: string, l10nOptions?: L10nYaml): XRegExp.MatchRecursiveValueNameMatch[] {
const unescaped = getUnescapedRegions(v, l10nOptions);
const unescaped = l10nOptions?.['use-escaping'] ?? false
? getUnescapedRegions(v) :
[[0, v.length]];

var values: XRegExp.MatchRecursiveValueNameMatch[] = [];
for (var region of unescaped) {
const newLocal = XRegExp.matchRecursive(v.substring(region[0], region[1]), '\\{', '\\}', 'g', {
Expand All @@ -281,13 +284,9 @@ function parseYaml(uri: string): L10nYaml | undefined {
return YAML.parse(yaml) as L10nYaml;
}

export function getUnescapedRegions(expression: string, l10nOptions?: L10nYaml): [number, number][] {
export function getUnescapedRegions(expression: string): [number, number][] {
const unEscapedRegions: [number, number][] = [];

if (!(l10nOptions?.['use-escaping'] ?? true)) {
return [[0, expression.length]];
}

var unEscapedRegionEdge: number | null;
unEscapedRegionEdge = 0;
for (let index = 0; index < expression.length; index++) {
Expand Down
9 changes: 9 additions & 0 deletions src/test/l10nYaml/use-escaping-false/_l10n/testarb.annotated
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"@@locale": "en",
"property01": "Test",
"property02": "Te'st",
"property03": "'{Te'st",
^^^^^^^[Error]:"Error: Unbalanced left delimiter found in string at position 1"
"property04": "'{Te'st'"
^^^^^^^^[Error]:"Error: Unbalanced left delimiter found in string at position 1"
}
7 changes: 7 additions & 0 deletions src/test/l10nYaml/use-escaping-false/_l10n/testarb.arb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"@@locale": "en",
"property01": "Test",
"property02": "Te'st",
"property03": "'{Te'st",
"property04": "'{Te'st'"
}
mosuem marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 3 additions & 0 deletions src/test/l10nYaml/use-escaping-false/l10n.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
arb-dir: _l10n
template-arb-file: testarb.arb
use-escaping: false
9 changes: 9 additions & 0 deletions src/test/l10nYaml/use-escaping-true/_l10n/testarb.annotated
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"@@locale": "en",
"property01": "Test",
"property02": "Te'st",
^^^^^[Error]:"Error: Unbalanced escape quotes. To escape a single quote ', prefix it with another single quote."
"property03": "'{Te'st",
"property04": "'{Te'st'"
^^^^^^^^[Error]:"Error: Unbalanced escape quotes. To escape a single quote ', prefix it with another single quote."
}
7 changes: 7 additions & 0 deletions src/test/l10nYaml/use-escaping-true/_l10n/testarb.arb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"@@locale": "en",
"property01": "Test",
"property02": "Te'st",
"property03": "'{Te'st",
"property04": "'{Te'st'"
}
3 changes: 3 additions & 0 deletions src/test/l10nYaml/use-escaping-true/l10n.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
arb-dir: _l10n
template-arb-file: testarb.arb
use-escaping: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"@@locale": "en",
"property01": "Test",
"property02": "Te'st",
"property03": "'{Te'st",
^^^^^^^[Error]:"Error: Unbalanced left delimiter found in string at position 1"
"property04": "'{Te'st'"
^^^^^^^^[Error]:"Error: Unbalanced left delimiter found in string at position 1"
}
7 changes: 7 additions & 0 deletions src/test/l10nYaml/use-escaping-undefined/_l10n/testarb.arb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"@@locale": "en",
"property01": "Test",
"property02": "Te'st",
"property03": "'{Te'st",
"property04": "'{Te'st'"
}
2 changes: 2 additions & 0 deletions src/test/l10nYaml/use-escaping-undefined/l10n.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
arb-dir: _l10n
template-arb-file: testarb.arb
25 changes: 23 additions & 2 deletions src/test/suite/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ suite('Extension Test Suite', async () => {
});

suite('Template Path', async () => {
/* eslint-disable @typescript-eslint/naming-convention */
test("Resolve template path from @@x-template with L10nYaml", async () => {
const testDir = 'l10nYaml/with_x-template/l10n';
await updateConfiguration(null);
Expand Down Expand Up @@ -194,7 +193,29 @@ suite('Extension Test Suite', async () => {
const contentWithAnnotations = await buildContentWithAnnotations(`${testDir}/testarb_2.arb`);
await compareGolden(contentWithAnnotations, `${testDir}/testarb_2.annotated`);
});
/* eslint-enable */
});

suite('use-escaping', async () => {
test("Decorate golden file when use-escaping=true.", async () => {
const testDir = 'l10nYaml/use-escaping-true/_l10n';
await updateConfiguration([DiagnosticCode.missingMetadataForKey]);
const contentWithAnnotations = await buildContentWithAnnotations(`${testDir}/testarb.arb`);
await compareGolden(contentWithAnnotations, `${testDir}/testarb.annotated`);
});

test("Decorate golden file when use-escaping=false.", async () => {
const testDir = 'l10nYaml/use-escaping-false/_l10n';
await updateConfiguration([DiagnosticCode.missingMetadataForKey]);
const contentWithAnnotations = await buildContentWithAnnotations(`${testDir}/testarb.arb`);
await compareGolden(contentWithAnnotations, `${testDir}/testarb.annotated`);
});

test("Decorate golden file when use-escaping=undefined.", async () => {
const testDir = 'l10nYaml/use-escaping-undefined/_l10n';
await updateConfiguration([DiagnosticCode.missingMetadataForKey]);
const contentWithAnnotations = await buildContentWithAnnotations(`${testDir}/testarb.arb`);
await compareGolden(contentWithAnnotations, `${testDir}/testarb.annotated`);
});
});
});

Expand Down
4 changes: 2 additions & 2 deletions src/test/testarb.annotated
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
}
},
"escaped": "Your pa'{'ssword",
^^^^^^^[Information]:"The message with key "escaped" does not have metadata defined."
^^^^^^^^^^^^^^^^[Error]:"Error: Unbalanced left delimiter found in string at position 8"
"unescaped": "Your pa'ssword",
^^^^^^^^^^^^^^[Error]:"Error: Unbalanced escape quotes. To escape a single quote ', prefix it with another single quote."
^^^^^^^^^[Information]:"The message with key "unescaped" does not have metadata defined."
"singlequote": "Your pa''ssword",
^^^^^^^^^^^[Information]:"The message with key "singlequote" does not have metadata defined."
"pageHomeInboxCount": "{count, plural, zero{I have {vehicle;;Type, select, sedn{Sedan} cabrolet{Solid roof cabriolet} tuck{16 wheel truck} other{Other}} no new messages} one{You have 1 new {counts} message} other{You have {count} new messages}}",
Expand Down
4 changes: 2 additions & 2 deletions src/test/testarb_2.annotated
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"pageHomeTitle": "Welcome {firstName}",
^^^^^^^^^[decoration]placeholder
"escaped": "Your pa'{'ssword"
^^^^^^^[Information]:"The message with key "escaped" does not have metadata defined."
^^^^^^^^^^^^^^^^[Error]:"Error: Unbalanced left delimiter found in string at position 8"
}

[Warning]:"Missing messages from template: singlequote, pageHomeInboxCount, commonVehicleType, pageHomeBalance"
[Warning]:"Missing messages from template: unescaped, singlequote, pageHomeInboxCount, commonVehicleType, pageHomeBalance"
4 changes: 2 additions & 2 deletions src/test/testarb_3.annotated
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
"pageHomeTitle": "Welcome {firstName}",
^^^^^^^^^[decoration]placeholder
"escaped": "Your pa'{'ssword",
^^^^^^^[Information]:"The message with key "escaped" does not have metadata defined."
^^^^^^^^^^^^^^^^[Error]:"Error: Unbalanced left delimiter found in string at position 8"
"unescaped": "Your pa'ssword",
^^^^^^^^^^^^^^[Error]:"Error: Unbalanced escape quotes. To escape a single quote ', prefix it with another single quote."
^^^^^^^^^[Information]:"The message with key "unescaped" does not have metadata defined."
"singlequote": "Your pa''ssword",
^^^^^^^^^^^[Information]:"The message with key "singlequote" does not have metadata defined."
"pageHomeInboxCount": "{count, plural, zero{I have {vehicle;;Type, select, sedn{Sedan} cabrolet{Solid roof cabriolet} tuck{16 wheel truck} other{Other}} no new messages} one{You have 1 new {counts} message} other{You have {count} new messages}}",
Expand Down