Skip to content

Static analyzer#377

Merged
miripiruni merged 21 commits intomasterfrom
static-analyze
Jan 18, 2017
Merged

Static analyzer#377
miripiruni merged 21 commits intomasterfrom
static-analyze

Conversation

@miripiruni
Copy link
Contributor

@miripiruni miripiruni commented Oct 26, 2016

Fixes

Changes proposed in this pull request

I introduce two features:

  1. Static linter
  2. Mirgation scripts

Both of them on jscodeshift + my boilerplate code for logging and AST checking.

How static lint work?

bem-xjst $ ./migration/lib/index.js --lint --input ../islands/common.blocks/ --from 7 --to 8

lint — lint option (if not present script will rewrite your templates)
input — path to templates (relative to current directory or absolute)
7 — current major version of bem-xjst templates
8 — version to check for

The output of comand will be:

Check rule: js() mode to addJs() mode

Processing 325 files...
Spawning 3 workers...
Sending 50 files to free worker...
…

BEM-XJST WARNING:
>>>> From v8.x you should use addJs() instead of js()
>>>> ../islands/common.blocks/ticker/ticker.bemhtml.js:25:4
>>>>
    js()(function() {

>>>>

All done.
Results:
0 errors
325 unmodified
0 skipped
0 ok
Time elapsed: 1.047seconds
…

How migrate tool work?

Migration tool work the same as lint tool. Except you shouldn’t pass lint option.

Templates will be changed. So be aware of changing something without version control.

TODO

  • Documentation changed
  • Tests
  • Collect list of transformers/linters (see below)

Transformers:
for v7 to v8

  • mix() to addMix()
  • attrs() to addAttrs()

for v6 to v7

  • this.isArray(argument) to Array.isArray(argument)
  • once() to def()
  • For safety: modifiers value should be String type. In BEMJSON and in template. If not so - we must warn you. Comare sandbox example in v6.x and v7.x https://goo.gl/auhOOV
  • for backwards capability use xhtml: true option value in all bemxjst.compile() functions.

for v5 to v6

  • There is nothing to rewrite in templates. (Really?)

for v4 to v5

  • Replace elemMatch(argument) to elem('*').match(function() { ... }).
  • mods for elem (instead of elemMods). behavior mods and elemMods BEMJSON fields are changed. bem-xjst should not treat elemMods as mods. See qfox’s commentBEMJSON tested in runtime lint.
  • API changed: require('bem-xjst') now returns two engines bemhtml and bemtree. Replace require('bem-xjst') to require('bem-xjst').bemhtml.

for v2 to v3

  • Do not use apply.call(BEMJSON). Use apply(BEMJSON).

for v1 to v2

  • Default (def()) mode of templates MUST return a string now.
  • this._ no more
  • mode('') no more

Some common checks:

  • Function that returns a literal can be replaced with literal itself.
  • warn about this.mods && this.mods.some should be this.mods.some
  • mix()([]) to function generator instead: function() { return []; }.
  • warn about html entities. Suggest UTF-8 symbols.
  • assignment to ctx.mods (instead of this.mods) — checked in runtime linter

@@ -0,0 +1,16 @@
{
"name": "bem-xjst-static-analyzer",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ух, а почему это отдельный модуль?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(кажется я уже писал ответ, но гитхаб его не отображает почему-то)

Это не отдельный модуль, это просто результат запуска npm init, чтобы сделать зависимости.

Copy link
Member

@arikon arikon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вопросик


cmd = cmd.join(' ');

execSync(cmd, { stdio: [ 0, 1, 2 ], encoding: 'utf8' })
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Проверить что exit 1 прокидывается.

console.warn(shift, opts.descr);
console.warn(shift, opts.file.path + ':' + opts.path.start.line);
console.warn(shift, opts.file.source.slice(opts.path.start, opts.path.end));
console.warn('\n');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

package.json Outdated
"bem-xjst": "bin/bem-xjst"
"bem-xjst": "bin/bem-xjst",
"lint": "bin/lint",
"mirgate": "bin/migrate"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

кажется, что так у нас появится тулза mirgate (miRgate?) и lint, это не очень, могут быть конфликты

bin/lint Outdated
@@ -0,0 +1,7 @@
if [ "$#" -ne 2 ]; then
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename file to bin/bem-xjst-lint


cmd = cmd.join(' ');

execSync(cmd, { stdio: [ 0, 1, 2 ], encoding: 'utf8' })
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For convenience, options.stdio may be one of the following strings:
'pipe' - equivalent to ['pipe', 'pipe', 'pipe'](the default)
'ignore' - equivalent to ['ignore', 'ignore', 'ignore']
'inherit' - equivalent to [process.stdin, process.stdout, process.stderr] or [0,1,2]

@qfox
Copy link
Member

qfox commented Nov 1, 2016

Replace elemMatch(argument) to elem('*').match(function() { ... }).

This is for v1→v2

@qfox
Copy link
Member

qfox commented Nov 1, 2016

I don't see warn about this._buf usage. It does not work in v2+

@miripiruni miripiruni force-pushed the static-analyze branch 7 times, most recently from 28f4e7d to 39cbf67 Compare November 2, 2016 14:02
@coveralls
Copy link

coveralls commented Jan 18, 2017

Coverage Status

Coverage remained the same at 97.048% when pulling d184d0f on static-analyze into a4146c1 on master.

@miripiruni
Copy link
Contributor Author

miripiruni commented Jan 18, 2017

Time to finish.
Integration tests on Yandex.Money, bem-components and Islands went very well.

@coveralls
Copy link

coveralls commented Jan 18, 2017

Coverage Status

Coverage remained the same at 97.048% when pulling b7587be on static-analyze into edac12f on master.

@miripiruni miripiruni merged commit 0b3b876 into master Jan 18, 2017
@miripiruni miripiruni deleted the static-analyze branch January 18, 2017 12:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants