Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: validate imports added to code snippets #1665

Merged
merged 47 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
199e8e8
implement helper extractImports for snippetPlugin
Torres-ssf Jan 18, 2024
ebfe9a1
add test suite for extractImports
Torres-ssf Jan 18, 2024
84b0df8
made findRegion to use extractImports
Torres-ssf Jan 18, 2024
4f50f2f
add test suite for findRegion
Torres-ssf Jan 18, 2024
70bceb3
add comments to snippetPlugin
Torres-ssf Jan 18, 2024
eb4d914
undo change
Torres-ssf Jan 18, 2024
44efb64
edit tsconfig file
Torres-ssf Jan 18, 2024
e895445
fix ts error
Torres-ssf Jan 18, 2024
eea07b5
validate if imports are used within the code snippet
Torres-ssf Jan 18, 2024
f206fc2
update test suite for extractImports
Torres-ssf Jan 18, 2024
398af02
add empty changeset
Torres-ssf Jan 18, 2024
33f29c3
fix TS error
Torres-ssf Jan 18, 2024
7755336
using addImport flag
Torres-ssf Jan 18, 2024
5833a49
implement way to ignore snippet import validation
Torres-ssf Jan 19, 2024
7d1174c
using addImport flag
Torres-ssf Jan 19, 2024
9287a4e
split extractImports in multiple helpers
Torres-ssf Jan 19, 2024
a1de836
documenting code
Torres-ssf Jan 19, 2024
86aa344
fix collectImportStatements
Torres-ssf Jan 19, 2024
7795049
refact code
Torres-ssf Jan 19, 2024
a5cb5f0
add test suite for extractImports helpers
Torres-ssf Jan 19, 2024
068f291
change flags to add import and to ignore
Torres-ssf Jan 19, 2024
de01527
using updated flag
Torres-ssf Jan 19, 2024
56e088b
Merge branch 'master' into st/docs/add-imports-to-code-snippets
Torres-ssf Jan 22, 2024
7e352b6
using regex in constants
Torres-ssf Jan 22, 2024
c5677b7
improve test cases
Torres-ssf Jan 22, 2024
560df67
Merge branch 'master' into st/docs/add-imports-to-code-snippets
Torres-ssf Jan 22, 2024
cb0ba98
update regex for collect imports
Torres-ssf Jan 22, 2024
f5961c1
update tests
Torres-ssf Jan 22, 2024
ec6ddae
update code snippet
Torres-ssf Jan 22, 2024
a550226
remove unused code snippet
Torres-ssf Jan 22, 2024
c135567
add type
Torres-ssf Jan 22, 2024
e0e819e
Merge branch 'master' into st/docs/add-imports-to-code-snippets
danielbate Jan 23, 2024
4c5d3a1
add semicolon
Torres-ssf Jan 24, 2024
c35a477
Merge branch 'st/docs/add-imports-to-code-snippets' of github.com:Fue…
Torres-ssf Jan 24, 2024
fd3610e
Merge branch 'master' into st/docs/add-imports-to-code-snippets
Torres-ssf Jan 24, 2024
8531bb3
add semicolon to import flags
Torres-ssf Jan 24, 2024
137f016
remove ignore import functionality
Torres-ssf Jan 24, 2024
9c8bd04
Merge branch 'master' into st/docs/add-imports-to-code-snippets
Torres-ssf Jan 24, 2024
74eb3be
documenting code
Torres-ssf Jan 24, 2024
99e8498
remove console.log
Torres-ssf Jan 24, 2024
8a0a556
Merge branch 'master' into st/docs/add-imports-to-code-snippets
Torres-ssf Jan 24, 2024
463410c
Merge branch 'master' into st/docs/add-imports-to-code-snippets
Torres-ssf Jan 25, 2024
9d845bc
Merge branch 'master' into st/docs/add-imports-to-code-snippets
Torres-ssf Jan 26, 2024
116f82f
Merge branch 'master' into st/docs/add-imports-to-code-snippets
Torres-ssf Jan 29, 2024
69b7d84
Merge branch 'master' into st/docs/add-imports-to-code-snippets
Torres-ssf Jan 29, 2024
fce5e0a
remove console.log
Torres-ssf Jan 29, 2024
d632054
ajusting test
Torres-ssf Jan 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
split extractImports in multiple helpers
  • Loading branch information
