Skip to content

Commit

Permalink
Merge pull request #5 from Rich-Harris/maranomynet/buble-feature/jsx-…
Browse files Browse the repository at this point in the history
…pragma-comments

Maranomynet/buble feature/jsx pragma comments
  • Loading branch information
Rich-Harris authored Sep 23, 2017
2 parents 406d4a4 + b4eab5c commit eeb40c8
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"acorn-jsx": "^3.0.1",
"acorn-object-spread": "^1.0.0",
"chalk": "^1.1.3",
"vlq": "0.2.1",
"magic-string": "^0.14.0",
"minimist": "^1.2.0",
"os-homedir": "^1.0.1",
Expand Down
8 changes: 8 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,25 @@ export function target ( target ) {

export function transform ( source, options = {} ) {
let ast;
let jsx = null;

try {
ast = parse( source, {
ecmaVersion: 7,
preserveParens: true,
sourceType: 'module',
onComment: (block, text) => {
if ( !jsx ) {
let match = /@jsx\s+([^\s]+)/.exec( text );
if ( match ) jsx = match[1];
}
},
plugins: {
jsx: true,
objectSpread: true
}
});
options.jsx = jsx || options.jsx;
} catch ( err ) {
err.snippet = getSnippet( source, err.loc );
err.toString = () => `${err.name}: ${err.message}\n${err.snippet}`;
Expand Down
1 change: 1 addition & 0 deletions test/cli/supports-jsx-pragma-comment/command.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
buble input.js -o actual/output.js --jsx NotReact.createElement
2 changes: 2 additions & 0 deletions test/cli/supports-jsx-pragma-comment/expected/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* @jsx customPragma */
var div = customPragma( 'div', null, "Hello" )
2 changes: 2 additions & 0 deletions test/cli/supports-jsx-pragma-comment/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* @jsx customPragma */
var div = <div>Hello</div>
29 changes: 27 additions & 2 deletions test/samples/jsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,32 @@ module.exports = [
`
},

{
description: 'supports /* @jsx customPragma */ directives (#195)',
input: `
/* @jsx customPragma */
var div = <div>Hello</div>
`,
output: `
/* @jsx customPragma */
var div = customPragma( 'div', null, "Hello" )
`
},

{
description: 'ignores subsequent /* @jsx customPragma */ directives (#195)',
input: `
/* @jsx customPragma */
/* @jsx customPragmaWannabe */
var div = <div>Hello</div>
`,
output: `
/* @jsx customPragma */
/* @jsx customPragmaWannabe */
var div = customPragma( 'div', null, "Hello" )
`
},

{
description: 'handles dash-cased value-less props',

Expand All @@ -233,6 +259,5 @@ module.exports = [
output: `
React.createElement( Thing, { 'data-foo': true })
`
},

}
];

0 comments on commit eeb40c8

Please sign in to comment.