1 | 'use strict';
|
2 |
|
3 | const template = require('babel-template');
|
4 |
|
5 | const { TEST_ID } = require('./const');
|
6 | const { getName, getId, getNode } = require('./utils');
|
7 |
|
8 | const build = template(`
|
9 | COMPONENT.NAME = ID;
|
10 | `);
|
11 |
|
12 | module.exports = ({ types: t }) => ({
|
13 | visitor: {
|
14 | JSXElement(p, { file }) {
|
15 | const { openingElement, rootPath, componentNode } = getNode(t, p) || {};
|
16 |
|
17 | if (!openingElement) return;
|
18 |
|
19 | const { filename } = file.opts;
|
20 | const name = getName({ rootPath, componentNode });
|
21 | const id = getId(filename, name);
|
22 |
|
23 | openingElement.attributes.push(t.JSXAttribute(t.JSXIdentifier(`data-${id}`)));
|
24 |
|
25 | rootPath.insertAfter(build({
|
26 | COMPONENT: t.identifier(name === 'default' ? `exports.${name}` : name),
|
27 | ID: t.StringLiteral(id),
|
28 | NAME: t.identifier(TEST_ID)
|
29 | }));
|
30 | }
|
31 | }
|
32 | }); |
\ | No newline at end of file |