Torres-ssf committed Jan 19, 2024
commit 9287a4e8426ea888f581a9296e49b2d1de53878b
114 changes: 1 addition & 113 deletions apps/docs/.vitepress/plugins/snippetPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from 'path';
import fs from 'fs';
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { RuleBlock } from 'markdown-it/lib/parser_block';
import { extractImports } from './utils/extractImports';

function dedent(text: string): string {
const lines = text.split('\n');
Expand Down Expand Up @@ -37,119 +38,6 @@ function testLine(line: string, regexp: RegExp, regionName: string, end: boolean
);
}

export function extractImports(
filepath: string,
specifiedImports: string[],
snippetContent: string[]
) {
// Read file content
const fileContent = fs.readFileSync(filepath, 'utf8');
// Split content into lines
const lines = fileContent.split(/\r?\n/);

// Initialize variables
let importStatements: Record<string, Set<string>> = {};
let allImportedItems = new Set<string>();
let ignoredImports = new Set<string>();
let currentImport = '';
let collecting = false;

lines.forEach((line) => {
// Parse and process ignore import flag
if (line.trim().startsWith('// #ignoreImport')) {
const ignoreMatches = line.match(/\/\/ #ignoreImport: (.+)/);

if (ignoreMatches && ignoreMatches[1]) {
const importsToIgnore = ignoreMatches[1].split(',').map((item) => item.trim());

importsToIgnore.forEach((item) => ignoredImports.add(item));
}
}

// Start collecting import line
if (line.trim().startsWith('import')) {
collecting = true;
currentImport = line;
}
// Continue collecting import line if it spans multiple lines
else if (collecting && line.trim()) {
currentImport += ' ' + line.trim();
}

// Process the collected import line
if (collecting && line.includes('} from')) {
collecting = false;

const matches = currentImport.match(/import.*\{([^\}]*)\}.*from\s+'([^']+)';/);

if (matches && matches.length >= 3) {
const importedItems = matches[1].split(',').map((item) => item.trim());

const importSource = matches[2];

// Add imported items to the allImportedItems set for later checking
importedItems.forEach((item) => {
if (!ignoredImports.has(item)) {
allImportedItems.add(item);
}
});

// Filter out only the specified imports and ignore specified ones
const filteredItems = importedItems.filter(
(item) => specifiedImports.includes(item) && !ignoredImports.has(item)
);

if (filteredItems.length > 0) {
// Initialize set for the import source if not already done
importStatements[importSource] = importStatements[importSource] || new Set();
// Add filtered items to the set
filteredItems.forEach((item) => importStatements[importSource].add(item));
}
}
currentImport = '';
}
});

// Check if all specified imports were found in the file
const notFoundImports = specifiedImports.filter(
(importItem) => !allImportedItems.has(importItem) && !ignoredImports.has(importItem)
);

if (notFoundImports.length > 0) {
throw new FuelError(
ErrorCode.VITEPRESS_PLUGIN_ERROR,
`The following imports were not found in the file: ${notFoundImports.join(', ')}`
);
}

// Combine imports from the same source into single import statements
let combinedImports: string[] = [];
for (const source in importStatements) {
combinedImports.push(
`import { ${Array.from(importStatements[source]).join(', ')} } from '${source}';`
);
}

