Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(babel-preset,shaker): do not delete exported functions even if it…
…s body was deleted fixes #1237
- Loading branch information
Showing
14 changed files
with
385 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
'@linaria/babel-preset': patch | ||
'@linaria/shaker': patch | ||
'@linaria/testkit': patch | ||
'@linaria/utils': patch | ||
--- | ||
|
||
Fix module function deletion when containing restricted code (fixes #1226) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
packages/testkit/src/__fixtures__/prepare-code-test-cases/.eslintignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
**/*.js | ||
**/*.ts | ||
**/*.tsx |
90 changes: 90 additions & 0 deletions
90
packages/testkit/src/__fixtures__/prepare-code-test-cases/for-debug/input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// slugify | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, '__esModule', { | ||
value: true, | ||
}); | ||
var _exportNames = { | ||
asyncResolveFallback: true, | ||
collectExportsAndImports: true, | ||
slugify: true, | ||
}; | ||
Object.defineProperty(exports, 'asyncResolveFallback', { | ||
enumerable: true, | ||
get: function () { | ||
return _asyncResolveFallback.default; | ||
}, | ||
}); | ||
Object.defineProperty(exports, 'collectExportsAndImports', { | ||
enumerable: true, | ||
get: function () { | ||
return _collectExportsAndImports.default; | ||
}, | ||
}); | ||
Object.defineProperty(exports, 'slugify', { | ||
enumerable: true, | ||
get: function () { | ||
return _slugify.default; | ||
}, | ||
}); | ||
var _asyncResolveFallback = _interopRequireWildcard( | ||
require('./asyncResolveFallback') | ||
); | ||
var _collectExportsAndImports = _interopRequireWildcard( | ||
require('./collectExportsAndImports') | ||
); | ||
Object.keys(_collectExportsAndImports).forEach(function (key) { | ||
if (key === 'default' || key === '__esModule') return; | ||
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return; | ||
if (key in exports && exports[key] === _collectExportsAndImports[key]) return; | ||
Object.defineProperty(exports, key, { | ||
enumerable: true, | ||
get: function () { | ||
return _collectExportsAndImports[key]; | ||
}, | ||
}); | ||
}); | ||
var _slugify = _interopRequireDefault(require('./slugify')); | ||
function _interopRequireDefault(obj) { | ||
return obj && obj.__esModule ? obj : { default: obj }; | ||
} | ||
function _getRequireWildcardCache(nodeInterop) { | ||
if (typeof WeakMap !== 'function') return null; | ||
var cacheBabelInterop = new WeakMap(); | ||
var cacheNodeInterop = new WeakMap(); | ||
return (_getRequireWildcardCache = function (nodeInterop) { | ||
return nodeInterop ? cacheNodeInterop : cacheBabelInterop; | ||
})(nodeInterop); | ||
} | ||
function _interopRequireWildcard(obj, nodeInterop) { | ||
if (!nodeInterop && obj && obj.__esModule) { | ||
return obj; | ||
} | ||
if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) { | ||
return { default: obj }; | ||
} | ||
var cache = _getRequireWildcardCache(nodeInterop); | ||
if (cache && cache.has(obj)) { | ||
return cache.get(obj); | ||
} | ||
var newObj = {}; | ||
var hasPropertyDescriptor = | ||
Object.defineProperty && Object.getOwnPropertyDescriptor; | ||
for (var key in obj) { | ||
if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) { | ||
var desc = hasPropertyDescriptor | ||
? Object.getOwnPropertyDescriptor(obj, key) | ||
: null; | ||
if (desc && (desc.get || desc.set)) { | ||
Object.defineProperty(newObj, key, desc); | ||
} else { | ||
newObj[key] = obj[key]; | ||
} | ||
} | ||
} | ||
newObj.default = obj; | ||
if (cache) { | ||
cache.set(obj, newObj); | ||
} | ||
return newObj; | ||
} |
39 changes: 39 additions & 0 deletions
39
packages/testkit/src/__snapshots__/prepareCode.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`prepareCode Testing transformation for for-debug: code 1`] = ` | ||
"'use strict'; | ||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
}); | ||
Object.defineProperty(exports, 'asyncResolveFallback', { | ||
enumerable: true, | ||
get: function get() {} | ||
}); | ||
Object.defineProperty(exports, 'collectExportsAndImports', { | ||
enumerable: true, | ||
get: function get() {} | ||
}); | ||
Object.defineProperty(exports, 'slugify', { | ||
enumerable: true, | ||
get: function get() { | ||
return _slugify.default; | ||
} | ||
}); | ||
var _slugify = _interopRequireDefault(require('./slugify')); | ||
function _interopRequireDefault(obj) { | ||
return obj && obj.__esModule ? obj : { | ||
default: obj | ||
}; | ||
}" | ||
`; | ||
|
||
exports[`prepareCode Testing transformation for for-debug: imports 1`] = ` | ||
Map { | ||
"./slugify" => Array [ | ||
"default", | ||
], | ||
} | ||
`; | ||
|
||
exports[`prepareCode Testing transformation for for-debug: metadata 1`] = `Object {}`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { existsSync, readFileSync, readdirSync, statSync } from 'fs'; | ||
import { join } from 'path'; | ||
|
||
import * as babel from '@babel/core'; | ||
|
||
import { prepareCode } from '@linaria/babel-preset'; | ||
|
||
const testCasesDir = join(__dirname, '__fixtures__', 'prepare-code-test-cases'); | ||
|
||
const testCaseFolders = readdirSync(testCasesDir).filter((file) => | ||
statSync(join(testCasesDir, file)).isDirectory() | ||
); | ||
|
||
const rules = [ | ||
{ | ||
test: () => true, | ||
action: require('@linaria/shaker').default, | ||
}, | ||
]; | ||
|
||
const pluginOptions = { | ||
rules, | ||
babelOptions: { | ||
babelrc: false, | ||
configFile: false, | ||
presets: [ | ||
['@babel/preset-env', { loose: true }], | ||
'@babel/preset-react', | ||
'@babel/preset-typescript', | ||
], | ||
}, | ||
}; | ||
|
||
const extensions = ['ts', 'tsx', 'js', 'jsx']; | ||
|
||
describe('prepareCode', () => { | ||
testCaseFolders.forEach((testCaseFolder) => { | ||
test(`Testing transformation for ${testCaseFolder}`, () => { | ||
const root = join(testCasesDir, testCaseFolder); | ||
const inputFileWithoutExtension = join(root, 'input'); | ||
const inputFilePath = extensions | ||
.map((ext) => `${inputFileWithoutExtension}.${ext}`) | ||
.find((path) => existsSync(path))!; | ||
|
||
const input = readFileSync(inputFilePath, 'utf8'); | ||
const [firstLine, ...restLines] = input.split('\n'); | ||
const only = firstLine | ||
.slice(2) | ||
.split(',') | ||
.map((s) => s.trim()); | ||
|
||
const [transformedCode, imports, metadata] = prepareCode( | ||
babel, | ||
inputFilePath, | ||
restLines.join('\n'), | ||
only, | ||
{ | ||
root, | ||
pluginOptions, | ||
} | ||
); | ||
|
||
expect(transformedCode).toMatchSnapshot('code'); | ||
expect(imports).toMatchSnapshot('imports'); | ||
expect(metadata).toMatchSnapshot('metadata'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.