From c5927b7d4591646454e2ae1e90223befd15148fe Mon Sep 17 00:00:00 2001 From: Ydream <995862798@qq.com> Date: Tue, 12 Apr 2022 12:11:07 +0800 Subject: [PATCH 1/3] =?UTF-8?q?fix(plugin-dva):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E5=90=88=E6=B3=95=E6=80=A7=E6=A3=80=E6=B5=8B=E4=B8=8D=E6=94=AF?= =?UTF-8?q?=E6=8C=81jsx=E8=AF=AD=E6=B3=95=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/plugin-dva/src/getModels/isValidModel.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/plugin-dva/src/getModels/isValidModel.ts b/packages/plugin-dva/src/getModels/isValidModel.ts index e044c42f0..fe3e7afe1 100644 --- a/packages/plugin-dva/src/getModels/isValidModel.ts +++ b/packages/plugin-dva/src/getModels/isValidModel.ts @@ -30,7 +30,6 @@ function getTSNode(node: any) { } export function isValidModel({ content }: { content: string }) { - const { parser } = utils; const ast = parser.parse(content, { sourceType: 'module', plugins: [ @@ -44,6 +43,7 @@ export function isValidModel({ content }: { content: string }) { 'objectRestSpread', 'optionalChaining', 'decorators-legacy', + 'jsx', ], }); From 1079b3ebcc5f9a94c569c93be9663213a549363e Mon Sep 17 00:00:00 2001 From: Ydream <995862798@qq.com> Date: Tue, 12 Apr 2022 15:35:36 +0800 Subject: [PATCH 2/3] =?UTF-8?q?fix(plugin-dva):=20=E8=A7=A3=E5=86=B3=20jsx?= =?UTF-8?q?=20=E8=AF=AD=E6=B3=95=E5=92=8C=20ts=20=E5=B0=96=E6=8B=AC?= =?UTF-8?q?=E5=8F=B7=E5=9E=8B=E7=B1=BB=E5=9E=8B=E6=96=AD=E8=A8=80=E7=9A=84?= =?UTF-8?q?=E5=86=B2=E7=AA=81=E9=97=AE=E9=A2=98=EF=BC=9A=20=E5=85=81?= =?UTF-8?q?=E8=AE=B8=E5=9C=A8=20ts=20=E6=96=87=E4=BB=B6=E4=B8=AD=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E5=B0=96=E6=8B=AC=E5=8F=B7=E7=B1=BB=E5=9E=8B=E6=96=AD?= =?UTF-8?q?=E8=A8=80=EF=BC=8Cjsx=20=E6=96=87=E4=BB=B6=E4=B8=AD=E9=81=87?= =?UTF-8?q?=E5=88=B0=E5=86=B2=E7=AA=81=E8=BF=9B=E8=A1=8C=E6=8A=A5=E9=94=99?= =?UTF-8?q?=EF=BC=8C=E5=B9=B6=E4=B8=94=E7=BB=99=E5=87=BA=E6=8F=90=E7=A4=BA?= =?UTF-8?q?=E3=80=82=20=E5=A2=9E=E5=8A=A0=E4=BA=86=20jsx=20=E7=9A=84?= =?UTF-8?q?=E5=8D=95=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugin-dva/src/getModels/getModels.ts | 24 +++++++++++++++---- .../src/getModels/isValidModel.test.ts | 20 ++++++++++++++++ .../plugin-dva/src/getModels/isValidModel.ts | 5 ++-- 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/packages/plugin-dva/src/getModels/getModels.ts b/packages/plugin-dva/src/getModels/getModels.ts index d00e8c04b..34e009830 100644 --- a/packages/plugin-dva/src/getModels/getModels.ts +++ b/packages/plugin-dva/src/getModels/getModels.ts @@ -27,16 +27,32 @@ export function getModels(opts: { // 允许通过配置下跳过 Model 校验 if (opts.skipModelValidate) return true; + const isJsx = /.(j|t)sx$/.test(f); // TODO: fs cache for performance try { - return isValidModel({ - content: readFileSync(f, 'utf-8'), - }); + console.log( + f, + isValidModel( + { + content: readFileSync(f, 'utf-8'), + }, + isJsx, + ), + ); + return isValidModel( + { + content: readFileSync(f, 'utf-8'), + }, + isJsx, + ); } catch (error) { throw new Error( `Dva model ${utils.winPath( relative(opts.cwd, f), - )} parse failed, ${error}`, + )} parse failed, ${error} ${ + isJsx && + 'Maybe you use type assertions that would be ambiguous with JSX' + }`, ); } }); diff --git a/packages/plugin-dva/src/getModels/isValidModel.test.ts b/packages/plugin-dva/src/getModels/isValidModel.test.ts index 8077bb55e..843579e7a 100644 --- a/packages/plugin-dva/src/getModels/isValidModel.test.ts +++ b/packages/plugin-dva/src/getModels/isValidModel.test.ts @@ -86,3 +86,23 @@ export default foo(model, { namespace: 'foo' }); }), ).toEqual(false); }); + +test('isValidModel with jsx', () => { + expect( + isValidModel( + { + content: ` +const t = () =>
; +export default { + reducers: { + add(){ + t(); + } + } +} +`, + }, + true, + ), + ).toEqual(true); +}); diff --git a/packages/plugin-dva/src/getModels/isValidModel.ts b/packages/plugin-dva/src/getModels/isValidModel.ts index fe3e7afe1..2687b6f34 100644 --- a/packages/plugin-dva/src/getModels/isValidModel.ts +++ b/packages/plugin-dva/src/getModels/isValidModel.ts @@ -29,7 +29,7 @@ function getTSNode(node: any) { } } -export function isValidModel({ content }: { content: string }) { +export function isValidModel({ content }: { content: string }, isJsx = false) { const ast = parser.parse(content, { sourceType: 'module', plugins: [ @@ -43,8 +43,7 @@ export function isValidModel({ content }: { content: string }) { 'objectRestSpread', 'optionalChaining', 'decorators-legacy', - 'jsx', - ], + ].concat(isJsx ? ['jsx'] : []), }); let isDvaModel = false; From c17816f3a9790e37cf8ec2d6ca8bf6e7569ff515 Mon Sep 17 00:00:00 2001 From: Ydream <995862798@qq.com> Date: Tue, 12 Apr 2022 15:48:30 +0800 Subject: [PATCH 3/3] =?UTF-8?q?fix(plugin-dva):=20=E5=88=A0=E9=99=A4=20get?= =?UTF-8?q?Models=20=E4=B8=AD=E5=85=B3=E4=BA=8E=20jsx=20=E7=9A=84=E5=8D=95?= =?UTF-8?q?=E6=B5=8B=EF=BC=8C=E5=A2=9E=E5=8A=A0=20jsx=20=E4=B8=AD=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E5=B0=96=E6=8B=AC=E5=8F=B7=E7=B1=BB=E5=9E=8B=E6=96=AD?= =?UTF-8?q?=E8=A8=80=E7=9A=84=E5=8D=95=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/getModels/fixtures/jsx/b.jsx | 8 +++++++ .../src/getModels/getModels.test.ts | 22 ++++++++++++++++--- .../plugin-dva/src/getModels/getModels.ts | 9 -------- 3 files changed, 27 insertions(+), 12 deletions(-) create mode 100644 packages/plugin-dva/src/getModels/fixtures/jsx/b.jsx diff --git a/packages/plugin-dva/src/getModels/fixtures/jsx/b.jsx b/packages/plugin-dva/src/getModels/fixtures/jsx/b.jsx new file mode 100644 index 000000000..a8891edad --- /dev/null +++ b/packages/plugin-dva/src/getModels/fixtures/jsx/b.jsx @@ -0,0 +1,8 @@ +import React from 'react'; + +const A = () =>
123
+const AbcModel = { + namespace: 'a', +}; + +export default AbcModel; diff --git a/packages/plugin-dva/src/getModels/getModels.test.ts b/packages/plugin-dva/src/getModels/getModels.test.ts index 3e24a5ca2..d22d17e07 100644 --- a/packages/plugin-dva/src/getModels/getModels.test.ts +++ b/packages/plugin-dva/src/getModels/getModels.test.ts @@ -62,9 +62,25 @@ test('getModels with opts.extraModels and opts.skipModelValidate', () => { ]); }); -test('parser error when has jsx', () => { +// test('parser error when has jsx', () => { +// const base = join(fixtures, 'jsx'); +// const filePath = join(base, 'a.jsx'); +// expect(() => { +// getModels({ +// base, +// cwd: __dirname, +// skipModelValidate: false, +// }); +// }).toThrow( +// `Dva model ${utils.winPath( +// relative(__dirname, filePath), +// )} parse failed, SyntaxError: Unterminated regular expression. (3:26)`, +// ); +// }); + +test('parser error when ambiguous with jsx', () => { const base = join(fixtures, 'jsx'); - const filePath = join(base, 'a.jsx'); + const filePath = join(base, 'b.jsx'); expect(() => { getModels({ base, @@ -74,6 +90,6 @@ test('parser error when has jsx', () => { }).toThrow( `Dva model ${utils.winPath( relative(__dirname, filePath), - )} parse failed, SyntaxError: Unterminated regular expression. (3:26)`, + )} parse failed, SyntaxError: Unterminated JSX contents. (5:21) Maybe you use type assertions that would be ambiguous with JSX`, ); }); diff --git a/packages/plugin-dva/src/getModels/getModels.ts b/packages/plugin-dva/src/getModels/getModels.ts index 34e009830..be094b6e0 100644 --- a/packages/plugin-dva/src/getModels/getModels.ts +++ b/packages/plugin-dva/src/getModels/getModels.ts @@ -30,15 +30,6 @@ export function getModels(opts: { const isJsx = /.(j|t)sx$/.test(f); // TODO: fs cache for performance try { - console.log( - f, - isValidModel( - { - content: readFileSync(f, 'utf-8'), - }, - isJsx, - ), - ); return isValidModel( { content: readFileSync(f, 'utf-8'),