From 038f4cccc9e1c8563adea55a5de8b15cf2f2d239 Mon Sep 17 00:00:00 2001 From: Matheus Wichman Date: Wed, 16 Nov 2022 18:56:28 -0300 Subject: [PATCH 1/4] [docs] Support demos with side effect imports --- docs/packages/markdown/extractImports.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/packages/markdown/extractImports.js b/docs/packages/markdown/extractImports.js index 3daefa092b7076..32aa7514ecff97 100644 --- a/docs/packages/markdown/extractImports.js +++ b/docs/packages/markdown/extractImports.js @@ -1,4 +1,4 @@ -const importModuleRegexp = /^import [^'"]* from ['"]([^'"\n ][^'"\n ]*)['"]/gm; +const importModuleRegexp = /^import [^'"]*(?: from )?['"]([^'"\n ][^'"\n ]*)['"]/gm; function extractImports(code) { return (code.match(importModuleRegexp) || []).map((x) => x.replace(importModuleRegexp, '$1')); From b9b873196f99809cdd1e96d5e34af1415df70240 Mon Sep 17 00:00:00 2001 From: Matheus Wichman Date: Tue, 22 Nov 2022 11:56:58 -0300 Subject: [PATCH 2/4] Add test case --- docs/packages/markdown/extractImports.js | 3 +- docs/packages/markdown/extractImports.test.js | 37 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 docs/packages/markdown/extractImports.test.js diff --git a/docs/packages/markdown/extractImports.js b/docs/packages/markdown/extractImports.js index 32aa7514ecff97..cbe0bda26bd0f4 100644 --- a/docs/packages/markdown/extractImports.js +++ b/docs/packages/markdown/extractImports.js @@ -1,4 +1,5 @@ -const importModuleRegexp = /^import [^'"]*(?: from )?['"]([^'"\n ][^'"\n ]*)['"]/gm; +const importModuleRegexp = + /^\s*import(?:["'\s]*(?:[\w*{}\n, ]+)from\s*)?["'\s]*([^"'\s]+)["'\s].*/gm; function extractImports(code) { return (code.match(importModuleRegexp) || []).map((x) => x.replace(importModuleRegexp, '$1')); diff --git a/docs/packages/markdown/extractImports.test.js b/docs/packages/markdown/extractImports.test.js new file mode 100644 index 00000000000000..7f95e974a43f8f --- /dev/null +++ b/docs/packages/markdown/extractImports.test.js @@ -0,0 +1,37 @@ +import { expect } from 'chai'; +import extractImports from './extractImports'; + +describe('extractImports', () => { + it('finds all imports', () => { + const imports = extractImports(` +import { + Component +} from '@angular2/core'; +import defaultMember from "module-1"; +import * as name from "module-2 "; +import { member } from " module-3"; +import { member as alias } from "module-4"; +import { member1 , +member2 } from "module-5"; +import { member1 , member2 as alias2 , member3 as alias3 } from "module-6"; +import defaultMember, { member, member } from "module-7"; +import defaultMember, * as name from "module-8"; +import "module-9"; + import "module-10"; +import * from './smdn'; + `); + + expect(imports[0]).to.equal('@angular2/core'); + expect(imports[1]).to.equal('module-1'); + expect(imports[2]).to.equal('module-2'); + expect(imports[3]).to.equal('module-3'); + expect(imports[4]).to.equal('module-4'); + expect(imports[5]).to.equal('module-5'); + expect(imports[6]).to.equal('module-6'); + expect(imports[7]).to.equal('module-7'); + expect(imports[8]).to.equal('module-8'); + expect(imports[9]).to.equal('module-9'); + expect(imports[10]).to.equal('module-10'); + expect(imports[11]).to.equal('./smdn'); + }); +}); From 0f95be6a084ceb780196948cde48b7a73255ab68 Mon Sep 17 00:00:00 2001 From: Matheus Wichman Date: Tue, 22 Nov 2022 12:20:49 -0300 Subject: [PATCH 3/4] Ignore import-syntax with template strings --- docs/packages/markdown/extractImports.js | 2 +- docs/packages/markdown/extractImports.test.js | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/packages/markdown/extractImports.js b/docs/packages/markdown/extractImports.js index cbe0bda26bd0f4..3b1a6ba5b6dc2c 100644 --- a/docs/packages/markdown/extractImports.js +++ b/docs/packages/markdown/extractImports.js @@ -1,5 +1,5 @@ const importModuleRegexp = - /^\s*import(?:["'\s]*(?:[\w*{}\n, ]+)from\s*)?["'\s]*([^"'\s]+)["'\s].*/gm; + /^\s*import(?:["'\s]*(?:[\w*{}\n, ]+)from\s*)?["'\s]*([^"'{}$\s]+)["'\s].*/gm; function extractImports(code) { return (code.match(importModuleRegexp) || []).map((x) => x.replace(importModuleRegexp, '$1')); diff --git a/docs/packages/markdown/extractImports.test.js b/docs/packages/markdown/extractImports.test.js index 7f95e974a43f8f..d9845294ab48ad 100644 --- a/docs/packages/markdown/extractImports.test.js +++ b/docs/packages/markdown/extractImports.test.js @@ -19,6 +19,7 @@ import defaultMember, * as name from "module-8"; import "module-9"; import "module-10"; import * from './smdn'; +import \${importName} from 'module11/\${importName}'; `); expect(imports[0]).to.equal('@angular2/core'); @@ -33,5 +34,6 @@ import * from './smdn'; expect(imports[9]).to.equal('module-9'); expect(imports[10]).to.equal('module-10'); expect(imports[11]).to.equal('./smdn'); + expect(imports[12]).to.equal(undefined); // It's not a valid import }); }); From 76e4765310f5867b063c3bdf0dec70f7bd24cb9e Mon Sep 17 00:00:00 2001 From: Matheus Wichman Date: Tue, 22 Nov 2022 13:43:50 -0300 Subject: [PATCH 4/4] Prevent matching variables starting with 'import' --- docs/packages/markdown/extractImports.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/packages/markdown/extractImports.js b/docs/packages/markdown/extractImports.js index 3b1a6ba5b6dc2c..b578414fe63911 100644 --- a/docs/packages/markdown/extractImports.js +++ b/docs/packages/markdown/extractImports.js @@ -1,5 +1,5 @@ const importModuleRegexp = - /^\s*import(?:["'\s]*(?:[\w*{}\n, ]+)from\s*)?["'\s]*([^"'{}$\s]+)["'\s].*/gm; + /^\s*import (?:["'\s]*(?:[\w*{}\n, ]+)from\s*)?["'\s]*([^"'{}$\s]+)["'\s].*/gm; function extractImports(code) { return (code.match(importModuleRegexp) || []).map((x) => x.replace(importModuleRegexp, '$1'));