Skip to content

Commit

Permalink
module: validate that strip-types does not insert any code
Browse files Browse the repository at this point in the history
  • Loading branch information
ChALkeR committed Jul 31, 2024
1 parent 346469c commit 4ab7ef5
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions lib/internal/modules/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,10 +307,49 @@ function lazyLoadTSParser() {
return parseTS;
}

function doesTSStripTypesResultMatchSource(code, source) {
const sourceBuf = Buffer.from(source);

Check failure on line 311 in lib/internal/modules/helpers.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Unexpected use of 'Buffer'. Use `const { Buffer } = require('buffer');` instead of the global

Check failure on line 311 in lib/internal/modules/helpers.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

'Buffer' is not defined
const codeBuf = Buffer.from(code);

Check failure on line 312 in lib/internal/modules/helpers.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Unexpected use of 'Buffer'. Use `const { Buffer } = require('buffer');` instead of the global

Check failure on line 312 in lib/internal/modules/helpers.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

'Buffer' is not defined
if (sourceBuf.length !== codeBuf.length) return false;

Check failure on line 313 in lib/internal/modules/helpers.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected { after 'if' condition
for (let i = 0; i < codeBuf.length; i++) {
// should match either the source buffer or spaces (sometimes multi-byte) or semicolon

Check failure on line 315 in lib/internal/modules/helpers.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Comments should not begin with a lowercase character
const val = codeBuf[i];
if (val === sourceBuf[i]) continue; // source match

Check failure on line 317 in lib/internal/modules/helpers.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected { after 'if' condition
// https://github.com/nodejs/amaro/blob/e533394f576f946add41dd8816816435e8100c3b/deps/swc/crates/swc_fast_ts_strip/src/lib.rs#L400-L414
if (val === 0x3b) continue; // Semicolon 3b

Check failure on line 319 in lib/internal/modules/helpers.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected { after 'if' condition
// https://github.com/nodejs/amaro/blob/e533394f576f946add41dd8816816435e8100c3b/deps/swc/crates/swc_fast_ts_strip/src/lib.rs#L200-L226
if (val === 0x20) continue; // Space 20

Check failure on line 321 in lib/internal/modules/helpers.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected { after 'if' condition
if (val === 0xc2) {
// No-Break Space 00A0
if (i + 1 >= codeBuf.length) return false;

Check failure on line 324 in lib/internal/modules/helpers.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected { after 'if' condition
if (codeBuf[++i] !== 0xa0) return false;
continue;
}
if (val === 0xc2) {
// En Space 2002
if (i + 2 >= codeBuf.length) return false;
if (codeBuf[++i] !== 0x80) return false;
if (codeBuf[++i] !== 0x82) return false;
continue;
}
if (val === 0xef) {
// ZWNBSP FEFF
if (i < 1 || codeBuf[i - 1] !== 0x20) return false; // only can get insterted after a space
if (i + 2 >= codeBuf.length) return false;
if (codeBuf[++i] !== 0xbb) return false;
if (codeBuf[++i] !== 0xbf) return false;
continue;
}
return false;
}
return true;
}

function tsParse(source) {
if (!source || typeof source !== 'string') { return; }
const transformSync = lazyLoadTSParser();
const { code } = transformSync(source, { mode: 'strip-only' });
assert(doesTSStripTypesResultMatchSource(code, source), 'Unexpected strip-types result');
return code;
}

Expand Down

0 comments on commit 4ab7ef5

Please sign in to comment.