babel-plugin-transform-taroapi 3.6.4 → 3.6.5-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,66 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`should leave other apis untouched 1`] = `
4
+ "import Taro from '@tarojs/taro-h5';
5
+ Taro.noop;"
6
+ `;
7
+
8
+ exports[`should move static apis under "Taro" 1`] = `
9
+ "import Taro from '@tarojs/taro-h5';
10
+ Taro.noop;
11
+ Taro.noop();"
12
+ `;
13
+
14
+ exports[`should not go wrong when using an api twice 1`] = `
15
+ "import Taro, { createAnimation as _createAnimation } from '@tarojs/taro-h5';
16
+ const animation = _createAnimation({
17
+ duration: dura * 1000,
18
+ timingFunction: 'linear'
19
+ });
20
+ const resetAnimation = _createAnimation({
21
+ duration: 0,
22
+ timingFunction: 'linear'
23
+ });"
24
+ `;
25
+
26
+ exports[`should not import taro duplicatly 1`] = `
27
+ "import Taro, { createAnimation as _createAnimation, initPxTransform as _initPxTransform } from "@tarojs/taro-h5";
28
+ Taro.Component;
29
+ _createAnimation();
30
+ _initPxTransform();"
31
+ `;
32
+
33
+ exports[`should preserve assignments in lefthands 1`] = `
34
+ "import Taro, { createAnimation as _createAnimation, request as _request } from '@tarojs/taro-h5';
35
+ let animation;
36
+ animation = _createAnimation({
37
+ transformOrigin: "50% 50%",
38
+ duration: 1000,
39
+ timingFunction: "ease",
40
+ delay: 0
41
+ });
42
+ _request();
43
+ Taro.request = '';
44
+ Taro['request'] = '';"
45
+ `;
46
+
47
+ exports[`should preserve default imports 1`] = `
48
+ "import Taro from '@tarojs/taro-h5';
49
+ console.log(Taro);"
50
+ `;
51
+
52
+ exports[`should support rename of imported names 1`] = `
53
+ "// import { inject as mobxInject, observer as mobxObserver } from '@tarojs/mobx'
54
+ import Taro from "@tarojs/taro-h5";
55
+ export class Connected extends Taro.Component {}"
56
+ `;
57
+
58
+ exports[`should work! 1`] = `
59
+ "import Taro, { setStorage as _setStorage, initPxTransform as _initPxTransform, getStorage as _getStorage } from '@tarojs/taro-h5';
60
+ _initPxTransform(Taro.param);
61
+ _initPxTransform();
62
+ _initPxTransform();
63
+ _getStorage();
64
+ _setStorage();
65
+ export { Taro };"
66
+ `;
@@ -1,9 +1,11 @@
1
- import * as apis from '@tarojs/taro-h5'
2
- import * as babel from 'babel-core'
3
- import * as t from 'babel-types'
1
+ import * as babel from '@babel/core'
2
+ import * as t from '@babel/types'
3
+ import * as apis from '@tarojs/plugin-platform-h5/dist/taroApis'
4
4
 
5
5
  import plugin from '../src'
6
6
 
