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

update substitution placeholder message in babel-template #7255

Conversation

thescientist13
Copy link
Contributor

@thescientist13 thescientist13 commented Jan 22, 2018

Q                       A
Fixed Issues? helps resolve #7035
Patch: Bug Fix? Yes
Major: Breaking Change? No
Minor: New Feature? No
Tests Added + Pass? Yes
Documentation PR No
Any Dependency Changes? No
License MIT
  1. Updated babel-template placeholder text for missing substitution per suggestion here

@thescientist13 thescientist13 changed the title linting passing update substitution placeholder message in babel-template Jan 22, 2018
throw new Error(`No substitution given for "${placeholder.name}"`);
throw new Error(`Error: No substitution given for "NODE_ENV". If this is not meant to be a
placeholder you may want to consider passing one of the following options to babel-template:
- { placeholderPattern: false, placeholderWhitelist: new Set(['NODE']) }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure if
new Set(['NODE']

should be this ?
new Set(['NODE_ENV']

Copy link
Member

Choose a reason for hiding this comment

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

The error message should continue using placeholder.name, don't think we want to say NODE_ENV for every error :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@existentialism
Ah... of course!

@thescientist13
Copy link
Contributor Author

thescientist13 commented Jan 22, 2018

Working on fixing the test, just getting a little hung up on formatting / linting for a multiline string in the test, example (I'm using Webstorm for IDE if it matters)

$ TEST_ONLY=babel-template make test
./node_modules/.bin/eslint scripts packages codemods *.js --format=codeframe --rulesdir="./scripts/eslint_rules"
error: Delete `⏎········` (prettier/prettier) at packages/babel-template/src/populate.js:21:46:
  19 |           placeholder you may want to consider passing one of the following options to babel-template:
  20 |           - { placeholderPattern: false, placeholderWhitelist: new Set(['NODE']) }
> 21 |           - { placeholderPattern: /^NODE$/ }`
     |                                              ^
  22 |         );
  23 |       }
  24 |     });

note: the test didn't fail locally for me running TEST_ONLY=babel-template make test, not sure if that is expected or not. (maybe not since it is dealing with NODE_ENV?)

@maazadeeb
Copy link
Contributor

Try running make fix to fix all the problems that can be fixed automatically. All errors reported by prettier are auto-fixable.

@abiduzz420
Copy link

@thescientist13 You need to modify the expected error message here https://github.com/babel/babel/blob/master/packages/babel-template/test/index.js#L130
Hope this helps.

@thescientist13
Copy link
Contributor Author

thescientist13 commented Jan 23, 2018

thank you for that tip @maaz93 !! The console output showed --fix so I was sad that it wasn't working for me

3 errors potentially fixable with the --fix option.

Now it's working! 🎉

@thescientist13
Copy link
Contributor Author

thescientist13 commented Jan 23, 2018

thanks @abiduzz420, that's what I'm working on now 👍

@thescientist13
Copy link
Contributor Author

think I got it now 🤞

@babel-bot
Copy link
Collaborator

babel-bot commented Jan 23, 2018

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/6710/

