-
Notifications
You must be signed in to change notification settings - Fork 47.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add transform react version pragma for devtools
- Loading branch information
Showing
5 changed files
with
260 additions
and
1 deletion.
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,9 @@ | ||
'use strict'; | ||
|
||
const pathToTransformReactVersionPragma = require.resolve( | ||
'../../../../scripts/babel/transform-react-version-pragma', | ||
); | ||
|
||
module.exports = { | ||
devtoolsPlugins: [pathToTransformReactVersionPragma], | ||
}; |
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
113 changes: 113 additions & 0 deletions
113
packages/react-devtools-shared/src/__tests__/transform-react-version-pragma-test.js
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,113 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
'use strict'; | ||
|
||
const semver = require('semver'); | ||
|
||
let shouldPass; | ||
let isFocused; | ||
describe('transform-react-version-pragma', () => { | ||
// eslint-disable-next-line no-unused-vars | ||
const _test_react_version = (range, testName, cb) => { | ||
test(testName, (...args) => { | ||
shouldPass = semver.satisfies('18.0.0', range); | ||
return cb(...args); | ||
}); | ||
}; | ||
|
||
// eslint-disable-next-line no-unused-vars | ||
const _test_react_version_focus = (range, testName, cb) => { | ||
test(testName, (...args) => { | ||
shouldPass = semver.satisfies('18.0.0', range); | ||
isFocused = true; | ||
return cb(...args); | ||
}); | ||
}; | ||
|
||
beforeEach(() => { | ||
shouldPass = null; | ||
isFocused = false; | ||
}); | ||
|
||
// @reactVersion >= 17.9 | ||
test('reactVersion flag is on >=', () => { | ||
expect(shouldPass).toBe(true); | ||
}); | ||
|
||
// @reactVersion >= 18.1 | ||
test('reactVersion flag is off >=', () => { | ||
expect(shouldPass).toBe(false); | ||
}); | ||
|
||
// @reactVersion <= 18.1 | ||
test('reactVersion flag is on <=', () => { | ||
expect(shouldPass).toBe(true); | ||
}); | ||
|
||
// @reactVersion <= 17.9 | ||
test('reactVersion flag is off <=', () => { | ||
expect(shouldPass).toBe(false); | ||
}); | ||
|
||
// @reactVersion > 17.9 | ||
test('reactVersion flag is on >', () => { | ||
expect(shouldPass).toBe(true); | ||
}); | ||
|
||
// @reactVersion > 18.1 | ||
test('reactVersion flag is off >', () => { | ||
expect(shouldPass).toBe(false); | ||
}); | ||
|
||
// @reactVersion < 18.1 | ||
test('reactVersion flag is on <', () => { | ||
expect(shouldPass).toBe(true); | ||
}); | ||
|
||
// @reactVersion < 17.0.0 | ||
test('reactVersion flag is off <', () => { | ||
expect(shouldPass).toBe(false); | ||
}); | ||
|
||
// @reactVersion = 18.0 | ||
test('reactVersion flag is on =', () => { | ||
expect(shouldPass).toBe(true); | ||
}); | ||
|
||
// @reactVersion = 18.1 | ||
test('reactVersion flag is off =', () => { | ||
expect(shouldPass).toBe(false); | ||
}); | ||
|
||
/* eslint-disable jest/no-focused-tests */ | ||
|
||
// @reactVersion >= 18.1 | ||
fit('reactVersion fit', () => { | ||
expect(shouldPass).toBe(false); | ||
expect(isFocused).toBe(true); | ||
}); | ||
|
||
// @reactVersion <= 18.1 | ||
test.only('reactVersion test.only', () => { | ||
expect(shouldPass).toBe(true); | ||
expect(isFocused).toBe(true); | ||
}); | ||
|
||
// @reactVersion <= 18.1 | ||
// @reactVersion <= 17.1 | ||
test.only('reactVersion multiple pragmas fail', () => { | ||
expect(shouldPass).toBe(false); | ||
expect(isFocused).toBe(true); | ||
}); | ||
|
||
// @reactVersion <= 18.1 | ||
// @reactVersion >= 17.1 | ||
test.only('reactVersion multiple pragmas pass', () => { | ||
expect(shouldPass).toBe(true); | ||
expect(isFocused).toBe(true); | ||
}); | ||
}); |
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,107 @@ | ||
'use strict'; | ||
|
||
/* eslint-disable no-for-of-loops/no-for-of-loops */ | ||
|
||
const GATE_VERSION_STR = '@reactVersion '; | ||
|
||
function transform(babel) { | ||
const {types: t} = babel; | ||
|
||
// Runs tests conditionally based on the version of react we are running | ||
// Input: | ||
// @reactVersion SEMVER_RANGE | ||
// test('some test', () => {/*...*/}) | ||
// | ||
// Output: | ||
// @reactVersion SEMVER_RANGE | ||
// _test_react_version(SEMVER_RANGE, 'some test', () => {/*...*/}); | ||
// | ||
// See info about semver ranges here: | ||
// https://www.npmjs.com/package/semver | ||
function buildGateVersionCondition(comments) { | ||
let conditions = null; | ||
for (const line of comments) { | ||
const commentStr = line.value.trim(); | ||
if (commentStr.startsWith(GATE_VERSION_STR)) { | ||
const condition = t.stringLiteral( | ||
commentStr.slice(GATE_VERSION_STR.length) | ||
); | ||
if (conditions === null) { | ||
conditions = [condition]; | ||
} else { | ||
conditions.push(condition); | ||
} | ||
} | ||
} | ||
|
||
if (conditions !== null) { | ||
let condition = conditions[0]; | ||
for (let i = 1; i < conditions.length; i++) { | ||
const right = conditions[i]; | ||
condition = t.logicalExpression('&&', condition, right); | ||
} | ||
return condition; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
return { | ||
name: 'transform-react-version-pragma', | ||
visitor: { | ||
ExpressionStatement(path) { | ||
const statement = path.node; | ||
const expression = statement.expression; | ||
if (expression.type === 'CallExpression') { | ||
const callee = expression.callee; | ||
switch (callee.type) { | ||
case 'Identifier': { | ||
if ( | ||
callee.name === 'test' || | ||
callee.name === 'it' || | ||
callee.name === 'fit' | ||
) { | ||
const comments = statement.leadingComments; | ||
if (comments !== undefined) { | ||
const condition = buildGateVersionCondition(comments); | ||
if (condition !== null) { | ||
callee.name = | ||
callee.name === 'fit' | ||
? '_test_react_version_focus' | ||
: '_test_react_version'; | ||
expression.arguments = [condition, ...expression.arguments]; | ||
} | ||
} | ||
} | ||
break; | ||
} | ||
case 'MemberExpression': { | ||
if ( | ||
callee.object.type === 'Identifier' && | ||
(callee.object.name === 'test' || | ||
callee.object.name === 'it') && | ||
callee.property.type === 'Identifier' && | ||
callee.property.name === 'only' | ||
) { | ||
const comments = statement.leadingComments; | ||
if (comments !== undefined) { | ||
const condition = buildGateVersionCondition(comments); | ||
if (condition !== null) { | ||
statement.expression = t.callExpression( | ||
t.identifier('_test_react_version_focus'), | ||
[condition, ...expression.arguments] | ||
); | ||
} | ||
} | ||
} | ||
break; | ||
} | ||
} | ||
} | ||
return; | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
module.exports = transform; |
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