Skip to content

Commit 5608d68

Browse files
committed
fix: remove console ninja injection
1 parent 3c29ab3 commit 5608d68

5 files changed

Lines changed: 336 additions & 112 deletions

File tree

__mocks__/Examples.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,13 @@ export const MockEvents = () => {
7575
<ComponentBox data-test="id">
7676
<DemoComponent
7777
onChange={(e) => {
78+
// comment
7879
console.log(e)
7980
}}
81+
onFocus={(e) => {
82+
/* eslint-disable */
83+
const cleaned = 'console.log(...oo_oo(`ecc41efd_0`, e))'
84+
}}
8085
/>
8186
</ComponentBox>
8287
)

__tests__/babelPluginReactLive.test.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ it('babelPluginReactLive', async () => {
3131
plugins: [[babelPluginReactLive, pluginOptions]],
3232
})
3333

34-
const code = String(babelFileResult?.code)
34+
const code = removeConsoleNinja(String(babelFileResult?.code))
3535

3636
const formattedCode = prettier.format(code, {
3737
filepath: 'file.tsx',
@@ -101,8 +101,12 @@ it('babelPluginReactLive', async () => {
101101
return (
102102
<ComponentBox data-test="id">{\`<DemoComponent
103103
onChange={(e) => {
104+
// comment
104105
console.log(e)
105106
}}
107+
onFocus={(e) => {
108+
const cleaned = 'console.log(e)'
109+
}}
106110
/>
107111
\`}</ComponentBox>
108112
)
@@ -114,3 +118,13 @@ it('babelPluginReactLive', async () => {
114118
expect(formattedCode.match(/\{`/g)).toHaveLength(7)
115119
expect(formattedCode.match(/`\}/g)).toHaveLength(7)
116120
})
121+
122+
function removeConsoleNinja(code) {
123+
if (code.includes('oo_cm')) {
124+
const index = code.indexOf('function oo_cm()')
125+
code = code.slice(0, index)
126+
code = code.replace(/(\/\* eslint-disable \*\/(\n|\s|)+)/g, '')
127+
}
128+
129+
return code
130+
}

babelPluginReactLive.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,18 @@ function babelPluginReactLive(babel, options) {
164164
function astToCode(ast) {
165165
const { code } = generate(ast)
166166

167-
return code.replace(/;$/, '')
167+
return removeConsoleNinja(code.replace(/;$/, ''))
168+
}
169+
170+
function removeConsoleNinja(code) {
171+
if (code.includes('oo_oo')) {
172+
code = code.replace(
173+
/(\/\* eslint-disable \*\/)(\n|\s|)+(.*\()(\n|\s|)+...oo_oo\(`.*`,(\s|)((.*)|(.*\n.*))\)/g,
174+
'$3$6'
175+
)
176+
}
177+
178+
return code
168179
}
169180

170181
module.exports = babelPluginReactLive

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"type": "git",
1414
"url": "https://github.com/dnbexperience/eufemia.git"
1515
},
16-
"version": "1.4.0",
16+
"version": "1.4.1",
1717
"main": "babelPluginReactLive.js",
1818
"scripts": {
1919
"test": "jest",

0 commit comments

Comments
 (0)