Skip to content

fix: do not match MySymbol.import("mod") #23358

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

Merged
merged 8 commits into from
Apr 16, 2018

Conversation

joscha
Copy link
Contributor

@joscha joscha commented Apr 12, 2018

The quill library has a static method called import on it's exported symbol, which is used like this:

Quill.import('dependency');

These dependencies are not actually imports, they are strings in a map. Unfortunately ts.preProcessFile thinks they are valid imports.
I tried fixing the issue, but there is some logic missing and I am unsure how to debug the test I added properly with me being able to look at the scanner state. Maybe someone can give me a hint on how to complete my fix, please?

  • There is an associated issue that is labeled
    'Bug' or 'help wanted' or is in the Community milestone
  • Code is up-to-date with the master branch
  • You've successfully run jake runtests locally [But the code fails, and I am unsure why]
  • You've signed the CLA
  • There are new or updated unit tests validating the change

Fixes #23359

@msftclas
Copy link

msftclas commented Apr 12, 2018

CLA assistant check
All CLA requirements met.

@@ -1469,6 +1471,7 @@ namespace ts {
pos++;
return token = SyntaxKind.MinusToken;
case CharacterCodes.dot:
tokenFlags |= TokenFlags.PrecedingDot;
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 might need to move right before the return if we understand the flag to denote a single dot.

@@ -78,7 +78,7 @@ namespace ts {
*/
function tryConsumeImport(): boolean {
let token = scanner.getToken();
if (token === SyntaxKind.ImportKeyword) {
if (token === SyntaxKind.ImportKeyword && !scanner.hasPrecedingDot()) {
Copy link
Contributor

@mhegazy mhegazy Apr 12, 2018

Choose a reason for hiding this comment

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

do not think you need to change the scanner API to do this. this whole function is just a mini parser. it advances the scanner each time it needs to consume a token, so you can do it here, no need to change the scanner implementation. for instance, you could have a flag for lastTokenWasDot in nextToken

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 was trying to avoid putting state into the parser, as I saw we had https://github.com/Microsoft/TypeScript/blob/7631ad03a330511418f5cad421b9b876002fcca3/src/compiler/types.ts#L1578-L1579 already. If you are happy for me to store that in a local var, then I will adapt the pull request?

Copy link
Contributor

Choose a reason for hiding this comment

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

that is not the parser.. this is just a small parser-like function that tries to speculatively find import/export/require statements.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, sorry for my wording, I used parser, because you called it a mini parser. So flag in nextToken SGTY?

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah. it is already keeping state, one more flag should not harm anyone.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, will give it a spin 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changes in ee429ef, but for some reason it doesn't work as expected. I am not even sure if the code is run, as I can't figure out from the docs on how to debug the tests or use the watch mode. Plus, process.stdout.write and console.log are not available either, so I am unsure on how to debug this - any pointers @mhegazy?

Copy link
Contributor

Choose a reason for hiding this comment

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

jake runtests-browser should run the tests int he browser, you can use the browser dev tools to debug.

@@ -25,6 +26,7 @@ namespace ts {
else if (token === SyntaxKind.CloseBraceToken) {
braceNesting--;
}
lastTokenWasDot = token === SyntaxKind.DotToken;
Copy link
Contributor

Choose a reason for hiding this comment

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

should not this be before we consume the next token? this just says token === SyntaxKind.DotToken

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Of course, fixed up in 02b8160

@mhegazy
Copy link
Contributor

mhegazy commented Apr 13, 2018

For debugging, I just use jake runtests-browser and use the browser devtools to debug. you can also write a node module that imports typescript.js and calls preprocessfile on it.

@joscha
Copy link
Contributor Author

joscha commented Apr 14, 2018

For debugging, I just use jake runtests-browser and use the browser devtools to debug. you can also write a node module that imports typescript.js and calls preprocessfile on it.

That works like a charm, thank you!

@joscha
Copy link
Contributor Author

joscha commented Apr 14, 2018

Tests should go ✅ hopefully, PTAL @mhegazy

@joscha
Copy link
Contributor Author

joscha commented Apr 14, 2018

I don't really understand, the test passes locally:

mocha_tests_and_1__npm_test__node_

@joscha
Copy link
Contributor Author

joscha commented Apr 14, 2018

OK, got it, no hot-reload, need to restart after file-changes:

mocha_tests

@joscha
Copy link
Contributor Author

joscha commented Apr 14, 2018

didn't need to introduce more state after all @mhegazy - tests pass locally after a fresh build via gulp clean && gulp runtests-browser --browser=chrome --tests=non-imports, hopefully CI will back me up this time 🤞 😬

@@ -13,20 +13,20 @@ namespace ts {
const importedFiles: FileReference[] = [];
let ambientExternalModules: { ref: FileReference, depth: number }[];
let braceNesting = 0;
let lastTokenWasDot = false;
let lastToken: SyntaxKind = null;
Copy link
Contributor

Choose a reason for hiding this comment

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

do not use null. undefined (or unintialized) is what we have used throughout the code base.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, I saw, the linter yelled at me :)

break;
}
if (token === SyntaxKind.DotToken) {
Copy link
Contributor

Choose a reason for hiding this comment

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

what about a.b.import?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should still work, because the next token is a literal, which means that it will go into the else came - I will add a 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.

ah, I see what you mean now. Will fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

let braceNesting = 0;
// assume that text represent an external module if it contains at least one top level import/export
// ambient modules that are found inside external modules are interpreted as module augmentations
let externalModule = false;

function nextToken() {
const token = scanner.scan();
if (token === SyntaxKind.OpenBraceToken) {
if (currentToken) {
Copy link
Contributor

Choose a reason for hiding this comment

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

do not think you need this check.

@mhegazy mhegazy merged commit a8618a7 into microsoft:master Apr 16, 2018
@mhegazy
Copy link
Contributor

mhegazy commented Apr 16, 2018

thanks @joscha!

@joscha joscha deleted the joscha/fix-non-imports-matching branch April 16, 2018 21:47
@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Code Quill.import('mod'); makes TS think it needs to import('mod')
3 participants