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

Add support for @stdlib imports for syntax highlighting (Issue #51) #52

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
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
14 changes: 12 additions & 2 deletions src/model/contract.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
'use strict';
import * as path from 'path';
import * as fs from 'fs';
import { formatPath } from '../util';

export class Contract {
public code: string;
// TODO: Import needs to be a class including if is local, absolutePath, module etc
public imports: Array<string>;
public absolutePath: string;

constructor(absoulePath: string, code: string) {
this.absolutePath = this.formatContractPath(absoulePath);
this.code = code;
Expand All @@ -24,12 +25,21 @@ export class Contract {
public resolveImports() {
const importRegEx = /^\s?import\s+[^'"]*['"](.*)['"]\s*/gm;
let foundImport = importRegEx.exec(this.code);

while (foundImport != null) {
const importPath = foundImport[1];

if (this.isImportLocal(importPath)) {
const importFullPath = this.formatContractPath(path.resolve(path.dirname(this.absolutePath), foundImport[1]));
this.imports.push(importFullPath);
} else if (importPath.startsWith('@stdlib/')) {
const stdlibPath = path.resolve(path.dirname(this.absolutePath), "../node_modules/@tact-lang/compiler/stdlib");
const contractFileName = importPath.replace('@stdlib/', '') + '.tact';
const contractFullPath = path.join(stdlibPath, 'libs', contractFileName);

if (fs.existsSync(contractFullPath)) {
this.imports.push(contractFullPath);
}
} else {
this.imports.push(importPath);
}
Expand Down