Skip to content

Commit

Permalink
Handle nodes with null loc attribute (SourceLocation), add test (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmark authored and oliviertassinari committed Nov 7, 2019
1 parent d73a2a3 commit f71a197
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/extractFromCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,11 @@ export default function extractFromCode(code, options = {}) {
CallExpression(path) {
const { node } = path;

if (ignoredLines.includes(node.loc.end.line)) {
// Skip ignored lines
return;
if (node.loc) {
if (ignoredLines.includes(node.loc.end.line)) {
// Skip ignored lines
return;
}
}

const {
Expand Down
52 changes: 52 additions & 0 deletions src/extractFromCode.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -802,4 +802,56 @@ describe('#extractFromCode()', () => {
);
});
});

describe('ast parsing', () => {
// See: https://github.com/oliviertassinari/i18n-extract/pull/62
it('can handle nodes with a null loc attribute (SourceLocation)', () => {
/**
* This plugin will mutate the ast to add a new node.
* The node created won't have an 'loc' (SourceLocation) attribute.
*
* According to https://github.com/babel/babel/blob/master/packages/babel-parser/ast/spec.md#node-objects,
* that's fine, and we should be able to handle this.
*/
function dynamicallyAddTranslation(babel) {
const { types: t } = babel;
return {
visitor: {
ImportDeclaration(p) {
p.insertAfter(t.callExpression(t.identifier('__'), [t.stringLiteral('foo')]));
},
},
};
}

const keys = extractFromCode(getCode('marker.js'), {
marker: '__',
babelOptions: { ast: true, plugins: [dynamicallyAddTranslation] },
});

assert.deepEqual(
[
{
key: 'foo',
loc: undefined,
},
{
key: 'this_is_a_custom_marker',
loc: {
end: {
column: 56,
line: 5,
},
start: {
column: 0,
line: 5,
},
},
},
],
keys,
'Should take into account the marker option.',
);
});
});
});

0 comments on commit f71a197

Please sign in to comment.