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

check module scripts for message definitions - CLI #157

Merged
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
25 changes: 15 additions & 10 deletions src/cli/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ function isMessagesDefinitionCall(node: Node, methodName: string) {
}

function getLibImportDeclarations(ast: Ast) {
return (ast.instance
? ast.instance.content.body.filter(
(node) =>
node.type === 'ImportDeclaration' && node.source.value === LIB_NAME,
)
: []) as ImportDeclaration[];
return (
ast.instance
? ast.instance.content.body.filter(
(node) =>
node.type === 'ImportDeclaration' && node.source.value === LIB_NAME,
)
: []
) as ImportDeclaration[];
}

function getDefineMessagesSpecifier(decl: ImportDeclaration) {
Expand Down Expand Up @@ -107,10 +109,10 @@ export function collectMessageDefinitions(ast: Ast) {

if (defineImportDecl == null) return [];

const defineMethodName = getDefineMessagesSpecifier(defineImportDecl).local
.name;
const defineMethodName =
getDefineMessagesSpecifier(defineImportDecl).local.name;

walk(ast.instance as any, {
const nodeStepInstructions = {
enter(node: Node) {
if (isMessagesDefinitionCall(node, defineMethodName) === false) return;
const [arg] = (node as CallExpression).arguments;
Expand All @@ -120,7 +122,10 @@ export function collectMessageDefinitions(ast: Ast) {
this.skip();
}
},
});
};

walk(ast.instance as any, nodeStepInstructions);
walk(ast.module as any, nodeStepInstructions);

return definitions.flatMap((definitionDict) =>
definitionDict.properties.map((propNode) => {
Expand Down
17 changes: 11 additions & 6 deletions test/cli/extract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,24 @@ describe('collecting message definitions', () => {
});

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';
defineMessages({ foo: { id: 'foo' }, bar: { id: 'bar' } })
defineMessages({ baz: { id: 'baz' }, quix: { id: 'qux' } })
</script>`);

const definitions = collectMessageDefinitions(ast);

expect(definitions).toHaveLength(4);
expect(definitions[0]).toMatchObject({ type: 'ObjectExpression' });
expect(definitions[1]).toMatchObject({ type: 'ObjectExpression' });
expect(definitions[2]).toMatchObject({ type: 'ObjectExpression' });
expect(definitions[3]).toMatchObject({ type: 'ObjectExpression' });
expect(definitions).toHaveLength(8);
definitions.forEach((definition) => {
expect(definition).toMatchObject({ type: 'ObjectExpression' });
});
});

it('throws an error if an spread is found', () => {
Expand Down