Let the import scanner accept the plug-in loader's own module import
Quick Setup made organization code import the plug-in loader, which loads packages/plugins/<kind>/<name>/index.mjs by a computed path. The scanner now allows exactly that form in the loader file and still refuses it anywhere else.
This commit is contained in:
1 parent
c09570d696
commit
3272fd9c6f
2 files changed
+15
-1
No files matched your search
@@ -36,6 +36,10 @@ const IMPORT=new RegExp([
|
||||
`${DYNAMIC.source}\\s*['"](\\.{1,2}\\/[^'"]+)['"]\\s*\\)`,
|
||||
].join('|'),'g');
|
||||
const DYNAMIC_LITERAL=/^import\s*\(\s*(['"])[^'"\n]*\1\s*\)/;
|
||||
// The plug-in loader's one computed import, allowed only in that file and only in this exact form. It loads
|
||||
// packages/plugins/<kind>/<name>/index.mjs after checking that path, and a real copy ships all of
|
||||
// packages/plugins, so a fixture that never loads a plug-in needs nothing more from it.
|
||||
const LOADER_FILE='packages/plugins/src/loader.mjs',LOADER_IMPORT=/^import\(pathToFileURL\(expected\)\.href\)/;
|
||||
const REGEX_AFTER='(,=:[!&|?{};+-*%<>~^';
|
||||
const inert=(literals,index)=>literals.some(([from,to])=>index>=from&&index<to);
|
||||
const sources=(dir)=>readdirSync(dir,{withFileTypes:true}).flatMap((e)=>e.isDirectory()?sources(join(dir,e.name)):e.name.endsWith('.mjs')?[join(dir,e.name)]:[]);
|
||||
@@ -110,7 +114,9 @@ function specifiers(file,text){
|
||||
catch(error){throw new Error(`${file} cannot be read for imports: ${error.message}`);}
|
||||
const {code,literals}=scanned,live=(m)=>!inert(literals,m.index);
|
||||
for(const m of code.matchAll(DYNAMIC)){
|
||||
if(live(m)&&!DYNAMIC_LITERAL.test(code.slice(m.index,m.index+512)))throw new Error(`${file} has a dynamic import that is not a plain string literal`);
|
||||
const at=code.slice(m.index,m.index+512);
|
||||
if(file===LOADER_FILE&&LOADER_IMPORT.test(at))continue;
|
||||
if(live(m)&&!DYNAMIC_LITERAL.test(at))throw new Error(`${file} has a dynamic import that is not a plain string literal`);
|
||||
}
|
||||
return [...code.matchAll(IMPORT)].filter(live).map((m)=>m[1]??m[2]??m[3]);
|
||||
}
|
||||
|
||||
@@ -29,6 +29,14 @@ test('the real tree gives the plug-in files the loader reads', ()=>{
|
||||
assert.deepEqual(found,[...found].sort());
|
||||
});
|
||||
|
||||
test('the loader exception holds only for the loader file and its exact computed import', ()=>{
|
||||
const text="const module=await import(pathToFileURL(expected).href);\n";
|
||||
const at=(file)=>tree({[`${BIZ}a.mjs`]:"import '../../plugins/src/loader.mjs';\n",[`repo/${file}`]:text});
|
||||
assert.deepEqual(businessImportedFiles(at('packages/plugins/src/loader.mjs')),['packages/plugins/src/loader.mjs']);
|
||||
assert.throws(()=>businessImportedFiles(tree({[`${BIZ}a.mjs`]:"import '../../plugins/src/other.mjs';\n",'repo/packages/plugins/src/other.mjs':text})),/not a plain string literal/);
|
||||
assert.throws(()=>businessImportedFiles(tree({[`${BIZ}a.mjs`]:"import '../../plugins/src/loader.mjs';\n",'repo/packages/plugins/src/loader.mjs':"await import(x);\n"})),/not a plain string literal/);
|
||||
});
|
||||
|
||||
test('every static form and a literal dynamic import is found, and the imports of what it finds in turn', ()=>{
|
||||
const repo=withBusiness([
|
||||
"import x from '../../other/src/from.mjs';",
|
||||
|
||||
Reference in new issue
Block a user