forked from gajus/flow-runtime
-
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.
Merge pull request gajus#166 from jedwards1211/convert-option
feat(pragma): add optInOnly option
- Loading branch information
Showing
7 changed files
with
133 additions
and
87 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
48 changes: 48 additions & 0 deletions
48
packages/babel-plugin-flow-runtime/src/__tests__/pragmaOptInOnly.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,48 @@ | ||
/* @flow */ | ||
|
||
import testTransform from './testTransform'; | ||
|
||
describe('optInOnly', () => { | ||
it("transforms files with @flow-runtime annotation", () => { | ||
const input = ` | ||
/* @flow */ | ||
/* @flow-runtime enable */ | ||
type Demo = 123; | ||
("nope": Demo); | ||
const demo = ([foo]: string[]): string => foo; | ||
`; | ||
|
||
const expected = ` | ||
import t from "flow-runtime"; | ||
/* @flow */ | ||
/* @flow-runtime enable */ | ||
const Demo = t.type("Demo", t.number(123)); | ||
let _undefinedType = Demo; | ||
"nope"; | ||
const demo = (_arg) => { | ||
let [foo] = _arg; | ||
return foo; | ||
}; | ||
`; | ||
|
||
testTransform(input, {annotate: false, assert: false, optInOnly: true}, expected); | ||
}); | ||
it("doesn't transform files without @flow-runtime annotation", () => { | ||
const input = ` | ||
/* @flow */ | ||
const Demo = 123; | ||
`; | ||
|
||
const expected = ` | ||
/* @flow */ | ||
const Demo = 123; | ||
`; | ||
|
||
testTransform(input, {optInOnly: true}, expected); | ||
}) | ||
}); |
76 changes: 76 additions & 0 deletions
76
packages/babel-plugin-flow-runtime/src/__tests__/testTransform.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,76 @@ | ||
/* @flow */ | ||
|
||
import {equal} from 'assert'; | ||
|
||
import fixtures from './fixtures'; | ||
|
||
import transform from '../transform'; | ||
|
||
import * as babylon from 'babylon'; | ||
import generate from 'babel-generator'; | ||
import traverse from 'babel-traverse'; | ||
import type {Node, NodePath} from 'babel-traverse'; | ||
|
||
import type {Options} from '../createConversionContext'; | ||
|
||
function stripFlowTypes (program: Node): Node { | ||
traverse(program, { | ||
Flow (path: NodePath) { | ||
path.remove(); | ||
}, | ||
TypeCastExpression(path) { | ||
let { node } = path; | ||
do { | ||
node = node.expression; | ||
} while (node.type === 'TypeCastExpression'); | ||
path.replaceWith(node); | ||
}, | ||
Class(path) { | ||
path.node.implements = null; | ||
} | ||
}); | ||
return program; | ||
} | ||
|
||
function parse (source: string): Node { | ||
return babylon.parse(source, { | ||
filename: 'unknown', | ||
sourceType: 'module', | ||
plugins: [ | ||
'jsx', | ||
'flow', | ||
'doExpressions', | ||
'objectRestSpread', | ||
'decorators', | ||
'classProperties', | ||
'exportExtensions', | ||
'asyncGenerators', | ||
'functionBind', | ||
'functionSent' | ||
] | ||
}); | ||
} | ||
|
||
function normalize (input: string): string { | ||
return input | ||
.trim() | ||
.replace(/\s+/g, ' ') | ||
.replace(/\(\s+/g, '(') | ||
.replace(/\s+\)/g, ')') | ||
.replace(/\{\s+/g, '{\n') | ||
.replace(/\s+\}/g, '\n}') | ||
.replace(/\[\s+/g, '[') | ||
.replace(/\s+]/g, ']') | ||
.replace(/\}\s+([A-Za-z])/g, '\n}\n$1') | ||
.split(';') | ||
.join(';\n') | ||
.trim() | ||
; | ||
} | ||
|
||
export default function testTransform(input: string, options: Options, expected: string) { | ||
const parsed = parse(input); | ||
const transformed = stripFlowTypes(transform(parsed, options)); | ||
const generated = generate(transformed).code; | ||
equal(normalize(generated), normalize(expected)); | ||
} |
90 changes: 4 additions & 86 deletions
90
packages/babel-plugin-flow-runtime/src/__tests__/transform.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 |
---|---|---|
@@ -1,105 +1,23 @@ | ||
/* @flow */ | ||
|
||
import {equal} from 'assert'; | ||
|
||
import fixtures from './fixtures'; | ||
|
||
import transform from '../transform'; | ||
|
||
import * as babylon from 'babylon'; | ||
import generate from 'babel-generator'; | ||
import traverse from 'babel-traverse'; | ||
import type {Node, NodePath} from 'babel-traverse'; | ||
|
||
import testTransform from './testTransform'; | ||
|
||
describe('transform', () => { | ||
for (const [name, {input, expected, annotated, combined}] of fixtures) { | ||
it(`should transform ${name}`, () => { | ||
const parsed = parse(input); | ||
const transformed = stripFlowTypes(transform(parsed, { | ||
assert: true, | ||
annotate: false | ||
})); | ||
const generated = generate(transformed).code; | ||
equal(normalize(generated), normalize(expected)); | ||
testTransform(input, {assert: true, annotate: false}, expected); | ||
}); | ||
if (annotated) { | ||
it(`should transform ${name} with decorations`, () => { | ||
const parsed = parse(input); | ||
const transformed = stripFlowTypes(transform(parsed, { | ||
assert: false, | ||
annotate: true | ||
})); | ||
const generated = generate(transformed).code; | ||
equal(normalize(generated), normalize(annotated)); | ||
testTransform(input, {assert: false, annotate: true}, annotated); | ||
}); | ||
} | ||
if (combined) { | ||
it(`should transform ${name} with decorations and assertions`, () => { | ||
const parsed = parse(input); | ||
const transformed = stripFlowTypes(transform(parsed, { | ||
assert: true, | ||
annotate: true | ||
})); | ||
const generated = generate(transformed).code; | ||
equal(normalize(generated), normalize(combined)); | ||
testTransform(input, {assert: true, annotate: true}, combined); | ||
}); | ||
} | ||
} | ||
}); | ||
|
||
|
||
function stripFlowTypes (program: Node): Node { | ||
traverse(program, { | ||
Flow (path: NodePath) { | ||
path.remove(); | ||
}, | ||
TypeCastExpression(path) { | ||
let { node } = path; | ||
do { | ||
node = node.expression; | ||
} while (node.type === 'TypeCastExpression'); | ||
path.replaceWith(node); | ||
}, | ||
Class(path) { | ||
path.node.implements = null; | ||
} | ||
}); | ||
return program; | ||
} | ||
|
||
function parse (source: string): Node { | ||
return babylon.parse(source, { | ||
filename: 'unknown', | ||
sourceType: 'module', | ||
plugins: [ | ||
'jsx', | ||
'flow', | ||
'doExpressions', | ||
'objectRestSpread', | ||
'decorators', | ||
'classProperties', | ||
'exportExtensions', | ||
'asyncGenerators', | ||
'functionBind', | ||
'functionSent' | ||
] | ||
}); | ||
} | ||
|
||
function normalize (input: string): string { | ||
return input | ||
.trim() | ||
.replace(/\s+/g, ' ') | ||
.replace(/\(\s+/g, '(') | ||
.replace(/\s+\)/g, ')') | ||
.replace(/\{\s+/g, '{\n') | ||
.replace(/\s+\}/g, '\n}') | ||
.replace(/\[\s+/g, '[') | ||
.replace(/\s+]/g, ']') | ||
.replace(/\}\s+([A-Za-z])/g, '\n}\n$1') | ||
.split(';') | ||
.join(';\n') | ||
.trim() | ||
; | ||
} |
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