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

Add column range to babel-code-frame #5646

Merged
merged 1 commit into from
May 31, 2017
Merged

Conversation

SimenB
Copy link
Contributor

@SimenB SimenB commented Apr 18, 2017

Q A
Patch: Bug Fix? no
Major: Breaking Change? no
Minor: New Feature? yes
Deprecations? no
Spec Compliancy? no
Tests Added/Pass? yes
Fixed Tickets #5064
License MIT
Doc PR No
Dependency Changes Yes

Inspired by flow errors, this allows us to highlight a whole word or phrase, not just a single character.

I can make this out to the 7.0 branch instead, and not make the argument optional? As in, same as
colNumber, but handle null and don't try to support both 4 and 5 args.

If accepted, I can update babel's own usage of babel-code-frame to use this new option. I didn't do it yet in case you don't want this 😄

@mention-bot
Copy link

@SimenB, thanks for your PR! By analyzing the history of the files in this pull request, we identified @lydell, @hzoo and @DrewML to be potential reviewers.

@@ -1,6 +1,7 @@
import jsTokens, { matchToToken } from "js-tokens";
import esutils from "esutils";
import Chalk from "chalk";
import repeat from "lodash/repeat";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This seemed to be the way lodash was used

opts: Object = {},
): string {
colNumber = Math.max(colNumber, 0);

if (typeof colRange === "object") {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the part that can be dropped if a breaking change is acceptable

@codecov
Copy link

codecov bot commented Apr 18, 2017

Codecov Report

Merging #5646 into 7.0 will decrease coverage by 0.02%.
The diff coverage is 88.67%.

Impacted file tree graph

@@            Coverage Diff            @@
##              7.0   #5646      +/-   ##
=========================================
- Coverage   84.63%   84.6%   -0.03%     
=========================================
  Files         282     282              
  Lines        9857    9899      +42     
  Branches     2766    2777      +11     
=========================================
+ Hits         8342    8375      +33     
- Misses        995    1002       +7     
- Partials      520     522       +2
Impacted Files Coverage Δ
packages/babel-code-frame/src/index.js 94.17% <88.67%> (-0.91%) ⬇️
packages/babel-helper-call-delegate/src/index.js 64% <0%> (-4%) ⬇️
packages/babel-traverse/src/path/context.js 84.48% <0%> (-1.73%) ⬇️
packages/babel-traverse/src/path/modification.js 73.78% <0%> (-0.98%) ⬇️
...bel-plugin-transform-es2015-classes/src/vanilla.js 90.12% <0%> (-0.86%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6646707...bc8ef0d. Read the comment docs.

@existentialism
Copy link
Member

@SimenB thanks for the PR!

There was some discussion about specifying a range in code-frame and breaking vs not in #5064 (also a WIP PR: #5515)

@SimenB
Copy link
Contributor Author

SimenB commented Apr 18, 2017

I failed searching apparently...
If we don't want to break backwards this PR can be considered ready for review? Unless we want to accept an object as column: {start: number, end: number} in addition to a single number instead of a new argument (which would be cleaner)?

If the approach in #5515 is preferred this can be closed, but that PR seems stalled. Maybe not though 😄

It might have stalled because of lack of feedback for all I know.

@mohsen1
Copy link

mohsen1 commented Apr 20, 2017

Hi @SimenB,

I can't find time to learn Flow and finish that PR. I'll close it in favor of this. I'm in favor of the new API I proposed in #5064.

This need to cover

  • single position
  • single line with no column
  • single line range (from and to positions)
  • multi line range from and to specific position
  • multi line range with no column data

@SimenB
Copy link
Contributor Author

SimenB commented Apr 20, 2017

@mohsen1 Sure, I can do that 😄
@existentialism Target master or 7.0?

@existentialism
Copy link
Member

Let's keep it targeting 7.0

@xtuc xtuc added the PR: New Feature 🚀 A type of pull request used for our changelog categories label Apr 21, 2017
@SimenB SimenB changed the base branch from master to 7.0 April 22, 2017 13:23
@SimenB
Copy link
Contributor Author

SimenB commented Apr 22, 2017

Rebased on 7.0, and now taking in { start: { column, line }, end: { column, line } }. Any comments so far? (ignore readme)

@mohsen1 How did you imagine multiple lines would look?

@@ -30,19 +30,6 @@ describe("babel-code-frame", function () {
].join("\n"));
});

it("optional column number", function () {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this test is duplicated right above it (which meant mocha didn't run it)

@SimenB SimenB force-pushed the code-frame-range branch 2 times, most recently from 9c54350 to 9a30d3a Compare April 22, 2017 17:47
@SimenB
Copy link
Contributor Author

SimenB commented Apr 22, 2017

I added some stuff for multiple lines. Not sure how useful it is, but it's there at least 😄

@existentialism, ready for review. Not really happy about all the +1s and -1s, but I'm not sure how to avoid them... The tests pass, so that's something 😄

EDIT:got away from most of them, so OK now

@SimenB SimenB force-pushed the code-frame-range branch 3 times, most recently from 213bc49 to 9e1635e Compare April 22, 2017 17:57
@mohsen1
Copy link

mohsen1 commented Apr 22, 2017

Based on how TSLint test runner marks ranges this is what I would expect:

single position

  1 | function foo() {
> 2 |   return 1;
    |          ^
  3 | }

single line with no column

  1 | function foo() {
> 2 |   return 1;
  3 | }

single line range (from and to positions)

  1 | function foo() {
> 2 |   return 1;
    |   ~~~~~~~~       
  3 | }

multi line range from and to specific position

> 1 | function foo() {
    |                ~
> 2 |   return 1;
    | ~~~~~~~~~~~
> 3 | }
    | ~

multi line range with no column data

> 1 | function foo() {
> 2 |   return 1;
> 3 | }

@SimenB
Copy link
Contributor Author

SimenB commented Apr 22, 2017

Thanks! Then I think the current implementation was correct, except for multi lines without column data. Fixed that now though. Can you take a look at the tests and see if you agree everything is covered?

EDIT: Should probably check out tslint's implementation, figure out why they wrote their own instead of reusing babel-code-frame.

EDIT2: They already use this module: https://github.com/palantir/tslint/blob/master/src/formatters/codeFrameFormatter.ts
What reporter do you use for tslint?

@hzoo
Copy link
Member

hzoo commented May 1, 2017

Sorry for the wait!

Can we add a section to the readme about how to upgrade to the new format?

@SimenB
Copy link
Contributor Author

SimenB commented May 2, 2017

@hzoo No probs, glad it wasn't forgotten 😄

Readme updated (and I rebased)

if (!deprecationWarningShown) {
deprecationWarningShown = true;

console.warn(new 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.

Not sure how I feel about this

@SimenB
Copy link
Contributor Author

SimenB commented May 14, 2017

@hzoo Sorry for the noise, but is there anything that needs changing? Would hate for you to circle back and want changes whenever you're ready to merge this, much rather it was good to go.

colNumber: ?number,
opts: Object = {},
): string {
if (!deprecationWarningShown) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do we want to print a deprecation, or just let it be?

Copy link
Member

@hzoo hzoo left a comment

Choose a reason for hiding this comment

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

lgtm, sorry been a long time for this PR 😅

@hzoo
Copy link
Member

hzoo commented May 19, 2017

I'm ok without the deprecation too, or we can make a simple codemod for this too

@existentialism
Copy link
Member

We can turn on flow for these files later :)

@hzoo hzoo merged commit 63b7137 into babel:7.0 May 31, 2017
@existentialism
Copy link
Member

@SimenB thanks again!

@SimenB SimenB deleted the code-frame-range branch May 31, 2017 19:26
@hzoo
Copy link
Member

hzoo commented Jun 1, 2017

Added in v7.0.0-alpha.12 (Example PR to update #5808) - dono if we want the deprecation message or not still if it's going to cause other tools to error but maybe it's ok

@SimenB
Copy link
Contributor Author

SimenB commented Jun 1, 2017

I'm all for just removing the old API and throwing :)

@SimenB
Copy link
Contributor Author

SimenB commented Jun 2, 2017

@hzoo This is a breaking change for require anyways, as the default export is now only on default. Should we just replace the replace the original export?

@SimenB
Copy link
Contributor Author

SimenB commented Jun 2, 2017

Can fix off by one at the same time?

@hzoo
Copy link
Member

hzoo commented Jun 7, 2017

also from @rwjblue on slack

would be great to be able to get the offenders info into deprecations like that
would be great to use callsites and report the calling filename, line, col info

@SimenB
Copy link
Contributor Author

SimenB commented Jun 8, 2017

Not sure I understand that one. Just do --trace-deprecation (possibly --trace-warnings) when running to get at least a stacktrace.

Or is a very pretty stakctrace wanted? If so, why? And if you really want it, could you provide a sample of the wanted output?

@hzoo
Copy link
Member

hzoo commented Jun 8, 2017

We just need to know the file that the import/use of babel-code-frame is at - we could even use babel-code-frame to do it 😛 .

Just saying the current output is not helpful because it only tells you to change it but not where.

Just check our travis tests: https://travis-ci.org/babel/babel/jobs/240419278

screen shot 2017-06-08 at 10 16 05 am

@SimenB
Copy link
Contributor Author

SimenB commented Jun 8, 2017

Yeah, but if you start node with --trace-warnings then you will get the full stack trace

@rwjblue
Copy link
Member

rwjblue commented Jun 8, 2017

I think in general we should strive to make the default console output have enough information so that folks can track this down. Having the filename, column, and line info would do that.

I doubt that most users know about process.emitWarning or --trace-warnings...

@hzoo
Copy link
Member

hzoo commented Jun 8, 2017

I definetely didn't know about it 😛

@SimenB
Copy link
Contributor Author

SimenB commented Jun 8, 2017

It's the official way to deprecate stuff in node. Maybe we could print a hint about it? If you use node 4 it's a normal stacktrace, though.

@hzoo
Copy link
Member

hzoo commented Jun 8, 2017

Sure just thinking about the end-user - they are going to see this in their output and wonder what to do. It might not even be their own code so we should update the main consumers to be updated beforehand (prettier which you did, eslint, css-loader, create-react-app, etc)

@SimenB
Copy link
Contributor Author

SimenB commented Jun 8, 2017

I can go through an send a PR to them. Should we try to fix the off by one first, though?

And should we just remove the bridge and throw? That way we don't have to deal with the trace at all

@hzoo
Copy link
Member

hzoo commented Jun 8, 2017

Since this is 7.x I guess we could just throw if it makes it easier, shouldn't be hard to do together? Clearly it didn't work since we didn't fix all the places in babel itself 😛 so unlikely the case for others too

@SimenB
Copy link
Contributor Author

SimenB commented Jun 8, 2017

We already create an Error, easier to just throw it than it is to check for process.emitWarning etc 🙂

Clearly it didn't work since we didn't fix all the places in babel itself

True story!

@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 6, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 6, 2019
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 PR: New Feature 🚀 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants