除了使用 TM_FILENAME_BASE 之外,有没有办法修剪 TM_FILENAME? [英] Is there a way to trim a TM_FILENAME beyond using TM_FILENAME_BASE?

查看:96
本文介绍了除了使用 TM_FILENAME_BASE 之外,有没有办法修剪 TM_FILENAME?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 redux 容器文件创建一个片段,该文件导入一些具有相同基本名称的 react 文件.TM_FILENAME_BASE 非常适合从文件名中删除 .js,但在这种情况下,我的组件文件的扩展名是 fun-thing.component.js,容器将效仿扩展名 fun-thing.container.js.

我用来在第一个句点之前选择所有内容的正则表达式是 ^([^.]+)

 Redux 容器":{"前缀": "rc",身体": [//@流动","从'react-redux'导入{连接};",从'./${TM_FILENAME/^([^.]+)/$1/}.component'导入{ ${TM_FILENAME/^([^.]+)/${1:/pascalcase}/}Component };","","const mapStateToProps = (state) => ({});","","const mapDispatchToProps = {};","","export const ${TM_FILENAME/^([^.]+)/${1:/pascalcase}/} = connect("," mapStateToProps,"," mapDispatchToProps",")(${TM_FILENAME/^([^.]+)/${1:/pascalcase}/}组件);"],"description": "为普通组件创建普通容器"}

预期

//@flow从'react-redux'导入{连接};import { FunThingComponent } from './fun-thing.component';...

实际

//@flow从'react-redux'导入{连接};import { FunThing.container.jsComponent } from './FunThing.container.js.component';...

如您所见,它没有省略文件扩展名.

解决方案

这两项工作:

 "import { ${TM_FILENAME/^([^.]+).*/${1:/pascalcase}/}Component } from './${TM_FILENAME/^([^.]+).*/$1/}.component';",从 './${TM_FILENAME/(.*?)\\..+/$1/导入 { ${TM_FILENAME/(.*?)\\..+/${1:/pascalcase}/}Component }}.成分';"

如果您想根据需要排除变量的一部分,则使用 vscode 代码段进行转换,从文件名中删除 .component.js,然后从文件名中删除该部分必须在正则表达式中考虑变量 - 因此 (.*?)\\..+.

否则变量的看不见"部分就会通过.

所以你的正则表达式 ^([^.]+) 准确地捕获了第一个 之前的文件名部分. 但是变量的其余部分是传递通过"未修改.

你可以通过这个例子更清楚地看到这一点:

"import { ${TM_FILENAME//${1:/pascalcase}/}Component }

产生:

import { fun-thing.component.jsComponent }

所以整个文件名都被传递了,尽管没有被捕获.

${someVariable/所有要转换的东西/对前一个做什么/}

如果它不在所有要转换的东西"部分,它什么也不会发生.

I am trying to create a snippet for a redux container file that takes an import of some react file of the same base name. TM_FILENAME_BASE works great for removing the .js from the file name but in this case, my component file's extension is fun-thing.component.js and the container will follow suit with an extension that is fun-thing.container.js.

The Regex I am using to select everything before the first period is ^([^.]+)

  "Redux Container": {
    "prefix": "rc",
    "body": [
      "// @flow",
      "import { connect } from 'react-redux';",
      "import { ${TM_FILENAME/^([^.]+)/${1:/pascalcase}/}Component } from './${TM_FILENAME/^([^.]+)/$1/}.component';",
      "",
      "const mapStateToProps = (state) => ({});",
      "",
      "const mapDispatchToProps = {};",
      "",
      "export const ${TM_FILENAME/^([^.]+)/${1:/pascalcase}/} = connect(",
      "  mapStateToProps,",
      "  mapDispatchToProps",
      ")(${TM_FILENAME/^([^.]+)/${1:/pascalcase}/}Component);"
    ],
    "description": "Creates a normal container for a normal component"
  }

expected

// @flow
import { connect } from 'react-redux';
import { FunThingComponent } from './fun-thing.component';
...

actual

// @flow
import { connect } from 'react-redux';
import { FunThing.container.jsComponent } from './FunThing.container.js.component';
...

As you can see, it is not omitting the file extensions.

解决方案

Both of these work:

  "import { ${TM_FILENAME/^([^.]+).*/${1:/pascalcase}/}Component } from './${TM_FILENAME/^([^.]+).*/$1/}.component';",

  "import { ${TM_FILENAME/(.*?)\\..+/${1:/pascalcase}/}Component } from './${TM_FILENAME/(.*?)\\..+/$1/}.component';"

With the vscode snippet transforms if you want to exclude part of the variable as you want, removing the .component.js from the filename, then that part of the variable must be accounted for in the regex - hence (.*?)\\..+.

Otherwise that "unseen" part of the variable just passes through.

So your regex ^([^.]+) was accurately capturing the part of the filename before the first . but then the rest of the variable was "passing through" unmodified.

You can see this more clearly with this example:

"import { ${TM_FILENAME//${1:/pascalcase}/}Component }

that yields:

import { fun-thing.component.jsComponent }

so the entire filename is passed through although none of it is captured.

${someVariable/everything To Be Transformed/what To Do To the previous/}

if it isn't in the "everything to be transformed" part nothing happens to it.

这篇关于除了使用 TM_FILENAME_BASE 之外,有没有办法修剪 TM_FILENAME?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