|
| 1 | +/* eslint-disable no-template-curly-in-string */ |
| 2 | +import outdent from 'outdent'; |
| 3 | +import {getTester} from './utils/test.mjs'; |
| 4 | + |
| 5 | +const {test} = getTester(import.meta); |
| 6 | + |
| 7 | +test.snapshot({ |
| 8 | + valid: [ |
| 9 | + 'JSON.parse(await fs.readFile(file, "buffer"));', |
| 10 | + 'JSON.parse(await fs.readFile(file, "gbk"));', |
| 11 | + 'JSON.parse(await fs.readFile(file, ));', |
| 12 | + 'JSON.parse(await fs.readFile(file, unknown));', |
| 13 | + 'JSON.parse(await fs.readFile(...file, "utf8"));', |
| 14 | + 'JSON.parse(await fs.readFile(file, ..."utf8"));', |
| 15 | + 'JSON.parse(await fs.readFile(file, 0));', |
| 16 | + 'JSON.parse(await fs.readFile(file, "utf8", extraArgument));', |
| 17 | + 'JSON.parse(await fs.readFile?.(file, "utf8"));', |
| 18 | + 'JSON.parse(await fs?.readFile(file, "utf8"));', |
| 19 | + 'JSON.parse(await fs.notReadFileMethod(file, "utf8"));', |
| 20 | + 'JSON.parse?.(await fs.readFile(file, "utf8"));', |
| 21 | + 'JSON?.parse(await fs.readFile(file, "utf8"));', |
| 22 | + 'window.JSON.parse(await fs.readFile(file, "utf8"));', |
| 23 | + 'JSON.stringify(await fs.readFile(file, "utf8"));', |
| 24 | + 'NOT_JSON.parse(await fs.readFile(file, "utf8"));', |
| 25 | + 'for (const string of []) JSON.parse(string);', |
| 26 | + 'JSON.parse(await fs.readFile(file, "utf8"), extraArgument);', |
| 27 | + 'JSON.parse(foo);', |
| 28 | + 'JSON.parse();', |
| 29 | + 'JSON.parse(await fs.readFile(file, {encoding: "not-utf8"}));', |
| 30 | + 'JSON.parse(await fs.readFile(file, {encoding: "utf8", extraProperty: "utf8"}));', |
| 31 | + 'JSON.parse(await fs.readFile(file, {...encoding}));', |
| 32 | + 'JSON.parse(await fs.readFile(file, {encoding: unknown}));', |
| 33 | + 'const encoding = "gbk";JSON.parse(await fs.readFile(file, {encoding: encoding}));', |
| 34 | + 'const readingOptions = {encoding: "utf8", extraProperty: undefined};JSON.parse(await fs.readFile(file, readingOptions));', |
| 35 | + outdent` |
| 36 | + const {string} = await fs.readFile(file, "utf8"); |
| 37 | + JSON.parse(string); |
| 38 | + `, |
| 39 | + outdent` |
| 40 | + const string = fs.readFile(file, () => {}); |
| 41 | + JSON.parse(string); |
| 42 | + `, |
| 43 | + outdent` |
| 44 | + const abortControl = new AbortControl(); |
| 45 | + const {signal} = abortControl; |
| 46 | + const promise = readFile(fileName, { encoding: "utf8", signal }); |
| 47 | + if (foo) { |
| 48 | + JSON.parse(await promise); |
| 49 | + } else { |
| 50 | + controller.abort(); |
| 51 | + } |
| 52 | + `, |
| 53 | + outdent` |
| 54 | + const string= await fs.readFile(file, "utf8"); |
| 55 | + console.log(string); |
| 56 | + JSON.parse(string); |
| 57 | + `, |
| 58 | + outdent` |
| 59 | + const string= await fs.readFile(file, "utf8"); |
| 60 | + JSON.parse(\`[\${string}]\`); |
| 61 | + `, |
| 62 | + outdent` |
| 63 | + const foo = {}; |
| 64 | + foo.bar = await fs.readFile(file, "utf8"); |
| 65 | + JSON.parse(foo.bar); |
| 66 | + `, |
| 67 | + outdent` |
| 68 | + const foo = await fs.readFile(file, "utf8"); |
| 69 | + const bar = await foo; |
| 70 | + console.log(baz); |
| 71 | + const baz = await bar; |
| 72 | + JSON.parse(baz); |
| 73 | + `, |
| 74 | + outdent` |
| 75 | + const foo = fs.readFile(file, "utf8"); |
| 76 | + function fn1() { |
| 77 | + const foo = "{}"; |
| 78 | + JSON.parse(foo); |
| 79 | + } |
| 80 | + `, |
| 81 | + ], |
| 82 | + invalid: [ |
| 83 | + 'JSON.parse(await fs.readFile(file, "utf8"));', |
| 84 | + 'JSON.parse(await fs.readFile(file, "utf8",));', |
| 85 | + 'JSON.parse(await fs.readFile(file, "UTF-8"));', |
| 86 | + 'JSON.parse(await fs.readFileSync(file, "utf8"));', |
| 87 | + 'JSON.parse(fs.readFileSync(file, "utf8"));', |
| 88 | + 'const CHARSET = "UTF8"; JSON.parse(await fs.readFile(file, CHARSET));', |
| 89 | + 'const EIGHT = 8; JSON.parse(await fs.readFile(file, `utf${EIGHT}`));', |
| 90 | + 'JSON.parse(await fs["readFile"](file, "utf8"));', |
| 91 | + 'JSON.parse(await fs.readFile(file, {encoding: "utf8"}));', |
| 92 | + 'const EIGHT = 8; JSON.parse(await fs.readFile(file, {encoding: `utf${EIGHT}`}));', |
| 93 | + 'JSON.parse(await fs.readFile(file, {...({encoding: "utf8"})}));', |
| 94 | + 'const encoding = "utf8";JSON.parse(await fs.readFile(file, {encoding}));', |
| 95 | + 'const CHARSET = "utF-8", readingOptions = {encoding: CHARSET}; JSON.parse(await fs.readFile(file, readingOptions));', |
| 96 | + 'const EIGHT = 8, ENCODING = "encoding"; JSON.parse(await fs.readFile(file, {[ENCODING]: `utf${EIGHT}`}));', |
| 97 | + outdent` |
| 98 | + const string = await fs.readFile(file, "utf8"); |
| 99 | + JSON.parse(string); |
| 100 | + `, |
| 101 | + outdent` |
| 102 | + let string = await fs.readFile(file, "utf8"); |
| 103 | + JSON.parse(string); |
| 104 | + `, |
| 105 | + outdent` |
| 106 | + const foo = await await fs.readFile(file, "utf8"); |
| 107 | + const bar = await await foo; |
| 108 | + const baz = await bar; |
| 109 | + JSON.parse(baz); |
| 110 | + `, |
| 111 | + outdent` |
| 112 | + var foo = await fs.readFile(file, "utf8"); |
| 113 | + let bar = await foo; |
| 114 | + const baz = await bar; |
| 115 | + JSON.parse(baz); |
| 116 | + `, |
| 117 | + outdent` |
| 118 | + const foo = fs.readFile(file, "utf8"); |
| 119 | + async function fn1() { |
| 120 | + const bar = await foo; |
| 121 | +
|
| 122 | + function fn2() { |
| 123 | + const baz = bar; |
| 124 | + JSON.parse(baz); |
| 125 | + } |
| 126 | + } |
| 127 | + `, |
| 128 | + outdent` |
| 129 | + const buffer = fs.readFile(file, "utf8"); /* Should report */ |
| 130 | + const foo = buffer; |
| 131 | + async function fn1() { |
| 132 | + const buffer = fs.readFile(file, "utf8"); /* Should NOT report */ |
| 133 | + JSON.parse(await foo); |
| 134 | + } |
| 135 | + `, |
| 136 | + outdent` |
| 137 | + const buffer = fs.readFile(file, "utf8"); /* Should report */ |
| 138 | + const foo = buffer; |
| 139 | + async function fn1() { |
| 140 | + const buffer = fs.readFile(file, "utf8"); /* Should NOT report */ |
| 141 | + const baz = foo; |
| 142 | + for (;;) { |
| 143 | + const buffer = fs.readFile(file, "utf8"); /* Should NOT report */ |
| 144 | + JSON.parse(await baz); |
| 145 | + } |
| 146 | + } |
| 147 | + `, |
| 148 | + outdent` |
| 149 | + const foo = fs.readFile(file, "utf8"); |
| 150 | + function fn1() { |
| 151 | + JSON.parse(foo); |
| 152 | +
|
| 153 | + function fn2() { |
| 154 | + const foo = "{}"; |
| 155 | + } |
| 156 | + } |
| 157 | + `, |
| 158 | + // Maybe false positive, we can trace the callee if necessary |
| 159 | + outdent` |
| 160 | + const string = await NOT_A_FS_MODULE.readFile(file, "utf8"); |
| 161 | + JSON.parse(string); |
| 162 | + `, |
| 163 | + ], |
| 164 | +}); |
0 commit comments