1 | "use strict";
|
2 |
|
3 | exports.__esModule = true;
|
4 | exports.default = addPragma;
|
5 |
|
6 | var _helperModuleImports = require("@babel/helper-module-imports");
|
7 |
|
8 | var _template = _interopRequireDefault(require("@babel/template"));
|
9 |
|
10 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
11 |
|
12 | const opts = {
|
13 | placeholderPattern: false,
|
14 | placeholderWhitelist: new Set(['JSX', 'JSX_FRAG'])
|
15 | };
|
16 |
|
17 |
|
18 | const importPattern = 'import JSX from "astroturf/jsx";';
|
19 | const buildImport = (0, _template.default)(importPattern, opts);
|
20 | const requirePattern = 'const JSX = require("astroturf/jsx");';
|
21 | const buildRequire = (0, _template.default)(requirePattern, opts);
|
22 |
|
23 | function addPragma(path, JSX, insertComments = true) {
|
24 | const [builder, pattern] = (0, _helperModuleImports.isModule)(path) ? [buildImport, importPattern] : [buildRequire, requirePattern];
|
25 | const importNode = builder({
|
26 | JSX
|
27 | });
|
28 |
|
29 | const targetPath = path.get('body').find(p => {
|
30 |
|
31 | const blockHoist = p.node._blockHoist;
|
32 |
|
33 | return blockHoist != null && blockHoist < 4;
|
34 | });
|
35 | if (targetPath) targetPath.insertBefore(importNode);else path.unshiftContainer('body', importNode);
|
36 | const changes = [];
|
37 |
|
38 | if (insertComments) {
|
39 | const jsxPrgama = `* @jsx ${JSX.name} `;
|
40 | const jsxFragPrgama = `* @jsxFrag ${JSX.name}.F `;
|
41 | path.addComment('leading', '* @jsxRuntime classic');
|
42 | path.addComment('leading', jsxPrgama);
|
43 | path.addComment('leading', jsxFragPrgama);
|
44 | changes.push({
|
45 | type: 'pragma',
|
46 | code: `/** @jsxRuntime classic*/\n`,
|
47 | start: 0,
|
48 | end: 0
|
49 | }, {
|
50 | type: 'pragma',
|
51 | code: `/*${jsxPrgama}*/\n`,
|
52 | start: 0,
|
53 | end: 0
|
54 | }, {
|
55 | type: 'pragma',
|
56 | code: `/*${jsxFragPrgama}*/\n\n`,
|
57 | start: 0,
|
58 | end: 0
|
59 | });
|
60 | }
|
61 |
|
62 | changes.push({
|
63 | type: 'pragma',
|
64 | code: `${pattern.replace('JSX', JSX.name)}\n`,
|
65 | start: 0,
|
66 | end: 0
|
67 | });
|
68 | return changes;
|
69 | } |
\ | No newline at end of file |