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: properly extract escaped characters and respect default namespace in magic comments #91

Merged
merged 3 commits into from
Jul 3, 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
27 changes: 27 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"json5": "^2.2.3",
"jsonschema": "^1.4.1",
"openapi-fetch": "^0.9.7",
"unescape-js": "^1.1.4",
"vscode-oniguruma": "^1.7.0",
"vscode-textmate": "^9.0.0",
"yauzl": "^2.10.0"
Expand Down
2 changes: 2 additions & 0 deletions src/extractor/parser/generalMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export const generalMapper = (token: Token) => {
case 'string.quoted.single.ts':
case 'string.quoted.double.ts':
return 'string.body';
case 'constant.character.escape.ts':
return 'escaped.character';

// template strings
case 'punctuation.definition.string.template.begin.ts':
Expand Down
2 changes: 1 addition & 1 deletion src/extractor/parser/generateReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function keyInfoFromComment(
context.unusedComments.delete(commentAtLine);
return {
keyName: commentAtLine.keyName,
namespace: commentAtLine.namespace,
namespace: commentAtLine.namespace ?? context.options.defaultNamespace,
defaultValue: commentAtLine.defaultValue,
line: commentAtLine.line,
};
Expand Down
39 changes: 21 additions & 18 deletions src/extractor/parser/tokenMergers/stringMerger.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { GeneralTokenType } from '../generalMapper.js';
import { MachineType } from '../mergerMachine.js';
import unescape from 'unescape-js';

export const enum S {
Idle,
RegularString,
RegularStringEnd,
TemplateString,
TemplateStringEnd,
}

export const stringMerger = {
Expand All @@ -23,35 +22,39 @@ export const stringMerger = {
break;
case S.RegularString:
if (type === 'string.body') {
return S.RegularStringEnd;
return S.RegularString;
} else if (type === 'escaped.character') {
return S.RegularString;
} else if (type === 'string.end') {
return end.MERGE_ALL;
}
break;
case S.RegularStringEnd:
if (type === 'string.end') {
return end.MERGE_ALL;
}
break;
case S.TemplateString:
if (type === 'string.template.body') {
return S.TemplateStringEnd;
return S.TemplateString;
} else if (type === 'escaped.character') {
return S.TemplateString;
} else if (type === 'string.template.end') {
return end.MERGE_ALL;
}
break;
case S.TemplateStringEnd:
if (type === 'string.template.end') {
return end.MERGE_ALL;
}
}
},
customType: 'string',
resultToken: (matched) => {
if (matched.length === 3) {
return matched[1].token;
} else {
return '';
}
const escaped = matched
.map((t) => {
switch (t.customType) {
case 'string.template.body':
case 'string.body':
case 'escaped.character':
return t.token;

default:
return '';
}
})
.join('');
return unescape(escaped);
},
} as const satisfies MachineType<GeneralTokenType, S>;
93 changes: 93 additions & 0 deletions src/test/unit/extractor/shared/properties.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,99 @@ describe('Plain JavaScript', () => {
expect(extractValue(dict.value.key)).toBe('key');
expect(extractValue(dict.value.ns)).toBe('');
});

it('handles strings with newlines', async () => {
const dict = await getObject(
"const a = { key: 'key', defaultValue: 'default\\nvalue' }",
FILE_NAME,
'react'
);

JSON.stringify(dict.value.defaultValue);

expect(extractValue(dict.value.key)).toBe('key');
expect(extractValue(dict.value.defaultValue)).toBe('default\nvalue');
});

it('handles strings with newlines in template strings', async () => {
const dict = await getObject(
"const a = { key: 'key', defaultValue: `default\\nvalue` }",
FILE_NAME,
'react'
);

JSON.stringify(dict.value.defaultValue);

expect(extractValue(dict.value.key)).toBe('key');
expect(extractValue(dict.value.defaultValue)).toBe('default\nvalue');
});

it('handles strings with escaped backslash', async () => {
const dict = await getObject(
"const a = { key: 'key', defaultValue: `default\\\\value` }",
FILE_NAME,
'react'
);

JSON.stringify(dict.value.defaultValue);

expect(extractValue(dict.value.key)).toBe('key');
expect(extractValue(dict.value.defaultValue)).toBe('default\\value');
});

it('handles strings with escaped tab', async () => {
const dict = await getObject(
"const a = { key: 'key', defaultValue: `default\\tvalue` }",
FILE_NAME,
'react'
);

JSON.stringify(dict.value.defaultValue);

expect(extractValue(dict.value.key)).toBe('key');
expect(extractValue(dict.value.defaultValue)).toBe('default\tvalue');
});

it('handles strings with escaped quotes', async () => {
const dict = await getObject(
"const a = { key: 'key', defaultValue: 'default\\'value' }",
FILE_NAME,
'react'
);

JSON.stringify(dict.value.defaultValue);

expect(extractValue(dict.value.key)).toBe('key');
expect(extractValue(dict.value.defaultValue)).toBe("default'value");
});

it('handles strings with escaped double quotes', async () => {
const dict = await getObject(
`const a = { key: "key", defaultValue: "default\\"value" }`,
FILE_NAME,
'react'
);

JSON.stringify(dict.value.defaultValue);

expect(extractValue(dict.value.key)).toBe('key');
expect(extractValue(dict.value.defaultValue)).toBe('default"value');
});

it('handles strings with escaped unicode chars', async () => {
const dict = await getObject(
"const a = { key: 'key', defaultValue: 'default\\u{03A9}value' }",
FILE_NAME,
'react'
);

JSON.stringify(dict.value.defaultValue);

expect(extractValue(dict.value.key)).toBe('key');
expect(extractValue(dict.value.defaultValue)).toBe(
'default\u{03A9}value'
);
});
});
});
});
Expand Down
16 changes: 16 additions & 0 deletions src/test/unit/extractor/shared/report.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,21 @@ describe('report not influenced by surrounding block', () => {

expect(report.keys).toEqual([{ keyName: 'keyName', line: 3 }]);
});

it('respects default namespace in magic comments', async () => {
const report = await getReport(
`
// @tolgee-key key-override
t('keyName')
`,
FILE_NAME,
'react',
{ defaultNamespace: 'namespace' }
);

expect(report.keys).toEqual([
{ keyName: 'key-override', namespace: 'namespace', line: 2 },
]);
});
});
});
Loading