-
-
Notifications
You must be signed in to change notification settings - Fork 877
Expand file tree
/
Copy pathpeephole_replace_known_methods.rs
More file actions
95 lines (77 loc) · 3.05 KB
/
peephole_replace_known_methods.rs
File metadata and controls
95 lines (77 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use oxc_ast::ast::*;
use oxc_traverse::{Traverse, TraverseCtx};
use crate::CompressorPass;
/// Minimize With Known Methods
/// <https://github.com/google/closure-compiler/blob/master/src/com/google/javascript/jscomp/PeepholeMinimizeReplacements.java>
pub struct PeepholeReplaceKnownMethods {
changed: bool,
}
impl<'a> CompressorPass<'a> for PeepholeReplaceKnownMethods {
fn changed(&self) -> bool {
self.changed
}
fn build(&mut self, _program: &mut Program<'a>, _ctx: &mut TraverseCtx<'a>) {
self.changed = false;
// oxc_traverse::walk_program(self, program, ctx);
}
}
impl<'a> Traverse<'a> for PeepholeReplaceKnownMethods {}
impl PeepholeReplaceKnownMethods {
pub fn new() -> Self {
Self { changed: false }
}
}
/// <https://github.com/google/closure-compiler/blob/master/test/com/google/javascript/jscomp/PeepholeReplaceKnownMethodsTest.java>
#[cfg(test)]
mod test {
use oxc_allocator::Allocator;
use crate::tester;
fn test(source_text: &str, positive: &str) {
let allocator = Allocator::default();
let mut pass = super::PeepholeReplaceKnownMethods::new();
tester::test(&allocator, source_text, positive, &mut pass);
}
fn test_same(source_text: &str) {
test(source_text, source_text);
}
fn fold_same(js: &str) {
test_same(js);
}
fn fold(js: &str, expected: &str) {
test(js, expected);
}
#[test]
#[ignore]
fn test_string_index_of() {
fold("x = 'abcdef'.indexOf('g')", "x = -1");
fold("x = 'abcdef'.indexOf('b')", "x = 1");
fold("x = 'abcdefbe'.indexOf('b', 2)", "x = 6");
fold("x = 'abcdef'.indexOf('bcd')", "x = 1");
fold("x = 'abcdefsdfasdfbcdassd'.indexOf('bcd', 4)", "x = 13");
fold("x = 'abcdef'.lastIndexOf('b')", "x = 1");
fold("x = 'abcdefbe'.lastIndexOf('b')", "x = 6");
fold("x = 'abcdefbe'.lastIndexOf('b', 5)", "x = 1");
// Both elements must be strings. Don't do anything if either one is not
// string.
fold("x = 'abc1def'.indexOf(1)", "x = 3");
fold("x = 'abcNaNdef'.indexOf(NaN)", "x = 3");
fold("x = 'abcundefineddef'.indexOf(undefined)", "x = 3");
fold("x = 'abcnulldef'.indexOf(null)", "x = 3");
fold("x = 'abctruedef'.indexOf(true)", "x = 3");
// The following test case fails with JSC_PARSE_ERROR. Hence omitted.
// fold_same("x = 1.indexOf('bcd');");
fold_same("x = NaN.indexOf('bcd')");
fold_same("x = undefined.indexOf('bcd')");
fold_same("x = null.indexOf('bcd')");
fold_same("x = true.indexOf('bcd')");
fold_same("x = false.indexOf('bcd')");
// Avoid dealing with regex or other types.
fold_same("x = 'abcdef'.indexOf(/b./)");
fold_same("x = 'abcdef'.indexOf({a:2})");
fold_same("x = 'abcdef'.indexOf([1,2])");
// Template Strings
fold_same("x = `abcdef`.indexOf('b')");
fold_same("x = `Hello ${name}`.indexOf('a')");
fold_same("x = tag `Hello ${name}`.indexOf('a')");
}
}