Skip to content

Commit

Permalink
feat: Don't allow children of choose elements, closes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
tricoder42 committed Mar 6, 2017
1 parent 309b291 commit 87b6be7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/babel-plugin-lingui-transform-react/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ export default function ({ types: t }) {

// Plural, Select, SelectOrdinal
} else if (isChooseElement(node)) {
// TODO: Disallow children
if (node.children.length) {
throw file.buildCodeFrameError(element, `Children of ${element.name.name} aren't allowed.`)
}

const choicesType = element.name.name.toLowerCase()
const choices = {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ exports[`babel-plugin-lingui-transform-react validation Date/Number value must b

exports[`babel-plugin-lingui-transform-react validation Date/Number value must be a variable 4`] = `"packages/babel-plugin-lingui-transform-react/test/fixtures/nested-components/actual.js: Value must be a variable."`;

exports[`babel-plugin-lingui-transform-react validation Plural/Select/SelectOrdinal children are not allowed 1`] = `"packages/babel-plugin-lingui-transform-react/test/fixtures/nested-components/actual.js: Children of Plural aren't allowed."`;

exports[`babel-plugin-lingui-transform-react validation Plural/Select/SelectOrdinal children are not allowed 2`] = `"packages/babel-plugin-lingui-transform-react/test/fixtures/nested-components/actual.js: Children of Select aren't allowed."`;

exports[`babel-plugin-lingui-transform-react validation Plural/Select/SelectOrdinal children are not allowed 3`] = `"packages/babel-plugin-lingui-transform-react/test/fixtures/nested-components/actual.js: Children of SelectOrdinal aren't allowed."`;

exports[`babel-plugin-lingui-transform-react validation Plural/Select/SelectOrdinal offset must be number or string, not variable 1`] = `"packages/babel-plugin-lingui-transform-react/test/fixtures/nested-components/actual.js: Offset argument cannot be a variable."`;

exports[`babel-plugin-lingui-transform-react validation Plural/Select/SelectOrdinal plural forms are missing 1`] = `"packages/babel-plugin-lingui-transform-react/test/fixtures/nested-components/actual.js: Missing plural choices. At least fallback argument 'other' is required."`;
Expand Down
6 changes: 6 additions & 0 deletions packages/babel-plugin-lingui-transform-react/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ describe('babel-plugin-lingui-transform-react', function () {

describe('validation', function () {
describe('Plural/Select/SelectOrdinal', function () {
it('children are not allowed', function () {
expect(transformCode('<Plural>Not allowed</Plural>')).toThrowErrorMatchingSnapshot()
expect(transformCode('<Select>Not allowed</Select>')).toThrowErrorMatchingSnapshot()
expect(transformCode('<SelectOrdinal>Not allowed</SelectOrdinal>')).toThrowErrorMatchingSnapshot()
})

it('value must be a variable', function () {
const code = `<Plural value="42" one="Book" other="Books" />`
expect(transformCode(code)).toThrowErrorMatchingSnapshot()
Expand Down

0 comments on commit 87b6be7

Please sign in to comment.