Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Commit

Permalink
feat(parser): handle template literal without expressions (#981)
Browse files Browse the repository at this point in the history
Co-authored-by: DevSide <DevSide@github.com>
  • Loading branch information
DevSide and DevSide committed Mar 30, 2021
1 parent 2c1fa89 commit 79cca54
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 24 deletions.
72 changes: 49 additions & 23 deletions lib/detector.js
Expand Up @@ -67,6 +67,29 @@ function valid2(v2) {
);
}

function isLiteral(argument) {
return (
argument &&
(argument.type === 'Literal' ||
(argument.type === 'TemplateLiteral' &&
argument.expressions.length === 0))
);
}

function getLiteralValue(node) {
if (!node) {
return;
}

if (node.type === 'Literal') {
return node.value;
}

if (node.type === 'TemplateLiteral') {
return node.quasis[0].value.raw;
}
}

function visitor_REQUIRE_RESOLVE(n) {
// eslint-disable-line camelcase
const c = n.callee;
Expand All @@ -80,13 +103,13 @@ function visitor_REQUIRE_RESOLVE(n) {
c.property.name === 'resolve';
if (!ci) return null;
const f =
n.type === 'CallExpression' &&
n.arguments &&
n.arguments[0] &&
n.arguments[0].type === 'Literal';
n.type === 'CallExpression' && n.arguments && isLiteral(n.arguments[0]);
if (!f) return null;
const m = n.arguments[1] && n.arguments[1].type === 'Literal';
return { v1: n.arguments[0].value, v2: m ? n.arguments[1].value : null };
const m = isLiteral(n.arguments[1]);
return {
v1: getLiteralValue(n.arguments[0]),
v2: m ? getLiteralValue(n.arguments[1]) : null,
};
}

function visitor_REQUIRE(n) {
Expand All @@ -96,13 +119,13 @@ function visitor_REQUIRE(n) {
const ci = c.type === 'Identifier' && c.name === 'require';
if (!ci) return null;
const f =
n.type === 'CallExpression' &&
n.arguments &&
n.arguments[0] &&
n.arguments[0].type === 'Literal';
n.type === 'CallExpression' && n.arguments && isLiteral(n.arguments[0]);
if (!f) return null;
const m = n.arguments[1] && n.arguments[1].type === 'Literal';
return { v1: n.arguments[0].value, v2: m ? n.arguments[1].value : null };
const m = isLiteral(n.arguments[1]);
return {
v1: getLiteralValue(n.arguments[0]),
v2: m ? getLiteralValue(n.arguments[1]) : null,
};
}

function visitor_IMPORT(n) {
Expand Down Expand Up @@ -133,11 +156,10 @@ function visitor_PATH_JOIN(n) {
const f =
n.type === 'CallExpression' &&
n.arguments &&
n.arguments[1] &&
n.arguments[1].type === 'Literal' &&
isLiteral(n.arguments[1]) &&
n.arguments.length === 2; // TODO concate them
if (!f) return null;
return { v1: n.arguments[1].value };
return { v1: getLiteralValue(n.arguments[1]) };
}

export function visitor_SUCCESSFUL(node, test) {
Expand Down Expand Up @@ -207,14 +229,16 @@ function visitorNonLiteral(n) {
const f =
n.type === 'CallExpression' &&
n.arguments &&
n.arguments[0] &&
n.arguments[0].type !== 'Literal';
!isLiteral(n.arguments[0]);
if (!f) return null;
const m = n.arguments[1];
if (!m) return { v1: reconstruct(n.arguments[0]) };
const q = n.arguments[1] && n.arguments[1].type === 'Literal';
const q = isLiteral(n.arguments[1]);
if (!q) return null;
return { v1: reconstruct(n.arguments[0]), v2: n.arguments[1].value };
return {
v1: reconstruct(n.arguments[0]),
v2: getLiteralValue(n.arguments[1]),
};
})() ||
(() => {
const c = n.callee;
Expand All @@ -224,14 +248,16 @@ function visitorNonLiteral(n) {
const f =
n.type === 'CallExpression' &&
n.arguments &&
n.arguments[0] &&
n.arguments[0].type !== 'Literal';
!isLiteral(n.arguments[0]);
if (!f) return null;
const m = n.arguments[1];
if (!m) return { v1: reconstruct(n.arguments[0]) };
const q = n.arguments[1] && n.arguments[1].type === 'Literal';
const q = isLiteral(n.arguments[1]);
if (!q) return null;
return { v1: reconstruct(n.arguments[0]), v2: n.arguments[1].value };
return {
v1: reconstruct(n.arguments[0]),
v2: getLiteralValue(n.arguments[1]),
};
})()
);
}
Expand Down
2 changes: 1 addition & 1 deletion test/test-50-ast-parsing/main.js
Expand Up @@ -29,7 +29,7 @@ left =
return line.indexOf('/***/ ') >= 0;
})
.map(function (line) {
return line.split('/***/ ')[1];
return line.split('/***/ ')[1].replace(/['`]/g, '"');
})
.join('\n') + '\n';

Expand Down
19 changes: 19 additions & 0 deletions test/test-50-ast-parsing/test-y-data.txt
Expand Up @@ -7,19 +7,24 @@ function hellee(a, b, c) {

require.resolve();
/***/ require.resolve("barbos1");
/***/ require.resolve(`barbos1`);
/***/ require.resolve(`barbos1`, `must-exclude1`);
/***/ require.resolve("barbos1", "must-exclude1");
/***/ require.resolve("barbos1", "may-exclude1");
/***/ require.resolve("barbos1", "unknown1");

require();
/***/ require("barbos2");
/***/ require(`barbos2`);
/***/ require(`barbos2`, `must-exclude2`);
/***/ require("barbos2", "must-exclude2");
/***/ require("barbos2", "may-exclude2");
/***/ require("barbos2", "unknown2");

path.join();
path.join(__dirname);
/***/ path.join(__dirname, "file");
/***/ path.join(__dirname, `file`);

// TODO path.join(__dirname + "/file");

Expand All @@ -33,12 +38,16 @@ function helleeTry(a, b, c) {

try { require.resolve(); } catch (_) {}
/***/ try { require.resolve("barbos1"); } catch (_) {}
/***/ try { require.resolve(`barbos1`); } catch (_) {}
/***/ try { require.resolve(`barbos1`, `must-exclude1`); } catch (_) {}
/***/ try { require.resolve("barbos1", "must-exclude1"); } catch (_) {}
/***/ try { require.resolve("barbos1", "may-exclude1"); } catch (_) {}
/***/ try { require.resolve("barbos1", "unknown1"); } catch (_) {}

try { require(); } catch (_) {}
/***/ try { require("barbos2"); } catch (_) {}
/***/ try { require(`barbos2`); } catch (_) {}
/***/ try { require(`barbos2`, `must-exclude2`); } catch (_) {}
/***/ try { require("barbos2", "must-exclude2"); } catch (_) {}
/***/ try { require("barbos2", "may-exclude2"); } catch (_) {}
/***/ try { require("barbos2", "unknown2"); } catch (_) {}
Expand All @@ -65,19 +74,24 @@ function helluu(a, b, c) {

require.resolve();
/***/ require.resolve("barbos1");
/***/ require.resolve(`barbos1`);
/***/ require.resolve(`barbos1`, `must-exclude1`);
/***/ require.resolve("barbos1", "must-exclude1");
/***/ require.resolve("barbos1", "may-exclude1");
/***/ require.resolve("barbos1", "unknown1");

require();
/***/ require("barbos2");
/***/ require(`barbos2`);
/***/ require(`barbos2`, `must-exclude2`);
/***/ require("barbos2", "must-exclude2");
/***/ require("barbos2", "may-exclude2");
/***/ require("barbos2", "unknown2");

path.join();
path.join(__dirname);
/***/ path.join(__dirname, "file");
/***/ path.join(__dirname, `file`);

// TODO path.join(__dirname + "/file");

Expand All @@ -100,19 +114,24 @@ function helluuTry(a, b, c) {

try { require.resolve(); } catch (_) {}
/***/ try { require.resolve("barbos1"); } catch (_) {}
/***/ try { require.resolve(`barbos1`); } catch (_) {}
/***/ try { require.resolve(`barbos1`, `must-exclude1`); } catch (_) {}
/***/ try { require.resolve("barbos1", "must-exclude1"); } catch (_) {}
/***/ try { require.resolve("barbos1", "may-exclude1"); } catch (_) {}
/***/ try { require.resolve("barbos1", "unknown1"); } catch (_) {}

try { require(); } catch (_) {}
/***/ try { require("barbos2"); } catch (_) {}
/***/ try { require(`barbos2`); } catch (_) {}
/***/ try { require(`barbos2`, `must-exclude2`); } catch (_) {}
/***/ try { require("barbos2", "must-exclude2"); } catch (_) {}
/***/ try { require("barbos2", "may-exclude2"); } catch (_) {}
/***/ try { require("barbos2", "unknown2"); } catch (_) {}

try { path.join(); } catch (_) {}
try { path.join(__dirname); } catch (_) {}
/***/ try { path.join(__dirname, "file"); } catch (_) {}
/***/ try { path.join(__dirname, `file`); } catch (_) {}

// TODO path.join(__dirname + "/file");

Expand Down

0 comments on commit 79cca54

Please sign in to comment.