throw new Error(`Error: No substitution given for "${
placeholder.name
}". If this is not meant to be a
placeholder you may want to consider passing one of the following options to babel-template:
Copy link
Contributor Author

@thescientist13 thescientist13 Jan 23, 2018

Choose a reason for hiding this comment

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

this formatting was put in by prettier. maybe it expects { to always be the start of an object and thus need a line break after?

let me know if this 🆗

Copy link
Contributor

Choose a reason for hiding this comment

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

Template literals, the ``, preserve spaces and new lines. You'll need to construct your string something like the way it's done here

`You appear to be using an async codegen plugin, ` +

Otherwise, the error message will have unnecessary indentation.

Copy link
Contributor Author

@thescientist13 thescientist13 Jan 23, 2018

Choose a reason for hiding this comment

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

@maaz93
It seems to keep complaining about that {

      if (
        !Object.prototype.hasOwnProperty.call(replacements, placeholder.name)
      ) {
        throw new Error(
            `Error: No substitution given for "${placeholder.name}". If this is not meant ` +
              `to be a placeholder you may want to consider passing one of the following options to babel-template:` +
              `- { placeholderPattern: false, placeholderWhitelist: new Set(['${placeholder.name}'])}` +
            `- { placeholderPattern: /^${placeholder.name}$/ }`
        );
      }
error: Replace `··`Error:·No·substitution·given·for·"${placeholder.name` with ``Error:·No·substitution·given·for·"${⏎············placeholder.name⏎··········` (prettier/prettier) at packages/babel-template/src/populate.js:19:11:
  17 |       ) {
  18 |         throw new Error(
> 19 |             `Error: No substitution given for "${placeholder.name}". If this is not meant ` +
     |           ^
  20 |               `to be a placeholder you may want to consider passing one of the following options to babel-template:` +
  21 |               `- { placeholderPattern: false, placeholderWhitelist: new Set(['${placeholder.name}'])}` +
  22 |               `- { placeholderPattern: /^${placeholder.name}$/ }`

Copy link
Contributor

Choose a reason for hiding this comment

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

Alright. This seems a little tricky. I formatted it here to work fine:

        const placeholderName = placeholder.name;
        throw new Error(
          `Error: No substitution given for "${placeholderName}". If this is not meant to be a placeholder ` +
            `you may want to consider passing one of the following options to babel-template:` +
            `- { placeholderPattern: false, placeholderWhitelist: new Set(['${placeholderName}'])}` +
            `- { placeholderPattern: /^${placeholderName}$/ }`,
        );

It was complaining about printWidth and the { placement. I've overcome the { by not having a property access, rather storing it in placeholderName. Also, I changed number of characters per line in one of the lines to meet the allowed printWidth. Let me know if this works

@maazadeeb
Copy link
Contributor

It's generally recommended to run make test once locally, after you make a local commit. This will give you an opportunity to see if any tests are failing or not on your system, instead of waiting for the CI. Were you able to figure out why tests in your last commit failed?

@thescientist13
Copy link
Contributor Author

thescientist13 commented Jan 23, 2018

@maaz93
It looks like it's because of the formatting still (but because the output differs because of how the test / populatePlaceholders are constructing the string.

I have tried again with using `` for both still getting test failures on the expected vs actual
populate.js

        const placeholderName = placeholder.name;

        throw new Error(
          `Error: No substitution given for "${placeholderName}". If this is not meant to be a 
            placeholder you may want to consider passing one of the following options to babel-template:
            - { placeholderPattern: false, placeholderWhitelist: new Set(['${placeholderName}'])}
            - { placeholderPattern: /^${placeholderName}$/ }`,
        );

index.js

      }).to.throw(
        Error,
        `Error: No substitution given for "ANOTHER_ID". If this is not meant to be a
          placeholder you may want to consider passing one of the following options to babel-template:
          - { placeholderPattern: false, placeholderWhitelist: new Set(['ANOTHER_ID']) }
          - { placeholderPattern: /^ANOTHER_ID$/ }`,
      );

Locally with both implementations seemingly the same, I am seeing this test failure occuring

 1) babel-template string-based should throw if placeholders are not given explicit values:

      AssertionError: expected [Function] to throw error including 'Error: No substitution given for "ANOTHER_ID". If this is not meant to be a\n          placeholder you may want to consider passing one of the following options to babel-template:\n          - { placeholderPattern: false, placeholderWhitelist: new Set([\'ANOTHER_ID\']) }\n          - { placeholderPattern: /^ANOTHER_ID$/ }' but got 'Error: No substitution given for "ANOTHER_ID". If this is not meant to be a placeholderyou may want to consider passing one of the following options to babel-template:- { placeholderPattern: false, placeholderWhitelist: new Set([\'ANOTHER_ID\'])}- { placeholderPattern: /^ANOTHER_ID$/ }'
      + expected - actual

      -Error: No substitution given for "ANOTHER_ID". If this is not meant to be a placeholderyou may want to consider passing one of the following options to babel-template:- { placeholderPattern: false, placeholderWhitelist: new Set(['ANOTHER_ID'])}- { placeholderPattern: /^ANOTHER_ID$/ }
      +Error: No substitution given for "ANOTHER_ID". If this is not meant to be a
      +          placeholder you may want to consider passing one of the following options to babel-template:
      +          - { placeholderPattern: false, placeholderWhitelist: new Set(['ANOTHER_ID']) }
      +          - { placeholderPattern: /^ANOTHER_ID$/ }

}).to.throw(Error, 'No substitution given for "ANOTHER_ID"');
}).to.throw(
Error,
`Error: No substitution given for "ANOTHER_ID". If this is not meant to be a
Copy link
Contributor

Choose a reason for hiding this comment

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

ES6 template literals take raw string, so if you have multi-line string, then it will automatically add new Line character there. However, when you use + (with strings), you are actually doing concatenation and not putting any new Line character there. I would suggest you to convert both of them into template literals, that would automatically add new line character in both. Hence resolving test!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks @nveenjain
I forgot to include it in my comment, but locally both implementations are seemingly matching to me in terms of implementation, yet still getting a test failure on string construction. (I've since updated my comment to show everything)

I can push my changes if it helps.

Copy link
Member

Choose a reason for hiding this comment

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

this one is still constructed with multiline template string, you just have to adjust this one so it is constructed like the other one (the other one is correct)

Copy link
Contributor Author

@thescientist13 thescientist13 Jan 24, 2018

Choose a reason for hiding this comment

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

@Andarist
Do you mean in this PR? Yes I understand they are different and I know why they are incompatible, but if you see the updated link from my comment above, locally they seem identical to me. So still struggling with the difference between this

        const placeholderName = placeholder.name;

        throw new Error(
          `Error: No substitution given for "${placeholderName}". If this is not meant to be a 
            placeholder you may want to consider passing one of the following options to babel-template:
            - { placeholderPattern: false, placeholderWhitelist: new Set(['${placeholderName}'])}
            - { placeholderPattern: /^${placeholderName}$/ }`,
        );

and this

      }).to.throw(
        Error,
        `Error: No substitution given for "ANOTHER_ID". If this is not meant to be a
          placeholder you may want to consider passing one of the following options to babel-template:
          - { placeholderPattern: false, placeholderWhitelist: new Set(['ANOTHER_ID']) }
          - { placeholderPattern: /^ANOTHER_ID$/ }`,
      );

🤒

Copy link
Member

@Andarist Andarist Jan 24, 2018

Choose a reason for hiding this comment

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

notice that in the source file (when throwing) the string is created (in the PR) with concatenation (+) and in tests it's defined with multiline template literal

This:

		const str1 = `first line` +
												`second line`

and this:

		const str2 = `first line
												second line`

are not equivalents.

str1 === 'first linesecond line'
str2 === 'first line\n\t\t\t\t\t\t\t\t\t\t\t\tsecond line'

Copy link
Contributor Author

@thescientist13 thescientist13 Jan 24, 2018

Choose a reason for hiding this comment

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

@Andarist
Understood that is what is causing the failure in this PR specifically, but my comment was just trying to demonstrate what I was trying locally to fix the failure so as to avoid pushing intentionally failing commits.

I suppose that keeping my local work local is causing more confusion, so I will just push my latest changes.

Appreciate you helping out, sorry for any confusion, hopefully the issue I'm seeing will be better highlighted in a few moments.

Copy link
Member

@existentialism existentialism Jan 24, 2018

Choose a reason for hiding this comment

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

@thescientist13 the difference is an extra space after "a" in your input vs output and from travis, as well as the subsequent whitespace:

'Error: No substitution given for "ANOTHER_ID". If this is not meant to be a\n          placeholder you may want to consider passing one of the following options to babel-template:\n          - { placeholderPattern: false, placeholderWhitelist: new Set([\'ANOTHER_ID\']) }\n          - { placeholderPattern: /^ANOTHER_ID$/ }'

vs

'Error: No substitution given for "ANOTHER_ID". If this is not meant to be a \n            placeholder you may want to consider passing one of the following options to babel-template:\n            - { placeholderPattern: false, placeholderWhitelist: new Set([\'ANOTHER_ID\'])}\n            - { placeholderPattern: /^ANOTHER_ID$/ }'`

Copy link
Contributor Author

@thescientist13 thescientist13 Jan 24, 2018

Choose a reason for hiding this comment

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

Thanks @existentialism ! I think part of the issue is that what I see in CircleCI is different output than what I see locally. Will need to see if I need to align my local environment

  • OSX: 10.13.2
  • node: 6.9.1
  • npm: 5.6.0

I think my next goal is to make my local try and match CI.

Copy link
Member

@existentialism existentialism Jan 24, 2018

Choose a reason for hiding this comment

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

@thescientist13 i pushed a change to your branch that should fix it, sorry for all the trouble digging into it!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

wow, thanks @existentialism !!

I'll certainly never underestimate a string again... 😌

const placeholderName = placeholder.name;

throw new Error(
`Error: No substitution given for "${placeholderName}". If this is not meant to be a
Copy link
Member

Choose a reason for hiding this comment

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

multiline string like this will look weirdly in the console, personally I would prefer using regular concatenation for this

Copy link
Contributor Author

@thescientist13 thescientist13 Jan 24, 2018

Choose a reason for hiding this comment

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

fair point and appreciate your assistance / feedback.

String concatenation had been suggested before, but then I got too focused on the struggle with template string to step back and consider the alternatives and wanting to stick with the existing convention.

Lesson learned!

@existentialism existentialism added PR: Bug Fix 🐛 A type of pull request used for our changelog categories PR: Polish 💅 A type of pull request used for our changelog categories pkg: template and removed PR: Bug Fix 🐛 A type of pull request used for our changelog categories labels Jan 30, 2018
Copy link
Member

@existentialism existentialism left a comment

Choose a reason for hiding this comment

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

LGTM

@hzoo
Copy link
Member

hzoo commented Jan 30, 2018

Thanks for sticking with this - we should use @babel/template instead of babel-template now but I'l just fix it up in another commit

@hzoo hzoo merged commit 2185256 into babel:master Jan 30, 2018
aminmarashi pushed a commit to aminmarashi/babel that referenced this pull request Mar 17, 2018
@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label May 2, 2018
@lock lock bot locked as resolved and limited conversation to collaborators May 2, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: template PR: Polish 💅 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Regression in babel-template with uppercase variables not meant to be replaced
8 participants