forked from keystonejs/keystone
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow and replace
process.env.SHOW_NEXT_RELEASE
in TypeScript files…
… for the website (keystonejs#7990)
- Loading branch information
Showing
11 changed files
with
308 additions
and
16 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
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,2 @@ | ||
// @skipShowNextReleaseReplacement | ||
export const showNextReleaseWithoutReplacement = !!process.env.SHOW_NEXT_RELEASE; |
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
2 changes: 1 addition & 1 deletion
2
...oc/remove-next-release-conditions.test.ts → ...replace-show-next-release/markdoc.test.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
2 changes: 1 addition & 1 deletion
2
...markdoc/remove-next-release-conditions.ts → ...ipts/replace-show-next-release/markdoc.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
112 changes: 112 additions & 0 deletions
112
docs/scripts/replace-show-next-release/typescript.test.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,112 @@ | ||
import { replaceShowNextRelease } from './typescript'; | ||
test('no usage', async () => { | ||
expect(await replaceShowNextRelease('a.ts', "const a = 'something';")).toMatchInlineSnapshot(` | ||
"const a = 'something'; | ||
" | ||
`); | ||
}); | ||
test('assignment to variable', async () => { | ||
expect(await replaceShowNextRelease('a.ts', 'const a = process.env.SHOW_NEXT_RELEASE;')) | ||
.toMatchInlineSnapshot(` | ||
"const a = '1'; | ||
" | ||
`); | ||
}); | ||
test('basic if statement', async () => { | ||
expect( | ||
await replaceShowNextRelease( | ||
'a.ts', | ||
` | ||
function a() { | ||
if (process.env.SHOW_NEXT_RELEASE) { | ||
console.log('yes') | ||
} else { | ||
console.log('other') | ||
} | ||
} | ||
` | ||
) | ||
).toMatchInlineSnapshot(` | ||
"function a() { | ||
console.log('yes'); | ||
} | ||
" | ||
`); | ||
}); | ||
test('conditional expression', async () => { | ||
expect( | ||
await replaceShowNextRelease( | ||
'a.ts', | ||
` | ||
function a() { | ||
const a = process.env.SHOW_NEXT_RELEASE ? 'new release' : 'old'; | ||
} | ||
` | ||
) | ||
).toMatchInlineSnapshot(` | ||
"function a() { | ||
const a = 'new release'; | ||
} | ||
" | ||
`); | ||
}); | ||
test('conditional expression in jsx', async () => { | ||
expect( | ||
await replaceShowNextRelease( | ||
'a.tsx', | ||
` | ||
function MyThing() { | ||
return <div>something {process.env.SHOW_NEXT_RELEASE ? 'next release' : 'previous release'} other</div> | ||
} | ||
` | ||
) | ||
).toMatchInlineSnapshot(` | ||
"function MyThing() { | ||
return <div>something {'next release'} other</div>; | ||
} | ||
" | ||
`); | ||
}); | ||
test('conditional expression in jsx with jsx in consequent', async () => { | ||
expect( | ||
await replaceShowNextRelease( | ||
'a.tsx', | ||
` | ||
function MyThing() { | ||
return <div>something {process.env.SHOW_NEXT_RELEASE ? <div>new release</div>: 'previous release'} other</div> | ||
} | ||
` | ||
) | ||
).toMatchInlineSnapshot(` | ||
"function MyThing() { | ||
return ( | ||
<div> | ||
something <div>new release</div> other | ||
</div> | ||
); | ||
} | ||
" | ||
`); | ||
}); | ||
|
||
test('&&', async () => { | ||
expect( | ||
await replaceShowNextRelease( | ||
'a.tsx', | ||
` | ||
function MyThing() { | ||
return <div>something {process.env.SHOW_NEXT_RELEASE && <div>new release</div>} other</div> | ||
} | ||
` | ||
) | ||
).toMatchInlineSnapshot(` | ||
"function MyThing() { | ||
return ( | ||
<div> | ||
something <div>new release</div> other | ||
</div> | ||
); | ||
} | ||
" | ||
`); | ||
}); |
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,75 @@ | ||
// @codemod/core is basically @babel/core but it uses recast to preserve formatting/comments better | ||
import { transform } from '@codemod/core'; | ||
import { NodePath, Node, types } from '@babel/core'; | ||
import prettier from 'prettier'; | ||
|
||
function truthyConditionalExpression(path: NodePath<Node>) { | ||
if (path.parentPath?.isConditionalExpression() && path.parentKey === 'test') { | ||
return { consequent: path.parentPath.node.consequent, path: path.parentPath }; | ||
} | ||
if ( | ||
path.parentPath?.isLogicalExpression() && | ||
path.parentKey === 'left' && | ||
path.parentPath.node.operator === '&&' | ||
) { | ||
return { consequent: path.parentPath.node.right, path: path.parentPath }; | ||
} | ||
} | ||
|
||
export async function replaceShowNextRelease(filename: string, source: string) { | ||
const transformed = transform(source, { | ||
parserOpts: { plugins: filename.endsWith('.tsx') ? ['typescript', 'jsx'] : ['typescript'] }, | ||
plugins: [ | ||
{ | ||
visitor: { | ||
MemberExpression(path) { | ||
const { node } = path; | ||
if ( | ||
!( | ||
!node.computed && | ||
node.property.type === 'Identifier' && | ||
node.property.name === 'SHOW_NEXT_RELEASE' && | ||
node.object.type === 'MemberExpression' && | ||
!node.object.computed && | ||
node.object.property.type === 'Identifier' && | ||
node.object.property.name === 'env' && | ||
node.object.object.type === 'Identifier' && | ||
node.object.object.name === 'process' | ||
) | ||
) { | ||
return; | ||
} | ||
if ( | ||
path.parentPath.isIfStatement() && | ||
path.parentKey === 'test' && | ||
path.parentPath.node.consequent.type === 'BlockStatement' | ||
) { | ||
path.parentPath.replaceWithMultiple(path.parentPath.node.consequent.body); | ||
return; | ||
} | ||
|
||
const conditional = truthyConditionalExpression(path); | ||
if (conditional) { | ||
if ( | ||
conditional.path.parentPath?.isJSXExpressionContainer() && | ||
conditional.consequent.type === 'JSXElement' | ||
) { | ||
conditional.path.parentPath.replaceWith(conditional.consequent); | ||
return; | ||
} | ||
conditional.path.replaceWith(conditional.consequent); | ||
return; | ||
} | ||
path.replaceWith(types.stringLiteral('1')); | ||
}, | ||
}, | ||
}, | ||
], | ||
babelrc: false, | ||
configFile: false, | ||
babelrcRoots: false, | ||
filename, | ||
}); | ||
const config = await prettier.resolveConfig(filename); | ||
return prettier.format(transformed!.code!, { filepath: filename, ...config }); | ||
} |
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.