mirror of
https://github.com/cupcakearmy/svelte-i18n.git
synced 2024-11-16 18:10:43 +01:00
fix: check modules for defineMessages imports (#163)
Co-authored-by: Denis Richard <richard@satelligence.com>
This commit is contained in:
parent
2dd2b54ff7
commit
ec939ab6c6
10921
package-lock.json
generated
Normal file
10921
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
@ -49,14 +49,15 @@ function isMessagesDefinitionCall(node: Node, methodName: string) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getLibImportDeclarations(ast: Ast) {
|
function getLibImportDeclarations(ast: Ast): ImportDeclaration[] {
|
||||||
return (
|
const bodyElements = [
|
||||||
ast.instance
|
...(ast.instance?.content.body || []),
|
||||||
? ast.instance.content.body.filter(
|
...(ast.module?.content.body || []),
|
||||||
(node) =>
|
];
|
||||||
node.type === 'ImportDeclaration' && node.source.value === LIB_NAME,
|
|
||||||
)
|
return bodyElements.filter(
|
||||||
: []
|
(node) =>
|
||||||
|
node.type === 'ImportDeclaration' && node.source.value === LIB_NAME,
|
||||||
) as ImportDeclaration[];
|
) as ImportDeclaration[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -105,6 +105,76 @@ describe('collecting message definitions', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('gets message definitions if in module only', () => {
|
||||||
|
const ast = parse(`
|
||||||
|
<script context="module">
|
||||||
|
import { defineMessages } from 'svelte-i18n';
|
||||||
|
defineMessages({ quux: { id: 'quux' }, quuz: { id: 'quuz' } })
|
||||||
|
defineMessages({ corge: { id: 'corge' }, grault: { id: 'grault' } })
|
||||||
|
</script>
|
||||||
|
<script>
|
||||||
|
const a = "a";
|
||||||
|
</script>`);
|
||||||
|
|
||||||
|
const definitions = collectMessageDefinitions(ast);
|
||||||
|
|
||||||
|
expect(definitions).toHaveLength(4);
|
||||||
|
definitions.forEach((definition) => {
|
||||||
|
expect(definition).toMatchObject({ type: 'ObjectExpression' });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('gets message definitions if in script only', () => {
|
||||||
|
const ast = parse(`
|
||||||
|
<script context="module">
|
||||||
|
const a = "a";
|
||||||
|
</script>
|
||||||
|
<script>
|
||||||
|
import { defineMessages } from 'svelte-i18n';
|
||||||
|
defineMessages({ foo: { id: 'foo' }, bar: { id: 'bar' } })
|
||||||
|
defineMessages({ baz: { id: 'baz' }, quix: { id: 'qux' } })
|
||||||
|
</script>`);
|
||||||
|
|
||||||
|
const definitions = collectMessageDefinitions(ast);
|
||||||
|
|
||||||
|
expect(definitions).toHaveLength(4);
|
||||||
|
definitions.forEach((definition) => {
|
||||||
|
expect(definition).toMatchObject({ type: 'ObjectExpression' });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('gets message definitions if script only', () => {
|
||||||
|
const ast = parse(`
|
||||||
|
<script>
|
||||||
|
import { defineMessages } from 'svelte-i18n';
|
||||||
|
defineMessages({ foo: { id: 'foo' }, bar: { id: 'bar' } })
|
||||||
|
defineMessages({ baz: { id: 'baz' }, quix: { id: 'qux' } })
|
||||||
|
</script>`);
|
||||||
|
|
||||||
|
const definitions = collectMessageDefinitions(ast);
|
||||||
|
|
||||||
|
expect(definitions).toHaveLength(4);
|
||||||
|
definitions.forEach((definition) => {
|
||||||
|
expect(definition).toMatchObject({ type: 'ObjectExpression' });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('gets message definitions if module only', () => {
|
||||||
|
const ast = parse(`
|
||||||
|
<script context="module">
|
||||||
|
import { defineMessages } from 'svelte-i18n';
|
||||||
|
defineMessages({ foo: { id: 'foo' }, bar: { id: 'bar' } })
|
||||||
|
defineMessages({ baz: { id: 'baz' }, quix: { id: 'qux' } })
|
||||||
|
</script>`);
|
||||||
|
|
||||||
|
const definitions = collectMessageDefinitions(ast);
|
||||||
|
|
||||||
|
expect(definitions).toHaveLength(4);
|
||||||
|
definitions.forEach((definition) => {
|
||||||
|
expect(definition).toMatchObject({ type: 'ObjectExpression' });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('throws an error if an spread is found', () => {
|
it('throws an error if an spread is found', () => {
|
||||||
const ast = parse(`<script>
|
const ast = parse(`<script>
|
||||||
import { defineMessages } from 'svelte-i18n';
|
import { defineMessages } from 'svelte-i18n';
|
||||||
|
@ -30,7 +30,7 @@ $locale.subscribe(() => {
|
|||||||
formatTime = get($formatTime);
|
formatTime = get($formatTime);
|
||||||
formatDate = get($formatDate);
|
formatDate = get($formatDate);
|
||||||
formatNumber = get($formatNumber);
|
formatNumber = get($formatNumber);
|
||||||
getJSON = get($getJSON);
|
getJSON = get($getJSON) as JSONGetter;
|
||||||
});
|
});
|
||||||
|
|
||||||
addMessages('en', require('../../fixtures/en.json'));
|
addMessages('en', require('../../fixtures/en.json'));
|
||||||
|
Loading…
Reference in New Issue
Block a user