7
+ type ImportType = babel.types.ImportSpecifier | babel.types.ImportDefaultSpecifier | babel.types.ImportNamespaceSpecifier
8
+
7
9
  const pluginOptions = [
8
10
  plugin,
9
11
  {
@@ -11,11 +13,10 @@ const pluginOptions = [
11
13
  packageName: '@tarojs/taro-h5'
12
14
  }
13
15
  ]
14
-
15
16
  const getNamedImports = (importSpecifiers: (t.ImportSpecifier | t.ImportDefaultSpecifier | t.ImportNamespaceSpecifier)[]) => {
16
- return importSpecifiers.reduce((prev, curr: t.ImportSpecifier) => {
17
+ return importSpecifiers.reduce((prev, curr) => {
17
18
  if (t.isImportSpecifier(curr)) {
18
- prev.add(curr.imported.name)
19
+ prev.add(((curr as t.ImportSpecifier).imported as babel.types.Identifier).name)
19
20
  }
20
21
  return prev
21
22
  }, new Set())
@@ -31,8 +32,9 @@ it('should work!', function () {
31
32
  setStorage()
32
33
  export { Taro }
33
34
  `
34
- const result = babel.transform(code, { plugins: [pluginOptions] })
35
- expect(result.code).toMatchSnapshot()
35
+ const result = babel.transform(code, { ast: true, configFile: false, plugins: [pluginOptions] })
36
+
37
+ expect(result?.code).toMatchSnapshot()
36
38
  })
37
39
 
38
40
  it('should leave other apis untouched', function () {
@@ -40,24 +42,28 @@ it('should leave other apis untouched', function () {
40
42
  import Taro from '@tarojs/taro-h5'
41
43
  Taro.noop
42
44
  `
43
- const result = babel.transform(code, { plugins: [pluginOptions] })
45
+ const result = babel.transform(code, { ast: true, configFile: false, plugins: [pluginOptions] }) as babel.BabelFileResult
44
46
  expect(result.code).toMatchSnapshot()
45
47
 
46
48
  const ast = result.ast as t.File
47
49
  const body = ast.program.body as [t.ImportDeclaration, t.ExpressionStatement]
48
50
  expect(t.isImportDeclaration(body[0])).toBeTruthy()
49
51
  expect(t.isExpressionStatement(body[1])).toBeTruthy()
50
- const defaultImport = body[0].specifiers.find(v => t.isImportDefaultSpecifier(v))
52
+ const defaultImport = body[0].specifiers.find(v => t.isImportDefaultSpecifier(v)) as ImportType
51
53
  expect(defaultImport).toBeTruthy()
52
54
 
53
55
  const taroName = defaultImport.local.name
54
56
  const namedImports = getNamedImports(body[0].specifiers)
55
57
  expect(namedImports).toEqual(new Set())
56
58
  expect(t.isMemberExpression(body[1].expression)).toBeTruthy()
57
- expect((body[1].expression as t.MemberExpression)).toMatchObject(t.memberExpression(
59
+
60
+ const obj = t.memberExpression(
58
61
  t.identifier(taroName),
59
- t.identifier('noop')
60
- ))
62
+ t.identifier('noop'),
63
+ )
64
+ delete obj.optional
65
+
66
+ expect((body[1].expression as t.MemberExpression)).toMatchObject(obj)
61
67
  })
62
68
 
63
69
  it('should move static apis under "Taro"', function () {
@@ -67,10 +73,10 @@ it('should move static apis under "Taro"', function () {
67
73
  noop();
68
74
  `
69
75
 
70
- const result = babel.transform(code, { plugins: [pluginOptions] })
71
- expect(result.code).toMatchSnapshot()
76
+ const result = babel.transform(code, { ast: true, configFile: false, plugins: [pluginOptions] })
77
+ expect(result?.code).toMatchSnapshot()
72
78
 
73
- const ast = result.ast as t.File
79
+ const ast = result?.ast as t.File
74
80
  const body = ast.program.body as [t.ImportDeclaration, t.ExpressionStatement]
75
81
  expect(t.isImportDeclaration(body[0])).toBeTruthy()
76
82
  expect(t.isExpressionStatement(body[1])).toBeTruthy()
@@ -78,9 +84,9 @@ it('should move static apis under "Taro"', function () {
78
84
  expect(defaultImport).toBeTruthy()
79
85
 
80
86
  const taroName = defaultImport!.local.name
81
- let memberExpression = body[1].expression
87
+ let memberExpression: any = body[1].expression
82
88
  if (t.isCallExpression(body[1])) {
83
- memberExpression = (body[1].expression as t.CallExpression).callee
89
+ memberExpression = ((body[1] as t.ExpressionStatement).expression as t.CallExpression).callee
84
90
  }
85
91
  expect(memberExpression).toMatchObject(t.memberExpression(
86
92
  t.identifier(taroName),
@@ -97,10 +103,9 @@ it('should not import taro duplicatly', function () {
97
103
  Taro.initPxTransform()
98
104
  `
99
105
 
100
- const result = babel.transform(code, { plugins: [pluginOptions] })
101
- expect(result.code).toMatchSnapshot()
102
-
103
- const ast = result.ast as t.File
106
+ const result = babel.transform(code, { ast: true, configFile: false, plugins: [pluginOptions] })
107
+ expect(result?.code).toMatchSnapshot()
108
+ const ast = result?.ast as t.File
104
109
  const body = ast.program.body as [t.ImportDeclaration, t.ExpressionStatement, t.ExpressionStatement]
105
110
  expect(t.isImportDeclaration(body[0])).toBeTruthy()
106
111
  expect(t.isExpressionStatement(body[1])).toBeTruthy()
@@ -120,8 +125,8 @@ it('should not go wrong when using an api twice', function () {
120
125
  })
121
126
  `
122
127
  expect(() => {
123
- const result = babel.transform(code, { plugins: [pluginOptions] })
124
- expect(result.code).toMatchSnapshot()
128
+ const result = babel.transform(code, { ast: true, configFile: false, plugins: [pluginOptions] })
129
+ expect(result?.code).toMatchSnapshot()
125
130
  }).not.toThrowError()
126
131
  })
127
132
 
@@ -130,8 +135,8 @@ it('should preserve default imports', function () {
130
135
  import Taro from '@tarojs/taro-h5'
131
136
  console.log(Taro)
132
137
  `
133
- const result = babel.transform(code, { plugins: [pluginOptions] })
134
- expect(result.code).toMatchSnapshot()
138
+ const result = babel.transform(code, { ast: true, configFile: false, plugins: [pluginOptions] })
139
+ expect(result?.code).toMatchSnapshot()
135
140
  })
136
141
 
137
142
  it('should preserve assignments in lefthands', function () {
@@ -148,8 +153,8 @@ it('should preserve assignments in lefthands', function () {
148
153
  Taro.request = ''
149
154
  Taro['request'] = ''
150
155
  `
151
- const result = babel.transform(code, { plugins: [pluginOptions] })
152
- expect(result.code).toMatchSnapshot()
156
+ const result = babel.transform(code, { ast: true, configFile: false, plugins: [pluginOptions] })
157
+ expect(result?.code).toMatchSnapshot()
153
158
  })
154
159
 
155
160
  it('should support rename of imported names', function () {
@@ -158,6 +163,6 @@ it('should support rename of imported names', function () {
158
163
  import { Component as TaroComponent } from "@tarojs/taro-h5";
159
164
  export class Connected extends TaroComponent {}
160
165
  `
161
- const result = babel.transform(code, { plugins: [pluginOptions] })
162
- expect(result.code).toMatchSnapshot()
166
+ const result = babel.transform(code, { ast: true, configFile: false, plugins: [pluginOptions] })
167
+ expect(result?.code).toMatchSnapshot()
163
168
  })
@@ -0,0 +1,19 @@
1
+ {
2
+ "presets": [
3
+ [
4
+ "@babel/preset-env",
5
+ {
6
+ "targets": {
7
+ "ios": "9",
8
+ "android": "4.4"
9
+ }
10
+ }
11
+ ]
12
+ ],
13
+ "plugins": ["@babel/plugin-transform-react-jsx"],
14
+ "env": {
15
+ "test": {
16
+ "plugins": ["@babel/plugin-transform-runtime"]
17
+ }
18
+ }
19
+ }
package/jest.config.js ADDED
@@ -0,0 +1,20 @@
1
+ module.exports = {
2
+ collectCoverage: false,
3
+ moduleFileExtensions: ['js', 'jsx', 'ts', 'tsx', 'json', 'node'],
4
+ preset: 'ts-jest',
5
+ testEnvironment: 'node',
6
+ testMatch: ['**/__tests__/**/?(*.)+(spec|test).[jt]s?(x)'],
7
+ testPathIgnorePatterns: [
8
+ 'node_modules',
9
+ ],
10
+ transform: {
11
+ '^.+\\.jsx?$': 'babel-jest',
12
+ '^.+\\.tsx?$': ['ts-jest', {
13
+ diagnostics: false,
14
+ tsconfig: {
15
+ allowJs: true
16
+ }
17
+ }],
18
+ },
19
+ transformIgnorePatterns: ['<rootDir>/node_modules/']
20
+ }
package/package.json CHANGED
@@ -1,9 +1,23 @@
1
1
  {
2
2
  "name": "babel-plugin-transform-taroapi",
3
- "version": "3.6.4",
3
+ "version": "3.6.5-alpha.1",
4
4
  "main": "dist/index.js",
5
+ "devDependencies": {
6
+ "@babel/core": "^7.14.5",
7
+ "@babel/types": "^7.14.5",
8
+ "babel-jest": "^29.5.0",
9
+ "jest": "^29.3.1",
10
+ "jest-cli": "^29.3.1",
11
+ "ts-jest": "^29.0.5",
12
+ "typescript": "^4.7.4"
13
+ },
5
14
  "license": "MIT",
6
15
  "scripts": {
7
- "build": "tsc"
16
+ "build": "tsc",
17
+ "test": "cross-env NODE_ENV=jest jest",
18
+ "test:ci": "cross-env NODE_ENV=jest jest --ci -i",
19
+ "test:dev": "cross-env NODE_ENV=jest jest --watch",
20
+ "test:coverage": "cross-env NODE_ENV=jest jest --coverage",
21
+ "updateSnapshot": "cross-env NODE_ENV=jest jest --updateSnapshot"
8
22
  }
9
23
  }
@@ -0,0 +1,6 @@
1
+ {
2
+ "extends": "../../tsconfig.root.json",
3
+ "compilerOptions": {
4
+ "allowJs": true
5
+ }
6
+ }