mirror of
https://github.com/cupcakearmy/svelte-i18n.git
synced 2024-06-30 18:54:49 +02:00
check module scripts for message definitions - CLI
This commit is contained in:
parent
64e8ae2bd3
commit
721e991d90
|
@ -50,12 +50,14 @@ function isMessagesDefinitionCall(node: Node, methodName: string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function getLibImportDeclarations(ast: Ast) {
|
function getLibImportDeclarations(ast: Ast) {
|
||||||
return (ast.instance
|
return (
|
||||||
? ast.instance.content.body.filter(
|
ast.instance
|
||||||
(node) =>
|
? ast.instance.content.body.filter(
|
||||||
node.type === 'ImportDeclaration' && node.source.value === LIB_NAME,
|
(node) =>
|
||||||
)
|
node.type === 'ImportDeclaration' && node.source.value === LIB_NAME,
|
||||||
: []) as ImportDeclaration[];
|
)
|
||||||
|
: []
|
||||||
|
) as ImportDeclaration[];
|
||||||
}
|
}
|
||||||
|
|
||||||
function getDefineMessagesSpecifier(decl: ImportDeclaration) {
|
function getDefineMessagesSpecifier(decl: ImportDeclaration) {
|
||||||
|
@ -107,10 +109,10 @@ export function collectMessageDefinitions(ast: Ast) {
|
||||||
|
|
||||||
if (defineImportDecl == null) return [];
|
if (defineImportDecl == null) return [];
|
||||||
|
|
||||||
const defineMethodName = getDefineMessagesSpecifier(defineImportDecl).local
|
const defineMethodName =
|
||||||
.name;
|
getDefineMessagesSpecifier(defineImportDecl).local.name;
|
||||||
|
|
||||||
walk(ast.instance as any, {
|
const nodeStepInstructions = {
|
||||||
enter(node: Node) {
|
enter(node: Node) {
|
||||||
if (isMessagesDefinitionCall(node, defineMethodName) === false) return;
|
if (isMessagesDefinitionCall(node, defineMethodName) === false) return;
|
||||||
const [arg] = (node as CallExpression).arguments;
|
const [arg] = (node as CallExpression).arguments;
|
||||||
|
@ -120,7 +122,10 @@ export function collectMessageDefinitions(ast: Ast) {
|
||||||
this.skip();
|
this.skip();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
});
|
};
|
||||||
|
|
||||||
|
walk(ast.instance as any, nodeStepInstructions);
|
||||||
|
walk(ast.module as any, nodeStepInstructions);
|
||||||
|
|
||||||
return definitions.flatMap((definitionDict) =>
|
return definitions.flatMap((definitionDict) =>
|
||||||
definitionDict.properties.map((propNode) => {
|
definitionDict.properties.map((propNode) => {
|
||||||
|
|
|
@ -85,7 +85,13 @@ describe('collecting message definitions', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('gets all message definition objects', () => {
|
it('gets all message definition objects', () => {
|
||||||
const ast = parse(`<script>
|
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>
|
||||||
import { defineMessages } from 'svelte-i18n';
|
import { defineMessages } from 'svelte-i18n';
|
||||||
defineMessages({ foo: { id: 'foo' }, bar: { id: 'bar' } })
|
defineMessages({ foo: { id: 'foo' }, bar: { id: 'bar' } })
|
||||||
defineMessages({ baz: { id: 'baz' }, quix: { id: 'qux' } })
|
defineMessages({ baz: { id: 'baz' }, quix: { id: 'qux' } })
|
||||||
|
@ -93,11 +99,10 @@ describe('collecting message definitions', () => {
|
||||||
|
|
||||||
const definitions = collectMessageDefinitions(ast);
|
const definitions = collectMessageDefinitions(ast);
|
||||||
|
|
||||||
expect(definitions).toHaveLength(4);
|
expect(definitions).toHaveLength(8);
|
||||||
expect(definitions[0]).toMatchObject({ type: 'ObjectExpression' });
|
definitions.forEach((definition) => {
|
||||||
expect(definitions[1]).toMatchObject({ type: 'ObjectExpression' });
|
expect(definition).toMatchObject({ type: 'ObjectExpression' });
|
||||||
expect(definitions[2]).toMatchObject({ type: 'ObjectExpression' });
|
});
|
||||||
expect(definitions[3]).toMatchObject({ type: 'ObjectExpression' });
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('throws an error if an spread is found', () => {
|
it('throws an error if an spread is found', () => {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user