fix: check modules for defineMessages imports (#163)

Co-authored-by: Denis Richard <richard@satelligence.com>
This commit is contained in:
odeniso 2021-10-11 13:57:46 +02:00 committed by GitHub
parent 2dd2b54ff7
commit ec939ab6c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 11001 additions and 9 deletions

10921
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -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[];
} }

View File

@ -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';

View File

@ -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'));