Skip to content

Commit

Permalink
add linting slide and lateral composition
Browse files Browse the repository at this point in the history
  • Loading branch information
iammerrick committed Mar 8, 2017
1 parent 6e7b8db commit 1b57c0f
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 22 deletions.
3 changes: 2 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"presets": ["es2015", "react"]
"presets": ["es2015", "react"],
"plugins": ["transform-class-properties"]
}
62 changes: 41 additions & 21 deletions modules/Match.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,50 @@
import React from 'react';
import traverse from 'babel-traverse';

const isMatching = ({match, ast}, context) => {
const matches = [];

const walk = {
enter(path) {
const isMatch = match(path);

if (isMatch) {
matches.push(
path
);
}
},
};

if (ast) {
ast.traverse(walk);
} else {
traverse(context.ast, walk);
}

return matches;
};

class Match extends React.Component {
render() {
const matches = [];
const matchTest = this.props.match;

const walk = {
enter(path) {
const isMatch = matchTest(path);

if (isMatch) {
matches.push(
path
);
}
},
};

if (this.props.ast) {
this.props.ast.traverse(walk);
} else {
traverse(this.context.ast, walk);

state = {
matches: isMatching(this.props, this.context),
}

componentDidUpdate(_, prev) {
if (prev.matches !== this.state.matches) {
this.props.onMatch && this.props.onMatch(this.state.matches);
}
}

return React.Children.only(this.props.children(matches));
componentDidMount() {
this.props.onMatch && this.props.onMatch(this.state.matches);
}

render() {
return this.props.children
? React.Children.only(this.props.children(this.state.matches))
: null;
}
}

Expand Down
48 changes: 48 additions & 0 deletions modules/__tests__/MatchTest.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import ASTProvider from '../ASTProvider';
import React from 'react';
import Match from '../Match';
import Import from '../matchers/Import';
import Variable from '../matchers/Variable';
import * as t from 'babel-types';

import renderer from 'react-test-renderer';
Expand Down Expand Up @@ -58,6 +60,52 @@ describe('Match', () => {
expect(output.toJSON()).toMatchSnapshot();
});

it('should support callbacks for lateral composition', () => {
class JQueryAndName extends React.Component {
state = {
jquery: false,
name: false,
};

handleImportMatch = (matches) => {
if (matches.length >= 1) {
this.setState({
jquery: true,
});
}
};

handleVariableMatch = (matches) => {
if (matches.length >= 1) {
this.setState({
name: true,
});
}
};

render() {
return (
<div>
<Import from='jquery' onMatch={this.handleImportMatch} />
<Variable name='name' onMatch={this.handleVariableMatch} />
{ this.state.jquery && this.state.name
? 'Yes'
: 'Nope' }
</div>
);
}
}

const output = renderer.create(<ASTProvider source={`
import $ from 'jquery';
const name = $('Merrick');
`}>
<JQueryAndName />
</ASTProvider>);

expect(output.toJSON()).toMatchSnapshot();
});

it('should allow match nesting', () => {
const output = renderer.create(<ASTProvider source={`
const name = 'Merrick';
Expand Down
6 changes: 6 additions & 0 deletions modules/__tests__/__snapshots__/MatchTest.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@ exports[`Match should match a variable 1`] = `
VariableDeclarator
</div>
`;

exports[`Match should support callbacks for lateral composition 1`] = `
<div>
Yes
</div>
`;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"babel-cli": "^6.18.0",
"babel-loader": "^6.3.2",
"babel-plugin-external-helpers": "^6.22.0",
"babel-plugin-transform-class-properties": "^6.23.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.18.0",
"babel-preset-react": "^6.16.0",
Expand Down
Binary file modified resources/Slides.key
Binary file not shown.

0 comments on commit 1b57c0f

Please sign in to comment.