本文整理匯總了TypeScript中acorn.parse函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript parse函數的具體用法?TypeScript parse怎麽用?TypeScript parse使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了parse函數的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: readScript
export default function readScript(parser: Parser, start: number, attributes: Node[]) {
const scriptStart = parser.index;
const scriptEnd = parser.template.indexOf(scriptClosingTag, scriptStart);
if (scriptEnd === -1) parser.error(`<script> must have a closing tag`);
const source =
repeat(' ', scriptStart) + parser.template.slice(scriptStart, scriptEnd);
parser.index = scriptEnd + scriptClosingTag.length;
let ast;
try {
ast = acorn.parse(source, {
ecmaVersion: 9,
sourceType: 'module',
plugins: {
dynamicImport: true
}
});
} catch (err) {
parser.acornError(err);
}
if (!ast.body.length) return null;
ast.start = scriptStart;
return {
start,
end: parser.index,
attributes,
content: ast,
};
}
示例2: acornParse
export const loadAndParseConformation = (
fixtureFilePath: string
): ConformationTest[] => {
const pathname = path.join(fixturesFolderPath, fixtureFilePath)
const content = fs.readFileSync(pathname, 'utf8')
const expectedValues: any[] = []
const tests: ConformationTest[] = []
acornParse(content, {
ecmaVersion: 5,
locations: true,
onComment: (isBlock, text) => {
if (!isBlock) {
// tslint:disable-next-line
expectedValues.push(eval(text.trim()))
}
}
})
const context = createContext({ week: 3 })
const ast = parse(content, context).parser.program!
for (const [idx, statement] of ast.body.entries()) {
tests.push({
statement: statement as es.Statement,
expectedValue: expectedValues[idx]
})
}
return tests
}
示例3: processModule
function processModule(modulePath: string, context: IPaeckchenContext, detectedGlobals: IDetectedGlobals,
plugins: any): ESTree.Program {
// parse...
const comments: any[] = [];
const tokens: any[] = [];
const moduleAst = parse(context.host.readFile(modulePath).toString(), {
ecmaVersion: 7,
sourceType: 'module',
locations: true,
ranges: true,
allowHashBang: true,
onComment: comments,
onToken: tokens
});
// only attach comments which are not sourceMaps
attachComments(moduleAst,
comments.filter((comment: any) => comment.value.indexOf('# sourceMappingURL=') === -1), tokens);
// ... check for global features...
checkGlobals(detectedGlobals, moduleAst);
// ... and rewrite ast
Object.keys(plugins).forEach(plugin => {
plugins[plugin](moduleAst, modulePath, context);
});
return moduleAst;
}
示例4: it
it('returns correct scope name if closure is passed as arguments', () => {
const node: any = (parse('var x = function (n) {};').body[0] as any)
.declarations[0].init
const closure = new Closure(node, 0, 0)
const scope = closure.createScope([new Closure(node, 0, 1)])
expect(scope.name).toBe('lambda_0(lambda_1)')
})
示例5: getAST
public async getAST(): Promise<ESTree.BaseNode> {
if (!this.ast) {
const file = await fs.readFile(this.filePath, 'utf8');
this.ast = acorn.parse(file, {
allowHashBang: true,
allowReserved: true,
sourceType: 'module',
});
}
return this.ast;
}
示例6: it
it('interprets simple program', () => {
const generator = evalProgram(parse('1 + 2;'), createInterpreter())
const states = []
let g = generator.next()
while (g.value.isRunning) {
states.push(g.value)
g = generator.next()
}
expect(states.length).toBe(7)
expect(states[0].node!.type).toBe('ExpressionStatement')
expect(states[1].node!.type).toBe('BinaryExpression')
expect(states[2].node!.type).toBe('Literal')
expect(states[3].node!.type).toBe('Literal')
expect(states[4].node!.type).toBe('Literal')
expect(states[5].node!.type).toBe('Literal')
expect(states[6].node!.type).toBe('BinaryExpression')
})
示例7: bundle
export function bundle(options: IBundleOptions, host: IHost = new DefaultHost()): string {
const context: IPaeckchenContext = {
config: createConfig(options, host),
host
};
if (!context.config.input.entryPoint) {
throw new Error('Missing entry-point');
}
const detectedGlobals: IDetectedGlobals = {
global: false,
process: false,
buffer: false
};
const paeckchenAst = parse(paeckchenSource);
const modules = getModules(paeckchenAst).elements;
const absoluteEntryPath = join(host.cwd(), context.config.input.entryPoint);
// start bundling...
enqueueModule(getModulePath('.', absoluteEntryPath, context));
while (bundleNextModule(modules, context, detectedGlobals)) {
process.stderr.write('.');
}
// ... when ready inject globals...
injectGlobals(detectedGlobals, paeckchenAst, context);
// ... and bundle global dependencies
while (bundleNextModule(modules, context, detectedGlobals)) {
process.stderr.write('.');
}
process.stderr.write('\n');
const bundleResult = generate(paeckchenAst, {
comment: true
});
if (context.config.output.file) {
host.writeFile(
host.joinPath(context.config.output.folder, context.config.output.file),
bundleResult);
return undefined;
}
return bundleResult;
}
示例8: createContext
export const parse = (source: string, state: StaticState | number) => {
if (typeof state === 'number') {
state = createContext({ week: state })
}
try {
const program = acornParse(source, createAcornParserOptions(state))
state.parser.program = program
state.cfg.scopes[0].node = program
simple(program, walkers, undefined, state)
} catch (error) {
if (error instanceof SyntaxError) {
const loc = (error as any).loc
const location = {
start: { line: loc.line, column: loc.column },
end: { line: loc.line, column: loc.column + 1 }
}
const message = error.toString()
state.parser.errors.push(new FatalSyntaxError(location, error.toString()))
} else {
throw error
}
}
return state
}