// Remove #addImport and #ignoreImport lines and join the content for validation
const validatedContent = snippetContent
.filter((line) => !/(#addImport:)|(#ignoreImport:)/.test(line))
.join('\n');

// Validate if each specified import is used in the code snippet
for (const importItem of specifiedImports) {
if (!validatedContent.includes(importItem) && !ignoredImports.has(importItem)) {
const formattedSnippet = '\n'.concat(snippetContent.map((line) => `${line}`).join('\n'));
throw new FuelError(
ErrorCode.VITEPRESS_PLUGIN_ERROR,
`The specified import '${importItem}' is not in use within the code snippet: ${formattedSnippet}`
);
}
}

// Return the combined import statements as a single string
return combinedImports.join('\n');
}

export function findRegion(lines: string[], regionName: string) {
// Regex to match region start/end comments
const regionRegexp = /^\/\/ ?#?((?:end)?region) ([\w*-]+)$/;
Expand Down
127 changes: 127 additions & 0 deletions apps/docs/.vitepress/plugins/utils/extractImports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import { FuelError, ErrorCode } from '@fuel-ts/errors';
import fs from 'fs';

function parseIgnoreImportFlags(lines: string[]): Set<string> {
const ignoredImports = new Set<string>();

lines.forEach((line) => {
// Parse and process ignore import flag
if (line.trim().startsWith('// #ignoreImport')) {
const ignoreMatches = line.match(/\/\/ #ignoreImport: (.+)/);

if (ignoreMatches && ignoreMatches[1]) {
const importsToIgnore = ignoreMatches[1].split(',').map((item) => item.trim());

importsToIgnore.forEach((item) => ignoredImports.add(item));
}
}
});

return ignoredImports;
}

function combineImportStatements(importStatements: Record<string, Set<string>>) {
return Object.keys(importStatements)
.map(
(source) => `import { ${Array.from(importStatements[source]).join(', ')} } from '${source}';`
)
.join('\n');
}

function validateImports(
specifiedImports: string[],
ignoredImports: Set<string>,
allImportedItems: Set<string>,
snippetContent: string[]
) {
const notFoundImports = specifiedImports.filter(
(importItem) => !allImportedItems.has(importItem) && !ignoredImports.has(importItem)
);

if (notFoundImports.length > 0) {
throw new FuelError(
ErrorCode.VITEPRESS_PLUGIN_ERROR,
`The following imports were not found in the file: ${notFoundImports.join(', ')}`
);
}

const validatedContent = snippetContent
.filter((line) => !/(#addImport:)|(#ignoreImport:)/.test(line))
.join('\n');

for (const importItem of specifiedImports) {
if (!validatedContent.includes(importItem) && !ignoredImports.has(importItem)) {
const formattedSnippet = '\n'.concat(snippetContent.map((line) => `${line}`).join('\n'));

throw new FuelError(
ErrorCode.VITEPRESS_PLUGIN_ERROR,
`The specified import '${importItem}' is not in use within the code snippet: ${formattedSnippet}`
);
}
}
}

function collectImportStatements(lines: string[], ignoredImports: Set<string>) {
let importStatements: Record<string, Set<string>> = {};
let allImportedItems: Set<string> = new Set();
let currentImport = '';
let collecting = false;

lines.forEach((line) => {
// Start collecting import line
if (line.trim().startsWith('import')) {
collecting = true;

currentImport = line;
} else if (collecting && line.trim()) {
// Continue collecting import line if it spans multiple lines
currentImport += ' ' + line.trim();
}

// Process the collected import line
if (collecting && line.includes('} from')) {
collecting = false;
const matches = currentImport.match(/import.*\{([^\}]*)\}.*from\s+'([^']+)';/);

if (matches && matches.length >= 3) {
const importedItems = matches[1].split(',').map((item) => item.trim());
const importSource = matches[2];

importedItems.forEach((item) => {
if (!ignoredImports.has(item)) {
allImportedItems.add(item);

if (!importStatements[importSource]) {
importStatements[importSource] = new Set();
}

importStatements[importSource].add(item);
}
});
}

currentImport = '';
}
});

return { importStatements, allImportedItems };
}

export function extractImports(
filepath: string,
specifiedImports: string[],
snippetContent: string[]
) {
// Read file content
const fileContent = fs.readFileSync(filepath, 'utf8');

// Split content into lines
const lines = fileContent.split(/\r?\n/);

const ignoredImports = parseIgnoreImportFlags(lines);
const { importStatements, allImportedItems } = collectImportStatements(lines, ignoredImports);

validateImports(specifiedImports, ignoredImports, allImportedItems, snippetContent);

return combineImportStatements(importStatements);
}