Skip to content

Commit

Permalink
feat: Support .yml YAML files (#384)
Browse files Browse the repository at this point in the history
  • Loading branch information
patocallaghan committed Apr 26, 2022
1 parent 253e5a6 commit e150ba7
Show file tree
Hide file tree
Showing 2 changed files with 246 additions and 70 deletions.
6 changes: 4 additions & 2 deletions src/builtin-addons/core/intl-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ type TranslationFile =
}
| Record<string, never>;

const YAML_EXTENSIONS = ['.yaml', '.yml'];

export async function getTranslations(root: string, server: Server): Promise<TranslationsHashMap> {
const hashMap = {};
const intlEntry = path.join(root, 'translations');
Expand Down Expand Up @@ -72,7 +74,7 @@ async function recursiveIntlTranslationsSearch(server: Server, hashMap: Translat
async function objFromFile(server: Server, filePath: string): Promise<TranslationFile> {
const ext = path.extname(filePath);

if (ext === '.yaml') {
if (YAML_EXTENSIONS.includes(ext)) {
const content = await server.fs.readFile(filePath);

if (content === null) {
Expand Down Expand Up @@ -112,7 +114,7 @@ function addToHashMap(hash: TranslationsHashMap, translationFile: TranslationFil

if (extension === '.json' && translationFile.type === 'json') {
position = getPositionInJson(translationFile.jsonAst, p);
} else if (extension === '.yaml' && translationFile.type === 'yaml') {
} else if (YAML_EXTENSIONS.includes(extension) && translationFile.type === 'yaml') {
position = getPositionInYaml(translationFile.yamlAst, p, translationFile.yamlLineCounter);
}

Expand Down
310 changes: 242 additions & 68 deletions test/bultin-addons/core/intl-providers-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ const translationsYaml = {
},
};

const translationsYamlYMLExtension = {
'en-us.yml': `rootFileTranslation: text 1`,
'sub-folder': {
'en-us.yml': `subFolderTranslation:
subTranslation: text 2
anotherTranslation: another text
`,
},
};

const translationsYamlInvalid = {
'en-us.yaml': `rootFileTranslation text 1`,
};
Expand Down Expand Up @@ -424,8 +434,50 @@ for (const asyncFsEnabled of testCaseAsyncFsOptions) {
});
});

describe('provide completion - YAML', () => {
it('should autocomplete root translation in handlebars', async () => {
describe('YAML - .yaml file extensions', () => {
describe('provide completion', () => {
it('should autocomplete root translation in handlebars', async () => {
expect(
(
await getResult(
CompletionRequest.method,
connection,
{
app: {
components: {
'test.hbs': '{{t "rootFileTransla"}}',
},
},
translations: translationsYaml,
},
'app/components/test.hbs',
{ line: 0, character: 20 }
)
).response
).toEqual([
{
documentation: 'en-us : text 1',
kind: 12,
label: 'rootFileTranslation',
textEdit: {
newText: 'rootFileTranslation',
range: {
end: {
character: 5,
line: 0,
},
start: {
character: 5,
line: 0,
},
},
},
},
]);
});
});

it('should autocomplete sub folder translation in handlebars', async () => {
expect(
(
await getResult(
Expand All @@ -434,22 +486,40 @@ for (const asyncFsEnabled of testCaseAsyncFsOptions) {
{
app: {
components: {
'test.hbs': '{{t "rootFileTransla"}}',
'test.hbs': '{{t "subFolderTranslat"}}',
},
},
translations: translationsYaml,
},
'app/components/test.hbs',
{ line: 0, character: 20 }
{ line: 0, character: 22 }
)
).response
).toEqual([
{
documentation: 'en-us : text 1',
documentation: 'en-us : text 2',
kind: 12,
label: 'rootFileTranslation',
label: 'subFolderTranslation.subTranslation',
textEdit: {
newText: 'rootFileTranslation',
newText: 'subFolderTranslation.subTranslation',
range: {
end: {
character: 5,
line: 0,
},
start: {
character: 5,
line: 0,
},
},
},
},
{
documentation: 'en-us : another text',
kind: 12,
label: 'subFolderTranslation.anotherTranslation',
textEdit: {
newText: 'subFolderTranslation.anotherTranslation',
range: {
end: {
character: 5,
Expand All @@ -465,6 +535,110 @@ for (const asyncFsEnabled of testCaseAsyncFsOptions) {
]);
});

describe('provide definition', () => {
it('should provide translation definition in handlebars', async () => {
expect(
(
(await getResult(
DefinitionRequest.method,
connection,
{
app: {
components: {
'test.hbs': '{{t "subFolderTranslation.subTranslation" }}',
},
},
translations: translationsYaml,
},
'app/components/test.hbs',
{ line: 0, character: 32 }
)) as any
).response
).toEqual([
{
uri: '/translations/sub-folder/en-us.yaml',
range: {
start: { line: 1, character: 8 },
end: { line: 1, character: 30 },
},
},
]);
});

it('should provide translation definition in js', async () => {
expect(
(
(await getResult(
DefinitionRequest.method,
connection,
{
app: {
components: {
'test.js': 'export default class Foo extends Bar { text = this.intl.t("subFolderTranslation.anotherTranslation"); }',
},
},
translations: translationsYaml,
},
'app/components/test.js',
{ line: 0, character: 86 }
)) as any
).response
).toEqual([
{
uri: '/translations/sub-folder/en-us.yaml',
range: {
start: { line: 2, character: 8 },
end: { line: 2, character: 40 },
},
},
]);
});
});
});

describe('YAML - .yml file extensions', () => {
describe('provide completion', () => {
it('should autocomplete root translation in handlebars', async () => {
expect(
(
await getResult(
CompletionRequest.method,
connection,
{
app: {
components: {
'test.hbs': '{{t "rootFileTransla"}}',
},
},
translations: translationsYamlYMLExtension,
},
'app/components/test.hbs',
{ line: 0, character: 20 }
)
).response
).toEqual([
{
documentation: 'en-us : text 1',
kind: 12,
label: 'rootFileTranslation',
textEdit: {
newText: 'rootFileTranslation',
range: {
end: {
character: 5,
line: 0,
},
start: {
character: 5,
line: 0,
},
},
},
},
]);
});
});

it('should autocomplete sub folder translation in handlebars', async () => {
expect(
(
Expand All @@ -477,7 +651,7 @@ for (const asyncFsEnabled of testCaseAsyncFsOptions) {
'test.hbs': '{{t "subFolderTranslat"}}',
},
},
translations: translationsYaml,
translations: translationsYamlYMLExtension,
},
'app/components/test.hbs',
{ line: 0, character: 22 }
Expand Down Expand Up @@ -522,6 +696,66 @@ for (const asyncFsEnabled of testCaseAsyncFsOptions) {
},
]);
});

describe('provide definition', () => {
it('should provide translation definition in handlebars', async () => {
expect(
(
(await getResult(
DefinitionRequest.method,
connection,
{
app: {
components: {
'test.hbs': '{{t "subFolderTranslation.subTranslation" }}',
},
},
translations: translationsYamlYMLExtension,
},
'app/components/test.hbs',
{ line: 0, character: 32 }
)) as any
).response
).toEqual([
{
uri: '/translations/sub-folder/en-us.yml',
range: {
start: { line: 1, character: 8 },
end: { line: 1, character: 30 },
},
},
]);
});

it('should provide translation definition in js', async () => {
expect(
(
(await getResult(
DefinitionRequest.method,
connection,
{
app: {
components: {
'test.js': 'export default class Foo extends Bar { text = this.intl.t("subFolderTranslation.anotherTranslation"); }',
},
},
translations: translationsYamlYMLExtension,
},
'app/components/test.js',
{ line: 0, character: 86 }
)) as any
).response
).toEqual([
{
uri: '/translations/sub-folder/en-us.yml',
range: {
start: { line: 2, character: 8 },
end: { line: 2, character: 40 },
},
},
]);
});
});
});

describe('provide definition', () => {
Expand Down Expand Up @@ -620,66 +854,6 @@ for (const asyncFsEnabled of testCaseAsyncFsOptions) {
});
});

describe('provide definition -YAML', () => {
it('should provide translation definition in handlebars', async () => {
expect(
(
(await getResult(
DefinitionRequest.method,
connection,
{
app: {
components: {
'test.hbs': '{{t "subFolderTranslation.subTranslation" }}',
},
},
translations: translationsYaml,
},
'app/components/test.hbs',
{ line: 0, character: 32 }
)) as any
).response
).toEqual([
{
uri: '/translations/sub-folder/en-us.yaml',
range: {
start: { line: 1, character: 8 },
end: { line: 1, character: 30 },
},
},
]);
});

it('should provide translation definition in js', async () => {
expect(
(
(await getResult(
DefinitionRequest.method,
connection,
{
app: {
components: {
'test.js': 'export default class Foo extends Bar { text = this.intl.t("subFolderTranslation.anotherTranslation"); }',
},
},
translations: translationsYaml,
},
'app/components/test.js',
{ line: 0, character: 86 }
)) as any
).response
).toEqual([
{
uri: '/translations/sub-folder/en-us.yaml',
range: {
start: { line: 2, character: 8 },
end: { line: 2, character: 40 },
},
},
]);
});
});

describe('provide hover', () => {
it('should provide translation hover in handlebars', async () => {
expect(
Expand Down

0 comments on commit e150ba7

Please sign in to comment.