Skip to content

JS: add initial support for import.meta expressions in TypeScript#2169

Merged
asger-semmle merged 3 commits intogithub:masterfrom
erik-krogh:importMeta
Oct 24, 2019
Merged

JS: add initial support for import.meta expressions in TypeScript#2169
asger-semmle merged 3 commits intogithub:masterfrom
erik-krogh:importMeta

Conversation

@erik-krogh
Copy link
Contributor

Adding initial support for import.meta in TypeScript.

This is only for TypeScript, and the recently reported FP is therefore not fixed.

Adding support for import.meta in JavaScript should just be a case of emitting a new MetaProperty(..) instead of unexpected token.

@erik-krogh erik-krogh added the JS label Oct 22, 2019
@erik-krogh erik-krogh requested a review from a team as a code owner October 22, 2019 14:03
@erik-krogh erik-krogh changed the title JS: add initial support for expressions in TypeScript JS: add initial support for import.meta expressions in TypeScript Oct 22, 2019
}

function handleParseCommand(command: ParseCommand) {
function handleParseCommand(command: ParseCommand, checkPending = true) {
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 change is not needed to support import.meta.
But it was required for me to run the "standalone" commands, so I kept the change.

@erik-krogh
Copy link
Contributor Author

It might need a db upgrade script. I forgot about that.

Copy link

@xiemaisi xiemaisi left a comment

Choose a reason for hiding this comment

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

Looks plausible, though I can't comment on the TypeScript extractor change. I'd also suggest making import.meta expressions source nodes by adding astNode instanceof ImportMetaExpr to the characteristic predicate of SourceNode::DefaultRange in Sources.qll.

As you say, this will need a dbscheme upgrade script.

/**
* An `import.meta` expression.
*/
class ImportMeta extends @importmetaexpr, Expr {

Choose a reason for hiding this comment

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

I think this class would feel more at home in Expr.qll, and should probably be called ImportMetaExpr by analogy with other similar classes. Also, it looks like import.meta expressions are pure, so it may be worth adding an override of isImpure().

Copy link
Contributor

@asger-semmle asger-semmle left a comment

Choose a reason for hiding this comment

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

We'll also need to open an internal PR to bump the dbscheme hash.

new Position(metaStart.getLine(), metaStart.getColumn() + 3, metaStart.getOffset() + 3);
SourceLocation metaLoc = new SourceLocation("new", metaStart, metaEnd);
Identifier meta = new Identifier(metaLoc, "new");
String keyWordKind = syntaxKinds.get(node.getAsJsonPrimitive("keywordToken").getAsInt() + "").toString();
Copy link
Contributor

Choose a reason for hiding this comment

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

Use getAsString instead of toString.

Also keyWordKind -> keywordKind

SourceLocation metaLoc = new SourceLocation("new", metaStart, metaEnd);
Identifier meta = new Identifier(metaLoc, "new");
String keyWordKind = syntaxKinds.get(node.getAsJsonPrimitive("keywordToken").getAsInt() + "").toString();
String identifier = keyWordKind.equals("\"ImportKeyword\"") ? "import" : "new";
Copy link
Contributor

Choose a reason for hiding this comment

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

The quotes \" are unnecessary with getAsString

@@ -1582,8 +1582,10 @@ private Node convertMetaProperty(JsonObject node, SourceLocation loc) throws Par
Position metaStart = loc.getStart();
Position metaEnd =
new Position(metaStart.getLine(), metaStart.getColumn() + 3, metaStart.getOffset() + 3);
Copy link
Contributor

Choose a reason for hiding this comment

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

The magic constant 3 here seems to have been outdated, as it should presumably be 4 for import.meta.

}

/**
* An `import.meta` expression.
Copy link
Contributor

Choose a reason for hiding this comment

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

Our AST classes have concrete syntax examples:

Suggested change
* An `import.meta` expression.
* An `import.meta` expression.
*
* Example:
* ```js
* let url = import.meta.url;
* ```

- [rate-limiter-flexible](https://www.npmjs.com/package/rate-limiter-flexible)

* The call graph has been improved to resolve method calls in more cases. This may produce more security alerts.
* import.meta is now supported in TypeScript.
Copy link
Contributor

Choose a reason for hiding this comment

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

With this change I believe we have complete support for 3.6 so I'd rather just mention that we support TypeScript 3.6 now.

Bonus nitpick: add linebreak before the bullet point

@asger-semmle
Copy link
Contributor

Some tests are failing (in the internal PR) due to old.dbscheme not corresponding to the old dbscheme. The only diff seems be a newline at the end. Could you make sure it's an exact copy of the previous dbscheme?

erik-krogh and others added 3 commits October 24, 2019 10:17
@erik-krogh
Copy link
Contributor Author

Internal PR just turned green.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants