-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Review comments: Extract and stream-line code, add edge-case test
- Loading branch information
1 parent
bea94ff
commit 63f45cd
Showing
11 changed files
with
69 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// @ts-ignore | ||
import walk from 'acorn/dist/walk'; | ||
import * as ESTree from 'estree'; | ||
import { CommentDescription } from '../Module'; | ||
|
||
function forEachNodeAfterComment( | ||
ast: ESTree.Program, | ||
commentNodes: CommentDescription[], | ||
handleNode: (node: ESTree.Node) => void | ||
): void { | ||
let commentIdx = 0; | ||
|
||
checkCommentsBeforeNode(<any>ast); | ||
|
||
function checkCommentsBeforeNode(node: ESTree.Node & { start: number; end: number }) { | ||
if (commentIdx === commentNodes.length || commentNodes[commentIdx].end > node.end) return; | ||
let isFound = false; | ||
while (commentIdx < commentNodes.length && node.start >= commentNodes[commentIdx].end) { | ||
if (!isFound) { | ||
handleNode(node); | ||
isFound = true; | ||
} | ||
commentIdx++; | ||
} | ||
walk.base[node.type](node, null, checkCommentsBeforeNode); | ||
} | ||
} | ||
|
||
function markPureNode(node: ESTree.Node) { | ||
while (node.type === 'ExpressionStatement') { | ||
node = node.expression; | ||
} | ||
if (node.type === 'CallExpression') { | ||
(<any>node).pure = true; | ||
} | ||
} | ||
|
||
const pureCommentRegex = /^ ?#__PURE__\s*$/; | ||
|
||
export function markPureCallExpressions(comments: CommentDescription[], esTreeAst: ESTree.Program) { | ||
forEachNodeAfterComment( | ||
esTreeAst, | ||
comments.filter(comment => pureCommentRegex.test(comment.text)), | ||
markPureNode | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
description: 'does not associate multiple "pure" comments before a token with subsequent tokens' | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
retained(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/*#__PURE__*//*#__PURE__*//*#__PURE__*//*#__PURE__*//*#__PURE__*/ removed(); | ||
retained(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,20 @@ | ||
const assert = require('assert'); | ||
|
||
module.exports = { | ||
description: 'functions marked with pure comment do not have effects', | ||
description: 'external function calls marked with pure comment do not have effects', | ||
options: { | ||
external: ['socks'] | ||
}, | ||
context: { | ||
require(id) { | ||
if (id === 'socks') { | ||
return () => '🧦'; | ||
return () => { | ||
throw new Error('Not all socks were removed.'); | ||
}; | ||
} | ||
} | ||
}, | ||
code(code) { | ||
assert.ok(code.search(/socks\(\)/) === -1); | ||
}, | ||
warnings: [ | ||
{ | ||
code: 'UNRESOLVED_IMPORT', | ||
importer: 'main.js', | ||
message: | ||
"'socks' is imported by main.js, but could not be resolved – treating it as an external dependency", | ||
source: 'socks', | ||
url: | ||
'https://github.com/rollup/rollup/wiki/Troubleshooting#treating-module-as-external-dependency' | ||
} | ||
] | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters