Skip to content

Commit

Permalink
Issue Polyconseil#37 - Extract strings only when expression is a gett…
Browse files Browse the repository at this point in the history
…ext function

This commit is a fix for issue Polyconseil#37. The problem came from the extractor
that was trying to extract the strings arguments of any gettext-like
node without making sure that the type of the current node was a CallExpression.
This fix prevents the extractor to automatically extract strings or variables
or whatever called like a vue-gettext function.

How to test:
- setup a vue file containing decoys looking like some gettext functions
  like:

<template>
  <h1>{{ message }}<h1>
</template>
<script>
import $gettext from '@helper/gettext';

export default {
  name: "$gettext", // because some people are actually weird
  computed: {
    message() {
      return this.$gettext('Hello world from the $gettext function');
    }
  }
}
</script>

- Run extract-cli with this file.

Expected results
- The string "Hello world from the $gettext function" has been
  successfully extracted without any error.
  • Loading branch information
gorkat committed Jul 3, 2018
1 parent 73ae873 commit c6d7e4d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/javascript-extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ function getLocalizedStringsFromNode(filename, script, token) {
const expression = acorn.parseExpressionAt(script, token.start);
const localizedStrings = [];

if (expression.type !== 'CallExpression') {
return [];
}

const nodeTranslation = nodeTranslationInfoFactory.getNodeTranslationInfoRepresentation(
filename,
token,
Expand Down
11 changes: 11 additions & 0 deletions src/javascript-extract.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,16 @@ describe('Javascript extractor object', () => {
expect(firstString.reference.file).to.be.equal(filename);
expect(firstString.reference.line).to.be.equal(6);
});

it('should not try to extract strings when the node is not a function', () => {
const filename = 'traps.vue';
const extractedStrings = jsExtractor.extractStringsFromJavascript(
filename,
fixtures.SCRIPT_CONTAINING_DECOYS
);

expect(extractedStrings.length).to.be.equal(1);
expect(extractedStrings[0].msgid).to.be.equal('Hello world from the $gettext function');
});
});
});
12 changes: 12 additions & 0 deletions src/test-fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,18 @@ exports.SCRIPT_USING_NGETTEXT = `
}
`;

exports.SCRIPT_CONTAINING_DECOYS = `
import $gettext from '@helper/gettext';
export default {
name: "$gettext", // because some people are actually weird
computed: {
message() {
return this.$gettext('Hello world from the $gettext function');
}
}
}`;

exports.POT_OUTPUT_0 = `msgid ""
msgstr ""
"Content-Type: text/plain; charset=utf-8\\n"
Expand Down

0 comments on commit c6d7e4d

Please sign in to comment.