Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve regex for finding existing routes #1371

Merged
merged 1 commit into from
Jul 16, 2014

Conversation

kimroen
Copy link
Contributor

@kimroen kimroen commented Jul 13, 2014

Some improvements to the check for existing routes:

  • More liberal with spaces around parenthesis
  • " now works in addition to '. (This looks like it was an oversight)
  • Don't capture the first group

@stefanpenner
Copy link
Contributor

Can you add a failing test?

@@ -20,7 +20,7 @@ function addRouteToRouter(name, options) {
var type = options.type || 'route';
var routerPath = path.join(process.cwd(), 'app', 'router.js');
var oldContent = fs.readFileSync(routerPath, 'utf-8');
var existence = new RegExp("(route|resource)\\(['\"]" + name + "'");
var existence = new RegExp("(?:route|resource)\\s?\\(\\s?(['\"])" + name + "\\1");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, this is complex enough to warrant an explanation:

(?:route|resource)   // non-capturing match for literal "route" or "resource"
\s?                  // zero or one white space
\(                   // opening literal parantheses (
\s?                  // zero or one white space
(['\"])              // matching/capturing for single or double quote
name-variable
\1                   // matching the previously captured single or double quote

One tiny suggestion: wouldn't \s* make more sense than \s? ? In both cases, it would be syntactically correct to write route . . . . ( . . . "post" ) (dots intentional for more spacing)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree on the change.

Are you suggesting I add this explanation inline, or just for clarification here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On github, it is very easy to miss. Especially if the code is changed later-on. It surely doesn't need to be as big a block of text as my attempt (i was merely writing along as i was reading the regex). Sadly, JavaScript does not have the x mode that ruby has. This would be super cool.

@stefanpenner
Copy link
Contributor

I'm fine for this right now but going forward we should likely just use esprima for this, although escodgen doesn't support es6 syntax yet, we can simply replace the subgraph of code specific to the routes and splice it back in.

@kimroen
Copy link
Contributor Author

kimroen commented Jul 14, 2014

Added some tests.

@stefanpenner Yeah, I can see that being a good option.

rwjblue added a commit that referenced this pull request Jul 16, 2014
Improve regex for finding existing routes
@rwjblue rwjblue merged commit 88a2591 into ember-cli:master Jul 16, 2014
@kimroen kimroen deleted the improve-route-check branch July 16, 2014 11:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants